【解题报告】【HDOJ1114】【完全背包】Piggy-Bank

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114

注:
1. 01背包使用的是逆序,完全背包使用的顺序。
2.此题不是求最大,而是在满足背包全满的条件下,使之价值最小,也就是求最低下限。
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MAX 501
 4 #define INF 0x3f3f3f3f
 5 #define min(a,b) (a>b?b:a)
 6 int main()
 7 {
 8     long int weight[MAX],value[MAX],record[20*MAX];
 9     int T,E,F,N,v,i,j;
10     //freopen("1.txt.","r",stdin);//文件输入
11     scanf("%d",&T);
12     while(T--)
13     {
14         scanf("%d%d",&E,&F);
15         v=F-E;
16         scanf("%d",&N);
17         for(i=0;i<N;i++)
18             scanf("%d%d",&value[i],&weight[i]);
19         memset(record,INF,sizeof(record));
20         record[0]=0;//至关重要
21         for(i=0;i<N;i++)
22         { for(j=weight[i];j<=v;j++)
23         record[j]=min(record[j],record[j-weight[i]]+value[i]);
24         }
25         //输出
26         if(record[v]==INF) printf("This is impossible.\n");
27         else 
28             printf("The minimum amount of money in the piggy-bank is %ld.\n",record[v]);
29     } 
30     return 0;
31 }

 

posted on 2012-07-21 18:49  coding封神  阅读(116)  评论(0编辑  收藏  举报

导航