Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description

Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.

Output

Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".

Sample Input

3

10 110

2

1 1

30 50

10 110

2

1 1

50 30

1 6

2

10 3

20 4

Sample Output

The minimum amount of money in the piggy-bank is 60.

The minimum amount of money in the piggy-bank is 100.

This is impossible.

Source

Central Europe 1999

Recommend

Eddy

题意:有一个存钱罐,知道空的时候跟满的时候的重量,知道它里面每种硬币的重量,求它里面最少有多少钱。

题解:每种硬币数量不限,所以是完全背包。条件必须在装满的情况下,求最小价值,开始初始化为最大值,如果遍历完物品后满容量的dp值没有被改变则说明这些硬币无法组成这个容量。

  1. #include <queue>
  2. #include <stack>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <algorithm>
  9. using namespace std;
  10. const int maxn = 10050;
  11. const int INF = 1e9+7;
  12. int dp[maxn];
  13. struct node
  14. {
  15. int data,w;
  16. }s[550];
  17. bool cmp(node a,node b)
  18. {
  19. if(a.w==b.w)
  20. return a.data<b.data;
  21. return a.w<b.w;
  22. }
  23. int main()
  24. {
  25. int m,n,i,j,t;
  26. scanf("%d",&t);
  27. //printf("%d\n",t);
  28. while(t--)
  29. {
  30. //printf("%d\n",t);
  31. scanf("%d%d",&m,&n);
  32. n -= m;
  33. scanf("%d",&m);
  34. for(i=0;i<m;i++)
  35. scanf("%d%d",&s[i].data,&s[i].w);
  36. for(i=0;i<=n;i++)
  37. dp[i] = INF;
  38. dp[0] = 0;
  39. for(i=0;i<m;i++)
  40. {
  41. for(j=s[i].w;j<=n;j++)
  42. dp[j] = min(dp[j],dp[j - s[i].w] + s[i].data);
  43. }
  44. if(dp[n]==INF)
  45. printf("This is impossible.\n");
  46. else
  47. printf("The minimum amount of money in the piggy-bank is %d.\n",dp[n]);
  48. }
  49. return 0;
  50. }

HDU-1114_Piggy-Bank的更多相关文章

  1. hdu 2955 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...

  2. HDU 2955 Robberies 背包概率DP

    A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  3. hdu 1908 Double Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...

  4. HDU 5493 Queue 树状数组

    Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...

  5. [HDU 1963] Investment

    Investment Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Descrip ...

  6. POJ 3481 &amp; HDU 1908 Double Queue (map运用)

    题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...

  7. HDU 2955 Robberies(DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目: Problem Description The aspiring Roy the Rob ...

  8. HDU 2955 变形较大的01背包(有意思,新思路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 Robberies Time Limit: 2000/1000 MS (Java/Others) ...

  9. hdu 2955(概率转化,01背包)

    Hot~~招聘——巴卡斯(杭州),壹晨仟阳(杭州),英雄互娱(杭州) (包括2016级新生)除了校赛,还有什么途径可以申请加入ACM校队? Robberies Time Limit: 2000/100 ...

  10. HDU 3232 &amp;&amp; UVA 12230 (简单期望)

    Crossing Rivers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. 洛谷 3089 [USACO13NOV]POGO的牛Pogo-Cow

    单调队列优化dp; 对于每个点开个单调队列,按转移到它的点到它的距离从大到小,得分也从大到小排列. 每次枚举当前点前面的所有点,对于每个点的队列中二分一个距离小于等于它到当前点的答案值,放到当前点的队 ...

  2. 一探前端开发中的JS调试技巧(转)

    有请提示:文中涉及较多Gif演示动画,移动端请尽量在Wifi环境中阅读 前言:调试技巧,在任何一项技术研发中都可谓是必不可少的技能.掌握各种调试技巧,必定能在工作中起到事半功倍的效果.譬如,快速定位问 ...

  3. Hdu 1384(差分约束)

    题目链接 Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. spring cloud深入学习(二)-----服务注册中心spring cloud eureka

    服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...

  5. Codeforces Round #436 (Div. 2) 题解864A 864B 864C 864D 864E 864F

    A. Fair Game time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  6. 获取电脑名和Ip

    private string  GetHostNameAndIP( bool  isv4Orv6)        {            string HostName = Dns.GetHostN ...

  7. Java static 关键字学习

    static:意为静态的,简单理解就是公共的.独立于实例变量之外的1.概述:static是Java中常用的关键字,一般用于变量.方法.静态代码块.内部类上.静态导包2.用法: a.用于变量上表示该变量 ...

  8. 全面系统Python3入门+进阶课程

    全面系统Python3入门+进阶课程 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候 ...

  9. from和modelform的用法和介绍

    from和modelform的用法和介绍   一 form 1. form的作用 1. 生成HTML代码 2. 帮我们做数据有效性的校验 3. 保留上次输入内容,显示错误提示 2. form组件校验数 ...

  10. tcpdump抓取udp报文

    使用tcpdump命令抓取UDP 2000端口报文,并将报文保存到当前目录下的udp.cap文件,命令如下: tcpdump -i 网络接口名称 udp port 2000 -w ./udp.cap ...