分组背包
分组背包模板题
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//Mystery_Sky
//
#define M 1000
int f[M];
int v, n, k;
int tot[M], w[M][M], c[M][M];
int main() {
scanf("%d%d%d", &v, &n, &k);
for(int i = 1; i <= n; i++) {
int W, C, P;
scanf("%d%d%d", &W, &C, &P);
tot[P]++;
w[P][tot[P]] = W;
c[P][tot[P]] = C;
}
for(int i = 1; i <= k; i++) {
for(int j = v; j >= 0; j--) {
for(int l = 1; l <= tot[i]; l++) {
if(j - w[i][l] >= 0)
f[j] = max(f[j], f[j - w[i][l]] + c[i][l]);
}
}
}
printf("%d
", f[v]);
return 0;
}