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
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

题目大意:

多种硬币放入一个存钱罐中(不限数目), 每种硬币 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(完全背包变形)的更多相关文章

  1. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  2. 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 ...

  3. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  4. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. P1282 多米诺骨牌 (背包变形问题)

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  6. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  7. J-流浪西邮之寻找火石碎片 【经典背包变形】

    题目来源:2019 ACM ICPC Xi'an University of Posts & Telecommunications School Contest 链接:https://www. ...

  8. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  9. HDU 2955 Robberies(01背包变形)

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

  10. 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 ...

随机推荐

  1. solr定时增量索引

    当数据库的数据发生改变的时候,我们不想手动的去重新添加数据库的数据导solr索引库中,所以用到定时添加索引.增删改的数据.现在写的这些都是基于我之前做的一步步到这来的. 将solr/dist下的sol ...

  2. Notepad++ V6.9.0 中文绿色便携版

    软件名称: Notepad++软件语言: 简体中文授权方式: 免费软件运行环境: Win 32位/64位软件大小: 3.4MB图片预览: 软件简介:Notepad中文版是一款非常有特色的编辑器,是开源 ...

  3. javascript动画效果之多物体缓冲运动

    这个是通过一个for循环控制的三个li标签,被鼠标触发则会有一个宽度增加和减少的事件 html和css同样写在一起方便察看,这里就是简单的布局,重点在js <!DOCTYPE html> ...

  4. Cash Machine

    Problem Description A Bank plans to install a machine for cash withdrawal. The machine is able to de ...

  5. python基础(三)--列表、元组、字典

    一.列表: 有序序列,支持索引.切片.循环(for,while) 元素可以被修改: 元素可以是任何数据类型(数字,字符串,列表,布尔值...),可以嵌套: ##增 1.append(object)   ...

  6. juniper防护墙接口的NAT和ROUTE模式如何选择问题

    juniper防护墙一般是把trust接口部署为NAT模式,untrust接口部署为route模式.这样当来自内部的数据访问Utrust区域时,会把源地址翻译成untrust接口的地址.从而达到隐藏内 ...

  7. spring AOP 代理机制、执行过程、四种实现方式及示例详解

    1.加载过程 spring首先检测配置文件中的代理配置,然后去加载bean; 如果配置文件中没有配置代理,自然代理不会生效,如果配置了代理,但是代理还没有生效,那么有可能是加载顺序的问题,即在检测到代 ...

  8. windows任务计划程序路径设置

    用任务计划启动程序,特别是脚本,比如我要启动python脚本,其中有一句是这么写的 BasePath = removeLastSlash(os.path.abspath("..\\..\\& ...

  9. Day06 杂乱与4个对象

    1.杂乱 -- 数据库的分页操作 -- 分页使用的是rownum 例1: select rownum,empno,ename from emp ; 结果: 例2: select r,empno,ena ...

  10. hdu 计算机学院大学生程序设计竞赛(2015’11)

    搬砖 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submissi ...