Files
python/TangDou/BeiBao/bbdc2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
798 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 12010;
int n, m;
int v[N], w[N];
int f[N];
int idx;
//多重背包的二进制优化
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int vi, wi, si;
cin >> vi >> wi >> si;//二进制优化,能打包则打包之,1,2,4,8,16,...
int b = 1;
while (b <= si) {
v[++idx] = vi * b;
w[idx] = wi * b;
si -= b;
b *= 2;
}
//剩下的
if (si > 0) {
v[++idx] = vi * si;
w[idx] = wi * si;
}
}
for (int i = 1; i <= idx; i++)
for (int j = m; j >= v[i]; j--)
f[j] = max(f[j], f[j - v[i]] + w[i]);
printf("%d", f[m]);
return 0;
}