#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 6
#define W 21
int B[N][W] = { 0 };
int w[6] = { 0,2,3,4,5,9 };
int v[6] = { 0,3,4,5,8,10 };
void knapsack() {
int k, C;
for (k = 1; k < N; k++) {
for (C = 1; C < W; C++) {
if (w[k] > C) {
B[k][C] = B[k - 1][C];
}
else
{
B[k][C] = max(B[k-1][C-w[k]]+v[k],B[k-1][C]);
}
}
}
}
int main() {
knapsack();
printf("%d\n", B[5][20]);
}
01背包问题
最新推荐文章于 2024-08-22 17:27:46 发布