DP 集合定义:类似01背包,完全背包
DP[i][j]:选完第i个物品后,容量是j的背包,(选法)方案的最大价值
f[i][j]=max(f[i][j],f[i-1][j-kv[i]]+kw[i]);
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int main()
{
int n, V, f[N][N],v[N],w[N],s[N];
cin >> n >> V;
for (int i = 1; i <= n; i ++ ){
cin >> v[i] >> w[i] >> s[i];
}
for (int i = 1; i <= n; i ++ ){
for (int j = 1; j <=V; j ++ ){
for (int k = 0; k <= s[i] && k*v[i]<=j; k ++ ){
f[i][j] = max(f[i][j],f[i -1][j-k*v[i]]+k*w[i]);
}
}
}
cout << f[n][V] << endl;
return 0;
}
优化:
等价变形:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int main()
{
int n, V, f[N],v[N],w[N],s[N];
cin >> n >> V;
for (int i = 1; i <= n; i ++ ){
cin >> v[i] >> w[i] >> s[i];
}
for (int i = 1; i <= n; i ++ ){
for (int j = V; j >=0; j -- ){ //和01背包优化一个原理。递推顺序,反着的话f[i],f[i-1]顺序是对的,还是上一轮的f[i-1]而不是当前i轮更新过的
for (int k = 0; k <= s[i] && k*v[i]<=j; k ++ ){
f[j] = max(f[j],f[j-k*v[i]]+k*w[i]);
}
}
}
cout << f[V] << endl;
return 0;
}