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 ...
随机推荐
- DOS/VBS - 用 bat 批处理 实现自动telnet
一.VBS法 1. 建立一个tel.vbs脚本 '建立Shell对象 set sh=WScript.CreateObject("WScript.Shell") WScript.Sl ...
- linux 安装与启动nginx
linux系统为Centos 64位 一.去http://nginx.org/download/上下载相应的版本下载nginx-1.8.0.tar.gz(注:还有更高版本的). 二.解压 tar -z ...
- 【卷一】正则四 |> 练习
参考:<Python核心编程(3rd)>—P39 1-1 识别后续的字符串: "bat", "bit", "but" &quo ...
- iOS动画编程
IOS中的动画总结来说有五种:UIView<block>,CAAnimation<CABasicAnimation,CATransition,CAKeyframeAnimation& ...
- 菲菲更名宝贝 得意非凡版 v1.9 免费绿色版
软件名称: 菲菲更名宝贝 得意非凡版软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: 12.5MB图片预览: 软件简介:菲菲更名宝贝 ...
- A - 小Y上学记——修学分
A - 小Y上学记——修学分 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...
- I Think I Need a Houseboat
I Think I Need a Houseboat Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java ...
- 安装cocoapods遇到的一些问题
其实可以直接到https://github.com/CocoaPods/Specs上把所需的文件夹下载下来 解压后命名为master放在本地的 ~/.cocoapods/repos 下面就行 但是使 ...
- 《JavaScript高级程序设计》读书笔记 ---语句
do-while语句do-while 语句是一种后测试循环语句,即只有在循环体中的代码执行之后,才会测试出口条件.换句话说,在对条件表达式求值之前,循环体内的代码至少会被执行一次.以下是do-whil ...
- JQuery_图片未加载!
JQuery_图片未加载! <html> <head> <script type="text/javascript" src="/jquer ...