Piggy-Bank

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

Total Submission(s): 19735    Accepted Submission(s): 10020

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
  1. 3
  2. 10 110
  3. 2
  4. 1 1
  5. 30 50
  6. 10 110
  7. 2
  8. 1 1
  9. 50 30
  10. 1 6
  11. 2
  12. 10 3
  13. 20 4
 
Sample Output
  1. The minimum amount of money in the piggy-bank is 60.
  2. The minimum amount of money in the piggy-bank is 100.
  3. This is impossible.
  4. /*题目大意:已知猪灌所能容纳的重量,然后告诉若干硬币的价值与重量。求使得用已知硬币装入猪灌
  5.  *          中使得猪灌中硬币价值总和最小 ,且要求猪灌必须被装满,若不能装满则输出 This is impossible.
  6. */
  7. #include <iostream>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <cstdio>
  11. using namespace std;
  12. const int maxn = 999999;
  13. #define mem(a) memset(a, 0, sizeof(a))
  14. int dp[10010];				//dp[i]表示所装重量为i时候的最小价值
  15. struct node {
  16. 	int p, w;
  17. }a[550];
  18. int main() {
  19. 	int t;
  20. 	scanf("%d",&t);
  21. 	while (t --) {
  22. 		mem(a);
  23. 		mem(dp);
  24. 		int e, f;
  25. 		scanf("%d%d",&e, &f);
  26. 		e = f-e;
  27. 		for (int i = 0; i<=e; i++)	dp[i] = maxn;
  28. 		dp[0] = 0;
  29. 		int n;
  30. 		scanf("%d",&n);
  31. 		for (int i = 1; i<=n; i++)	scanf("%d%d",&a[i].p, &a[i].w);
  32. 		for (int i = 1; i<=n; i++) {
  33. 			for (int j = a[i].w; j<=e; j++) {
  34. 				dp[j] = min(dp[j], dp[j-a[i].w] + a[i].p);
  35. 			}
  36. 		}
  37. 		if (dp[e] == maxn)	printf("This is impossible.\n");
  38. 		else 	printf("The minimum amount of money in the piggy-bank is %d.\n",dp[e]);
  39. 	}
  40. 	return 0;
  41. }

  42.  

HDU1114Piggy-Bank(完全背包)的更多相关文章

  1. BZOJ 1531: [POI2005]Bank notes( 背包 )

    多重背包... ---------------------------------------------------------------------------- #include<bit ...

  2. bzoj1531: [POI2005]Bank notes(多重背包)

    1531: [POI2005]Bank notes Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 521  Solved: 285[Submit][Sta ...

  3. 【多重背包小小的优化(。・∀・)ノ゙】BZOJ1531-[POI2005]Bank notes

    [题目大意] Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出 ...

  4. 【bzoj1531】[POI2005]Bank notes 多重背包dp

    题目描述 Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值 ...

  5. bzoj 1531 Bank notes 多重背包/单调队列

    多重背包二进制优化终于写了一次,注意j的边界条件啊,疯狂RE(还是自己太菜了啊啊)最辣的辣鸡 #include<bits/stdc++.h> using namespace std; in ...

  6. 2018.09.08 bzoj1531: [POI2005]Bank notes(二进制拆分优化背包)

    传送门 显然不能直接写多重背包. 这题可以用二进制拆分/单调队列优化(感觉二进制好写). 所谓二进制优化,就是把1~c[i]拆分成20,21,...2t,c[i]−2t+1+1" role= ...

  7. bzoj1531: [POI2005]Bank notes

    Description Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我 ...

  8. DSY1531*Bank notes

    Description Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我 ...

  9. Hdu 2955 Robberies 0/1背包

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

随机推荐

  1. iOS js oc相互调用JavaScriptCore(一)

    原址:http://blog.csdn.net/lwjok2007/article/details/47058101 1.普通调用 从iOS7开始 苹果公布了JavaScriptCore.framew ...

  2. bzoj 3575: [Hnoi2014]道路堵塞

    Description A 国有N座城市,依次标为1到N.同时,在这N座城市间有M条单向道路,每条道路的长度是一个正整数.现在,A国交通部指定了一条从城市1到城市N的路径, 并且保证这条路径的长度是所 ...

  3. 限制ssh远程登陆

    超过十次,就添加到hosts.deny里面去 #!/bin/bash date=`date +%Y%m%d` file="/var/log/secure" max=10 if [[ ...

  4. Centos6.8防火墙配置

    1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service iptables star ...

  5. centos7 系统安装问题汇总

    centos7 系统安装问题汇总: 1.使用u盘 安装centos7时,一直提示:'.../dev/root  does not exist,could not boot' 解决方法: 2.不能将原来 ...

  6. 例子:web版坦克大战1.0

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. day 10 字符编码和文件处理 细节整理

    pycharm是文本编辑器. 大概理解为:  输出到屏幕上的时候,是解码过的字符串,用 decode 处理的时候要编码成相应的流, encode 成你要用的格式就可以了 1 .字符编码: 字符==== ...

  8. opensuse安装pycurl失败记录

    早上在opensuse安装pycurl,一直出现如下错误: pepper@VM_56_243_suse:~/code/gitosis-autotest> pip install pycurl C ...

  9. vue2.0 通过v-html指令渲染的富文本无法修改样式的解决方案

    在最近的vue项目中遇到的问题:v-html渲染的富文本,无法在样式表中修改样式: 比如下面的代码,div.descBox里面的p标签的color样式并不是"color: blue" ...

  10. Python 字典中一键对应多个值

    #字典的一键多值 print'方案一 list作为dict的值 值允许重复' d1={} key=1 value=2 d1.setdefault(key,[]).append(value) value ...