背包问题的板子
作者:
008
,
2023-03-12 16:43:06
,
所有人可见
,
阅读 181
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 20010;
int n, V;
int v, w, s;
int f[N];
void zeroOnePack(int v, int w) {
for (int j = V; j >= v; --j)
f[j] = max(f[j], f[j - v] + w);
}
void completePack(int v, int w) {
for (int j = v; j <= V; ++j)
f[j] = max(f[j], f[j - v] + w);
}
void multiPack(int v, int w, int s) {
if (s * v >= V) {
completePack(v, w);
return;
}
int k = 1;
while (k <= s) {
zeroOnePack(k * v, k * w);
s -= k;
k <<= 1;
}
if (s > 0) zeroOnePack(s * v, s * w);
}
int main(void) {
scanf("%d%d", &n, &V);
while (n--) {
scanf("%d%d%d", &v, &w, &s);
multiPack(v, w, s);
}
printf("%d\n", f[V]);
return 0;
}