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 ...
随机推荐
- poj 1411 Calling Extraterrestrial Intelligence Again
题意:给你数m,a,b,假设有数p,q,满足p*q<=m同时a/b<=p/q<=1,求当p*q最大的p和q的值 方法:暴力枚举 -_-|| and 优化范围 我们可以注意到在某一个m ...
- CCleaner(著名清理软件) 5.21.5700 中文免费版(著名清理软件) 5.21.5700 中文免费版
软件名称: CCleaner(著名清理软件) 5.21.5700 中文免费版著名清理软件(CCleaner)软件语言: 多国语言授权方式: 免费软件运行环境: Win 32位/64位软件大小: 5.6 ...
- JSONModel解析数据成Model
转自:http://blog.csdn.net/smking/article/details/40432287 JSONModel, Mantle 这两个开源库都是用来进行封装JSON->Mod ...
- php 类中设置成员属性方法
class FileUpload { private $path = "./uploads"; //上传文件保存的路径 private $allowtype = array('jp ...
- Python基础篇-day4
本节目录: 1.字符编码 2.函数 2.1参数 2.2变量 2.3返回值 2.4递归 2.5 编程范式 2.6 高阶函数 *************************************** ...
- wordpress建站过程2——结构
开始wordpress之前,我们需要了解,wordpress的结构和调用方式. 当一个wordpress开始之后,他会读取[当前主题]的index.php.所以一旦主题切换了,它读的就是其他主题的in ...
- javaWEB总结(4):ServletContext对象方法
前言:之前每次学到ServletContext对象都会抗拒,跳着学,后面才发现有很多不懂的原理都在这个对象里面,后悔莫及,所以今天特地把他单放在一篇文章里,算是对他的忏悔. 1.什么是ServletC ...
- eclipse代码提示优化
用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示 Window->Preferences ...
- cmd 下登陆ftp及相关操作
cmd 下登陆ftp及相关操作 2011-08-09 20:34:28| 分类: 小技巧|字号 订阅 一.举例 假设FTP地址为“ 61.129.83.39”(大家试验的时候不要以这个FTP去试,应 ...
- maven 代理
切换到{maven.home}\conf (ie: maven 安装目录) 拷贝settings.xml到自己的.m2目录 编辑.m2/settings.xml的代理部分: <proxy> ...