You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 1010; // 个数上限
const int M = 2010; // 体积上限
int n, m, idx;
// 无法使用二维数组原因是因为分拆后N*31*M=31*1010*2010太大了MLE了
// 所以,需要使用滚动数组进行优化一下,思想还是二维的
int f[2][M];
struct Node {
int w, v;
} c[N * 31];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int v, w, s;
cin >> v >> w >> s;
for (int j = 1; j <= s; j *= 2) { // 1,2,4,8,16,32,64,128,...打包
c[++idx] = {j * w, j * v};
s -= j;
}
// 不够下一个2^n时独立成包
if (s) c[++idx] = {s * w, s * v};
}
// 按01背包跑就可以啦
for (int i = 1; i <= idx; i++)
for (int j = 1; j <= m; j++) {
f[i & 1][j] = f[i - 1 & 1][j];
if (j >= c[i].v)
f[i & 1][j] = max(f[i & 1][j], f[i - 1 & 1][j - c[i].v] + c[i].w);
}
// 输出
printf("%d\n", f[idx & 1][m]);
return 0;
}