AcWing 4. 多重背包问题 I
原题链接
简单
作者:
tony_zsr
,
2024-12-07 11:27:21
,
所有人可见
,
阅读 2
算法1
(暴力枚举) $O(n^2)$
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 25000;
int f[N], v[N], w[N];
int n, m;
int main(){
cin >> n >> m;
int cnt = 0;
for(int i = 1; i <= n; i ++){
int a, b, s;
cin >> a >> b >> s;
int k = 1;
while(k <= s){
cnt ++;
v[cnt] = k * a;
w[cnt] = k * b;
s -= k;
k *= 2;
}
if(s > 0){
cnt ++;
v[cnt] = s * a;
w[cnt] = s * b;
}
}
for(int i = 1; i <= cnt; i ++){
for(int j = m; j >= v[i]; j --){
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
cout << f[m] << endl;
return 0;
}