HDU1114--Piggy-Bank(完全背包变形)
Piggy-Bank
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
| Total Submission(s): 557 Accepted Submission(s): 304 |
|
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 |
|
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 |
|
Sample Output
The minimum amount of money in the piggy-bank is 60. |
|
Source
Central Europe 1999
|
|
Recommend
Eddy
|
题目大意:
多种硬币放入一个存钱罐中(不限数目), 每种硬币 i 有价值 Vi 与重量 Wi 两个属性, 求给定重量 y-x 下最小的硬币钱数n
解题思路:
这是一个多重背包的变形[每种硬币最多有 (y-x)/Wi 个]. 但这道题目求的是在完全装满背包的前提下最小的价值.
自然想到的是将经典的背包问题的状态转移方程进行修改, 将max改为min, 但这样处理的话, 初始化的时候需要动一下脑筋
考虑正常的背包问题, 若要求的是恰好装满的状态是, 需要将除 dp[0] 之外的其他元素全部初始化为 负无穷, 最后的结果若为负无穷,说明无解(负无穷+有限数仍为负无穷, 这样就排除掉了不恰好装满的状态) .
但这道题目要求的是最小值, 所以我们需要将需 dp[0] 之外的其他元素全部初始化为 正无穷 , dp[0]初始化为0
如此即可AC
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x7ffffff
#define MAXN 10000
using namespace std;
int dp[];
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie();
int t;
int cc;
int e,f;
int n;
cin>>t;
int p,w;
while(t--){
cin>>e>>f;
cc=f-e;
cin>>n;
dp[]=;
for(int i=;i<=cc;i++){
dp[i]=INF;
}
for(int i=;i<n;i++){
cin>>p>>w;
for(int j=w;j<=cc;j++){
dp[j]=min(dp[j],dp[j-w]+p);
}
}
if(dp[cc]==INF){
cout<<"This is impossible."<<endl;
}
else cout<<"The minimum amount of money in the piggy-bank is "<<dp[cc]<<"."<<endl;
}
}
HDU1114--Piggy-Bank(完全背包变形)的更多相关文章
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- HDU 2639 Bone Collector II(01背包变形【第K大最优解】)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- P1282 多米诺骨牌 (背包变形问题)
题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...
- 【01背包变形】Robberies HDU 2955
http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...
- J-流浪西邮之寻找火石碎片 【经典背包变形】
题目来源:2019 ACM ICPC Xi'an University of Posts & Telecommunications School Contest 链接:https://www. ...
- CF#214 C. Dima and Salad 01背包变形
C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...
- HDU 2955 Robberies(01背包变形)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
随机推荐
- 第三次冲刺spring会议(第六次会议)
[例会时间]2014/5/25 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇
- Openjudge-计算概论(A)-完美立方
描述: a的立方 = b的立方 + c的立方 + d的立方为完美立方等式.例如12的立方 = 6的立方 + 8的立方 + 10的立方 .编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组 ...
- 关于oracle数据库(9)
内连接:从多张表中获取综合数据 inner join on select 表1.字段,表2.字段 from 表1 inner join 表2 on 关系; select 表1.*,表2.* from ...
- 清空form
在清空form是遇到问题 document.formname.reset(); 当reset对checkbox不起作用时,清空其需要用 $(" ").attr("chec ...
- 发现一个c++ vector sort的bug
在开发中遇到一个非常诡异的问题:我用vector存储了一组数据,然后调用sort方法,利用自定义的排序函数进行排序,但是一直都会段错误,在排序函数中打印参加排序的值,发现有空值,而且每次都跟同一个数据 ...
- InvalidateRect只是增加重绘区域,在下次WM_PAINT的时候才生效
emWIN里面的无效重绘和windows很类似. WM_InvalidateArea()和WM_InvalidateRect()只重绘指定的区域,其他区域不会重绘,这样避免了闪烁,重绘发生在下次WM_ ...
- jquery-图片轮播(新手请大神指教一下)
这是我刚学jquery写的,感觉效果不是很好. #scrollPics{ height: 330px; width: 980px; margin-bottom: 10px; overflow: hid ...
- 一起来用css画画
hello,大白来了... <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> ...
- Varnish && Varnish Cache
1 1 1 Varnish https://www.varnish-cache.org/intro/index.html#intro Introduction to Varnish The basic ...
- CSS3秘笈:第三章
1.标签选择器:控制整体. 使用标签选择器,只需要输入标签的名称即可. 2.类选择器:精确控制. 假如你要指定一些相同的元素拥有不同的样式,可以给元素添加不同的类名,然后用类选择器来应用对应的样式. ...