hdu 1114 基础完全背包

题意:给一个储钱罐,已知空的储钱罐和装了硬币的储钱罐的质量。然后给了n种硬币的质量和价值。
问储钱罐里最少有多少钱。
解法:完全背包。注意要初始化为 INF,要正好装满,如果结果是INF,输出This is impossible.
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=550;
 9 const int INF=0x3f3f3f3f;
10 int n,m,t;
11 int dp[100100],w[maxn],v[maxn];
12 int main()
13 {
14     int i,j,k;
15     #ifndef ONLINE_JUDGE
16     freopen("1.in","r",stdin);
17     #endif
18     scanf("%d",&t);
19     while(t--)
20     {
21         int a,b;
22         scanf("%d%d",&a,&b);
23         int W=b-a;
24         scanf("%d",&n);
25         for(i=0;i<n;i++)   scanf("%d%d",&v[i],&w[i]);
26         for(i=1;i<=W;i++) dp[i]=INF;
27         dp[0]=0;
28         for(i=0;i<n;i++)
29         {
30             for(j=w[i];j<=W;j++)
31             {
32                     dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
33             }
34         }
35         if(dp[W]==INF)  printf("This is impossible.\n");
36         else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]);
37     }
38 }

 

转载于:https://www.cnblogs.com/cnblogs321114287/p/4354764.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值