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 ...
随机推荐
- Leetcode016 3Sum Closest
public class S016 { //借鉴S015的思想,只是稍微有点慢 public int threeSumClosest(int[] nums, int target) { Arrays. ...
- WTL版本ACEdit控件,改写自MFC版,附带源码
自动完成是个很酷也很实用的功能,比如在浏览器地址栏输入几个字母,相关的记录就会在下拉框中陈列出来. 最近在做公司产品UI部分的改善,原版本是MFC做的,我决定用WTL,于是就遇到自动完成控件的问题.遍 ...
- uilabel 复制
//添加一个长按响应方法 - (void)addLongPressGestureRecognizer { UILongPressGestureRecognizer * longPress = [[UI ...
- CentOS安装配置JDK-7(.rpm)
声明:本文转自:http://www.cnblogs.com/zhoulf/archive/2013/02/04/2891608.html 安装说明 系统环境:centos-6.3安装方式:rpm安装 ...
- Linux下安装MySQL-5.7
写在前面:此博客是针对MySQL5.7安装教程,其他版本可能略有不同,仅供参考. 第一步:下载mysql 在Linux终端使用wget命令下载网络资源: wget http://mirrors.soh ...
- C#连接ACCESS的一个问题
C# 连接ACCESS数据库有时候报 "Microsoft.Jet.Oledb.4.0"没有注册,其实,并不是真的没注册,可能是下面的原因 在菜单 “项目”的最下面 工程属性 菜单 ...
- C#中Bitmap类 对图像の操作 可检测图片完整性
try { Bitmap bm = new Bitmap(pics[ip]); BitmapToBytes(bm).Reverse().Take(2); } catch (Exception ex) ...
- 第十五节,基本数据类型,元组tuple
元组和列表的区别 元组和列表几乎是一样的 不一样的地方就是元组创建后元组的元素不可以修改,比如(添加,拓展,移除等修改功能,但是元组里的元素的元素是可以修改的) 基本操作: 索引 切片 循环 长度 包 ...
- bash color
紫色:300A24 黄色:C4A000 Tango 紫色: 200213
- JVM调优总结-调优方法
JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪 JProfiler ...