hdu 2191 多重背包

悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7445    Accepted Submission(s): 3065


Problem Description
急!灾区的食物依然短缺!
为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。
请问:你用有限的资金最多能采购多少公斤粮食呢?

后记:
人生是一个充满了变数的生命过程,天灾、人祸、病痛是我们生命历程中不可预知的威胁。
月有阴晴圆缺,人有旦夕祸福,未来对于我们而言是一个未知数。那么,我们要做的就应该是珍惜现在,感恩生活——
感谢父母,他们给予我们生命,抚养我们成人;
感谢老师,他们授给我们知识,教我们做人
感谢朋友,他们让我们感受到世界的温暖;
感谢对手,他们令我们不断进取、努力。 
同样,我们也要感谢痛苦与艰辛带给我们的财富~

 
Input
输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。
 
Output
对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。
 
Sample Input
1
8 2
2 100 4
4 100 2
 
Sample Output
400

由上一篇日志学到的一般的多重背包问题,可以套用相似的模板,直接水过:
View Code
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 
 7 int dp[200];
 8 
 9 void CompletePack(int cost, int weight,int n)    //cost 费用, weight 价值
10 {
11     int i;
12     for(i=cost;i<=n;i++)
13         dp[i]=max(dp[i],dp[i-cost]+weight);
14 }
15 
16 void Zero_OnePack(int cost,int weight,int n)
17 {
18     int i;
19     for(i=n;i>=cost;i--)
20         dp[i]=max(dp[i],dp[i-cost]+weight);
21 }
22 
23 int main()
24 {
25     int cost[200],weight[200],amount[200];
26     int i,j,k;
27     int t,n,m;
28     scanf("%d",&t);
29     while(t--)
30     {
31         scanf("%d%d",&n,&m);
32         for(i=0;i<m;i++)
33             scanf("%d%d%d",&cost[i],&weight[i],&amount[i]);
34         memset(dp,0,sizeof(dp));
35         for(i=0;i<m;i++)
36         {
37             if(cost[i]*amount[i]>n) CompletePack(cost[i],weight[i],n);
38             else 
39             {
40                 k=1;
41                 while(k<amount[i])
42                 {
43                     Zero_OnePack(k*cost[i],k*weight[i],n);
44                     amount[i]-=k;
45                     k*=2;
46                 }
47                 Zero_OnePack(amount[i]*cost[i],amount[i]*weight[i],n);
48             }
49         }
50         printf("%d\n",dp[n]);
51     }
52  return 0;
53 }

在网站上还看到别人不一样的做法,代码虽短,但是要难懂一点:

View Code
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int dp[120],cost[120],weight[120],amount[120];
10     int i,j,k;
11     int n,m,t;
12     scanf("%d",&t);
13     while(t--)
14     {
15         scanf("%d%d",&n,&m);
16         for(i=0;i<m;i++)
17         {
18             scanf("%d%d%d",&cost[i],&weight[i],&amount[i]);
19         }
20         memset(dp,0,sizeof(dp));
21         int ans=-1;
22         for(i=0;i<m;i++)
23         {
24             for(j=1;j<=amount[i];j++)
25             {
26                 for(k=n;k>=cost[i];k--)
27                 {
28                     dp[k]=max(dp[k],dp[k-cost[i]]+weight[i]);
29                     if(dp[k]>ans)
30                         ans=dp[k];
31                 }
32             }
33         }
34         cout<<ans<<endl;
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/shenshuyang/p/2645178.html