AcWing 7. 混合背包问题
原题链接
中等
作者:
总有刁民想害朕
,
2020-03-11 21:51:06
,
所有人可见
,
阅读 596
思路简单,,就没必要写了
C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2000;
struct thing{
int kind;
int v, w;
};
vector<thing> things;
int dp[N];
int n, m;
int main(){
cin >> n >> m;
for(int i = 0;i < n;++i){
int v, w, s;
cin >> v >> w >> s;
if(s < 0) things.push_back({-1, v, w});
else if(s == 0) things.push_back({0, v, w});
else{
for(int k = 1;k <= s;k *= 2){
s -= k;
things.push_back({-1, k*v, k*w});
}
if(s > 0) things.push_back({-1, s*v, s*w});
}
}
for(int i = 0;i < things.size();++i){
if(things[i].kind < 0){
for(int j = m;j >= things[i].v;--j)
dp[j] = max(dp[j], dp[j-things[i].v] + things[i].w);
}else{
for(int j = things[i].v;j <= m;++j)
dp[j] = max(dp[j], dp[j-things[i].v] + things[i].w);
}
}
cout << dp[m] << endl;
return 0;
}