'commit'
This commit is contained in:
40
TangDou/AcWing/BeiBao/5_ErWei_GunDong.cpp
Normal file
40
TangDou/AcWing/BeiBao/5_ErWei_GunDong.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user