C
二维朴素 dp
#include <stdio.h>
#define MAXSIZE 105
#define max(a,b) ((a)>(b)?(a):(b))
int N, V, v[MAXSIZE], w[MAXSIZE], s[MAXSIZE], dp[MAXSIZE][MAXSIZE];
void init() {
scanf("%d%d", &N, &V);
for (int i = 1; i <= N; ++i) {
scanf("%d%d%d", v+i, w+i, s+i);
}
}
int main() {
init();
for (int i = 1; i <= N; ++i) {
for (int j = 0; j <= V; ++j) {
for (int k = 0; k <= s[i] && k*v[i] <= j; ++k) {
dp[i][j] = max(dp[i][j], dp[i-1][j-k*v[i]] + k*w[i]);
}
}
}
printf("%d\n", dp[N][V]);
return 0;
}
一维 dp
#include <stdio.h>
#define MAXSIZE 105
#define max(a,b) ((a)>(b)?(a):(b))
int N, V, v[MAXSIZE], w[MAXSIZE], s[MAXSIZE], dp[MAXSIZE];
void init() {
scanf("%d%d", &N, &V);
for (int i = 1; i <= N; ++i) {
scanf("%d%d%d", v+i, w+i, s+i);
}
}
int main() {
init();
for (int i = 1; i <= N; ++i) {
for (int j = V; j >= v[i]; --j) {
for (int k = 0; k <= s[i] && k*v[i] <= j; ++k) {
dp[j] = max(dp[j], dp[j-k*v[i]] + k*w[i]);
}
}
}
printf("%d\n", dp[V]);
return 0;
}