码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1114Piggy-Bank

时间:2017-03-23 01:44:10      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:问题   main   using   target   i++   bsp   imp   include   nim   

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

背包问题。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=10010;
 6 const int inf=0x3f3f3f3f;
 7 int dp[maxn];
 8 int sw,sv;
 9 int n;
10 struct node
11 {
12     int w,v;
13 }p[maxn];
14 
15 int main()
16 {
17     int t;
18     scanf("%d",&t);
19     while(t--)
20     {
21         scanf("%d%d",&sw,&sv);
22         sv-=sw;
23         scanf("%d",&n);
24         for(int i=0;i<n;i++)
25             scanf("%d%d",&p[i].v,&p[i].w);
26         for(int i=0;i<=sv;i++)
27             dp[i]=inf;       //初始化的时候,如果题目要求装满,则除dp[0]=0外其他置为inf。若不要求装满,全部置为0.
28         dp[0]=0;
29         for(int i=0;i<n;i++)
30             for(int j=p[i].w;j<=sv;j++)
31             dp[j]=min(dp[j],dp[j-p[i].w]+p[i].v);
32         if(dp[sv]!=inf) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[sv]);
33         else puts("This is impossible.");
34     }
35     return 0;
36 }

 

hdu1114Piggy-Bank

标签:问题   main   using   target   i++   bsp   imp   include   nim   

原文地址:http://www.cnblogs.com/yijiull/p/6602925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!