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

Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32136    Accepted Submission(s): 15965

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.
分析:
完全背包问题,注意初始化:
1.要求恰好装满背包(恰好装满存钱罐)
memset(dp,0x3f,sizeof(dp));//无穷大
dp[0]=0;
2.找的是最小值,把max改为min
 dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
代码如下:
#include<bits/stdc++.h>
#define max_v 10005
using namespace std;
int v[max_v],w[max_v];
int dp[max_v];
int main()
{int t;scanf("%d",&t);while(t--){int x1,x2,c;scanf("%d %d",&x1,&x2);c=abs(x1-x2);int n;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d %d",&v[i],&w[i]);}memset(dp,0x3f,sizeof(dp));//无穷大 //要求恰好装满背包dp[0]=0;for(int i=0;i<n;i++){for(int j=w[i];j<=c;j++){dp[j]=min(dp[j],dp[j-w[i]]+v[i]);}}if(dp[c]==dp[max_v-2]){printf("This is impossible.\n");}else{printf("The minimum amount of money in the piggy-bank is %d.\n",dp[c]);}}return 0;
}

转载于:https://www.cnblogs.com/yinbiao/p/9093017.html

HDU 1114(没有变形的完全背包)相关推荐

  1. D - 猪钱罐 HDU - 1114

    D - 猪钱罐 HDU - 1114 在 ACM 能够开展之前,必须准备预算,并获得必要的财力支持.该活动的主要收入来自于 Irreversibly Bound Money (IBM).思路很简单.任 ...

  2. HDU 1114—Piggy-Bank(储蓄罐)(完全背包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意 开始时输入T,代表一共有T个样例. 对于每一个样例,第一行给出E与F(1<=E,F< ...

  3. HDU 3033 I love sneakers! (分组背包变形)

    题目大意:xx去买鞋,有k种牌子,然后给出n双鞋,每双鞋有它属于的牌子.价格.收藏价值.xx认为他不差钱,要求每种鞋子买一双.但实际上他只有m毛钱,问能否买到符合xx要求的鞋,能找到的话输出最大的收藏 ...

  4. HDU 1114 Piggy-Bank 猪仔储钱罐(完全背包)

    题意: 给定一个存钱罐中要存硬币,知道空罐的重量和欲装满的重量,是否能装入?若能,打印最小价值.(注:能装的硬币重量一定刚刚好,里面的总价值要达到最小) 输入: 包含了T个测试例子,在第一行给出.接下 ...

  5. hdu 1114 完全背包

    传送门 Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. HDU 1114 iggy-Bank(完全背包)

    水 给出小猪钱罐的重量和装满钱后的重量,然后是几组数据,每组数据包括每种钱币的价值与重量 要求出重量最少能装满钱罐时的最大价值 1 #include<iostream> 2 #includ ...

  7. Piggy-Bank HDU - 1114(多重背包)

    在 ACM 能够开展之前,必须准备预算,并获得必要的财力支持.该活动的主要收入来自于 Irreversibly Bound Money (IBM).思路很简单.任何时候,某位 ACM 会员有少量的钱时 ...

  8. HDU 1114 Piggy-Bank 存钱罐

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1114 题目: Piggy-Bank Time Limit: 2000/1000 MS (Java/Oth ...

  9. hdu 5616 Jam's balance 正反背包+转换

    http://acm.hdu.edu.cn/showproblem.php?pid=5616 思路 题目中蕴含着两种需要计算的重量 1. 从所有的砝码中挑出任意种 2.(转换的思想)在天平的两端都挑出 ...

最新文章

  1. 关于write()和fsync()
  2. linux avd 界面,Android Studio创建AVD
  3. Sql Server 监控 Job 执行情况
  4. Shell脚本之一 Shell脚本简介
  5. PerlRegex堆栈溢出的问题
  6. 【斐波拉切数列第N项】
  7. 无聊特意去拍月亮。呵呵
  8. openSUSE 安装 Torch
  9. 电力电子仿真软件对比分析
  10. z世代中的z是什么意思_开放是与Z世代合作的关键
  11. 关于WPF你应该知道的2000件事
  12. 如何设置电脑的保护色?
  13. MOS 的TJ TA TC和功耗之类的计算
  14. Notepad++安装HexEditor插件
  15. 防范于未“燃”|涂鸦智慧社区推出“黑科技”,电瓶车禁入电梯智慧方案
  16. 使用苹果发布证书打包好的ipa如何安装到ios手机上测试
  17. matlab pi表示,matlab中pi是什么意思
  18. 信息系统项目管理师速记 - 十大知识领域
  19. PHP实现的日志收集系统
  20. 大咖实录|漆远蚂蚁金服首席数据科学家等演讲实录

热门文章

  1. Bit-Z收不到邮箱验证码怎么办(如何添加Bit-Z至邮箱白名单?)
  2. An Introduction to Hashing in the Era of Machine Learning
  3. 【进阶】从linux到android,进程的方方面面
  4. 深入理解Android的startservice和bindservice
  5. 网站假设php,PHP个人网站架设连环讲(一)
  6. JZOJ 100045. 【NOIP2017提高A组模拟7.13】好数
  7. ubuntu14.04安装完ros后常用的其他安装
  8. 3d 强制添加脚本_每日学点---Jenkins执行脚本权限问题
  9. ultraedit 运行的是试用模式_Wings面向企业级的单元测试用例自动编码引擎
  10. 电脑屏幕保护怎么取消_怎么监控员工电脑?老板如何知道员工上班在干什么?...