题目链接:51nod_1086
解题思路:多重背包+二进制优化
AC代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#include <utility>
using namespace std;
const int maxn = (int)1e2+10;
int dp[50010],n,V;
int cost[maxn],value[maxn],amount[maxn];
inline int Max (int a,int b) {
return a>b?a:b;
}
void ZeroOnePack (int cost, int value) {
for (int i = V; i >= cost; i--) {
dp[i] = Max(dp[i], dp[i-cost]+value);
}
}
void CompletePack (int cost, int value) {
for (int i = cost; i <= V; i++) {
dp[i] = Max(dp[i], dp[i-cost]+value);
}
}
void MultiplePack (int cost, int value, int amount) {
if(V <= amount*cost) {
CompletePack(cost,value);
return ;
}
int k = 1;
while (k < amount) {
ZeroOnePack(k*cost,k*value);
amount -= k;
k<<=1;
}
ZeroOnePack(amount*cost,amount*value);
return ;
}
int main() {
int T;
scanf("%d%d", &n, &V);
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &cost[i], &value[i], &amount[i]);
}
memset(dp,0,sizeof(dp));
for (int i = 0; i < n; i++) {
MultiplePack(cost[i],value[i],amount[i]);
}
printf("%d\n",dp[V]);
return 0;
}