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

42 lines
1.3 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = 1010; // 个数上限
const int M = 2010; // 体积上限
int n, m, idx;
int f[M];
/*
Q:为什么是N*12?
A:本题中v_i<=2000,因为c数组是装的打包后的物品集合,每类物品按二进制思想,2000最多可以打log2(2000)+1个包,即 10.96578+1=12个足够,
同时,共N类物品,所以最大值是N*12。
如果题目要求v_i<=INT_MAX,那么就是log2(INT_MAX)=31,开31个足够,因为31是准确的数字,不需要再上取整。
为保险起见,可以不用计算数组上限,直接N*32搞定!
*/
struct Node {
int w, v;
} c[N * 12];
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 = m; j >= c[i].v; j--) // 倒序
f[j] = max(f[j], f[j - c[i].v] + c[i].w);
// 输出
printf("%d\n", f[m]);
return 0;
}