概述
//每个物品的重量
vector<int> weight;
//每个物品的价值
vector<int> value;
//每个物品的数量
vector<int> nums;
//背包的总重量
int all;
//多少种物品
int n;
01背包
一般版本
vector<vector<int>> dp(n + 1, vector<int>(all + 1, 0));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= all; j++)
{
if(weight[i] <= j)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
优化版本
vector<int> dp(all + 1, 0);
for (int i = 1; i <= n; i++)
{
//写法一
for (int j = all; j >= 1; j--)
{
if (weight[i] <= j)
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
//写法二
for (int j = all; j >= weight[i]; j--)
{
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
例题:https://vjudge.net/problem/HDU-1114
答案:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main()
{
int cases;
cin >> cases;
while(cases--)
{
//骨头n种,背包体积allv
int n, allv;
int tmp;
cin >> n >> allv;
vector<int> value(n + 1, 0);
vector<int> volume(n + 1, 0);
for (int i = 1; i <= n; i++)
{
cin >> tmp;
value[i] = tmp;
}
for (int i = 1; i <= n; i++)
{
cin >> tmp;
volume[i] = tmp;
}
vector<vector<int>> dp(n + 1, vector<int>(allv + 1, 0));
for (int i = 1; i <= n; i++)
{
//注意从0开始
for (int j = 0; j <= allv; j++)
{
if (volume[i] <= j)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - volume[i]] + value[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][allv] << endl;
}
return 0;
}
int main()
{
int cases;
cin >> cases;
while(cases--)
{
//骨头n种,背包体积allv
int n, allv;
int tmp;
cin >> n >> allv;
vector<int> value(n + 1, 0);
vector<int> volume(n + 1, 0);
for (int i = 1; i <= n; i++)
{
cin >> tmp;
value[i] = tmp;
}
for (int i = 1; i <= n; i++)
{
cin >> tmp;
volume[i] = tmp;
}
vector<int> dp(allv + 1, 0);
for (int i = 1; i <= n; i++)
{
for (int j = allv; j >= volume[i]; j--)
{
dp[j] = max(dp[j], dp[j - volume[i]] + value[i]);
}
}
cout << dp[allv] << endl;
}
return 0;
}
完全背包
一般版本
vector<vector<int>> dp(n + 1, vector<int>(all + 1, 0));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= all; j++)
{
for (int k = 0; k * weight[i] <= j; k++)
{
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - k * weight[i]] + k * value[i]);
}
}
}
优化版本
vector<int> dp(n + 1, 0);
for (int i = 1; i <= n; i++)
{
//写法一
for (int j = 1; j <= all; j++)
{
if(weight[i] <= j)
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
//写法二
for (int j = weight[i]; j <= all; j++)
{
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
例题:https://vjudge.net/problem/HDU-2602
答案:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
/*超时
int main()
{
int cases;
cin >> cases;
while(cases--)
{
int all1, all2, all;
cin >> all1 >> all2;
all = all2 - all1;
int INF = all2 * 1000;
int n;
cin >> n;
vector<int> value(n + 1, 0);
vector<int> weight(n + 1, 0);
for (int i = 1; i <= n; i++)
{
cin >> value[i] >> weight[i];
}
vector<vector<int>> dp(n + 1, vector<int>(all + 1, INF));
for (int i = 0; i <= n; i++)
{
dp[i][0] = 0;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= all; j++)
{
for (int k = 1; k * weight[i] <= j; k++)
{
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - k * weight[i]] + k * value[i]);
}
}
}
int MIN = INF;
for (int i = 1; i <= n; i++)
{
MIN = min(dp[i][all], MIN);
}
if(MIN >= INF)
cout << "This is impossible." << endl;
else
{
cout << "The minimum amount of money in the piggy-bank is " << MIN << "." << endl;
}
}
return 0;
}
*/
int main()
{
int cases;
cin >> cases;
while (cases--)
{
int all1, all2, all;
cin >> all1 >> all2;
all = all2 - all1;
int INF = all2 * 1000;
int n;
cin >> n;
vector<int> value(n + 1, 0);
vector<int> weight(n + 1, 0);
for (int i = 1; i <= n; i++)
{
cin >> value[i] >> weight[i];
}
vector<int> dp(all + 1, INF);
dp[0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = weight[i]; j <= all; j++)
{
dp[j] = min(dp[j], dp[j - weight[i]] + value[i]);
}
}
int MIN = min(INF, dp[all]);
if (MIN >= INF)
cout << "This is impossible." << endl;
else
{
cout << "The minimum amount of money in the piggy-bank is " << MIN << "." << endl;
}
}
return 0;
}
多重背包
for (int i = 1; i <= n; i++)
{
while(nums[i] != 1)
{
weight.push_back(weight[i]);
nums[i]--;
}
}
vector<int> dp(weight.size() + 1, 0);
for (int i = 1; i < weight.size(); i++)
{
for (int j = all; j >= weight[i]; j--)
{
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
最后
以上就是野性鸡为你收集整理的01背包,完全背包,多重背包模板及例题的全部内容,希望文章能够帮你解决01背包,完全背包,多重背包模板及例题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复