模板题解
#include <iostream>
#include <algorithm>
#include <cstring>
#include <math.h>
using namespace std;
const int maxn=1005, maxv=12000; //maxv不够大的时候,结果dp[V]会溢出
//ceil(log(2005.0)/log(2.0));向上取整为11
int main(){
int N=0, V=0, v[maxv], w[maxv], s=0;
int dp[maxv];
memset(dp, 0, sizeof(dp)); //不初始化可能会数据错乱
memset(v, 0, sizeof(v));
memset(w, 0, sizeof(w));
cin >> N >> V;
int count=0; //所有小包计数
while(N--){
cin >> v[0] >> w[0] >> s;
int x=1;
while(s>=x){ //打包多重背包
++count;
v[count] = x*v[0]; //容积
w[count] = x*w[0];
s -= x;
x *= 2;
}
if(s!=0){
++count;
v[count] = s*v[0]; //容积
w[count] = s*w[0];
}
}
N = count;
//类似01背包的遍历方法
for(int i=1; i<=N; ++i){
for(int j=V; j>=v[i]; --j){
dp[j] = max(dp[j], dp[j-v[i]]+w[i]);
}
}
cout << dp[V] << endl;
return 0;
}