原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=2191
一:原题内容
1 8 2 2 100 4 4 100 2
400
二:分析理解
一个多重背包模板题。。
三:AC代码
#include<iostream> #include<algorithm> using namespace std; struct Node { int price; int weight; int number; }; int n, m; Node node[110]; int dp[110]; void ComplatePack(int cost, int weight) { for (int i = cost; i <= n; i++) dp[i] = max(dp[i], dp[i - cost] + weight); } void ZeroOnePack(int cost, int weight) { for (int i = n; i >= cost; i--) dp[i] = max(dp[i], dp[i - cost] + weight); } void MultiplePack(int cost, int weight, int number) { if (cost*number >= n) { ComplatePack(cost, weight); return; } int k = 1; while (k < number) { ZeroOnePack(k*cost, k*weight); number -= k; k *= 2; } ZeroOnePack(number*cost, number*weight); } int main() { int test; scanf("%d", &test); while (test--) { scanf("%d%d", &n, &m); memset(dp, 0, sizeof(dp)); for (int i = 0; i < m; i++) scanf("%d%d%d", &node[i].price, &node[i].weight, &node[i].number); for (int i = 0; i < m; i++) MultiplePack(node[i].price, node[i].weight, node[i].number); printf("%d\n", dp[n]); } return 0; }