Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26573    Accepted Submission(s): 13459

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.
 

题目大意:

就是有一个存钱罐,比不知道里面有多少钱,但是我们知道(就是 输入)T组测试数据 E 空的存钱罐的质量, F 存钱罐装满钱的重量,

接着输入 N 里面硬币的种类,

下面N行 P 硬币的价值, W 硬币的质量 你的任务是求出来存钱罐里面最少多少钱

样例1:

10 110

2

1 1

30 50

里面的硬币是 2个价值30,重量为50的硬币的时候,最少,故宗价值最少60.

题目注意要点:

1.完全背包问题,但是求的是最小值,不是最大值!所以初始化的时候做下改变:

求最大价值:要求恰好装满背包,那么在初始化时除了dp[0]为0其它dp[1..V]均设为-∞

求最小价值:要求恰好装满背包,那么在初始化时除了dp[0]为0其它dp[1..V]均设为∞

2.题目核心算法:

   for(int i=;i<=n;i++)
{
for(int j=w[i];j<=vol;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+value[i]);
}
}

相比较01背包而言,这个是顺序的,01背包是逆序的。至于为什么,请参考博文:

http://www.cppblog.com/tanky-woo/archive/2010/07/31/121803.html

或者我的:http://www.cnblogs.com/William-xh/p/7327734.html

3.注意输出的最后有句号,我就是因为没有句号被WA了n次。

最后附上代码:

#include <iostream>
#include<math.h>
#include <iomanip>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<sstream>
#include<string.h>
#include<stdio.h>
using namespace std; int main()
{
int t;
cin>>t;
int empty,full,vol;//空的 和 满的 实际的
int n;//表示各种硬币
int value[],w[],dp[];
while(t--)
{
cin>>empty>>full;
cin>>n;
for(int ii=;ii<=n;ii++)
{
cin>>value[ii];
cin>>w[ii];
}
vol=full-empty; for(int iii=;iii<=vol;iii++)
{
dp[iii]=;
}
dp[]=;//除了dp 0 以外 其它的全部都为无穷大 for(int i=;i<=n;i++)
{
for(int j=w[i];j<=vol;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+value[i]);
}
} if(dp[vol]<)
{
cout<<"The minimum amount of money in the piggy-bank is "<<dp[vol]<<"."<<endl;
}
else
{
cout<<"This is impossible."<<endl;
} }
return ;
}

杭电 1114 Piggy-Bank 完全背包问题的更多相关文章

  1. Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)

    Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone C ...

  2. 杭电 1114 Piggy-Bank【完全背包】

    解题思路,首先很容易想到方程f[v]=min(f[v],f[v-w[i]+p[i]),因为是要求当包装满的时候(因为题目中给出的是包的质量是一定的),包里面装的钱最少,所以将f[]初始化成一个很大的数 ...

  3. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  4. 杭电dp题集,附链接还有解题报告!!!!!

    Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...

  5. 杭电ACM题单

    杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...

  6. 杭电acm习题分类

    专注于C语言编程 C Programming Practice Problems (Programming Challenges) 杭电ACM题目分类 基础题:1000.1001.1004.1005. ...

  7. acm入门 杭电1001题 有关溢出的考虑

    最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...

  8. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  9. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

随机推荐

  1. 一个实例 ---灵活使用jquery选择器实现input一个key,多个value 。 用ajax传递对象到后台控制器

    标题可能不是很清晰,我们看实例: 简单来说就是需要实现sku的功能...一件商品可以有多个属性, 一个属性可以有多个值  . 最后以json格式存到数据库 难点一: 如何实现input输入框的弹性使用 ...

  2. 安装ipython[win/linux]

    首先以win7  64位系统, python2.7.9为例,linux见底部 1.下载材料http://files.cnblogs.com/files/smileyes/ipython-win64.z ...

  3. LeetCodr 43 字符串相乘

    思路 用一个数组记录乘积的结果,最后处理进位. 代码 class Solution { public: string multiply(string num1, string num2) { if(n ...

  4. 自定义虚拟机MAC地址 | 它与 VMware 预留的 MAC 冲突 解法

    https://blog.csdn.net/wangrui1573/article/details/82056020 问题:我想给VMware ESXi上的一台虚拟机分配一个静态的MAC地址.然而当我 ...

  5. 2.1 【配置环境】 JDK + eclipse + selenium

    1.jdk以及eclipse的具体安装详见  http://www.cnblogs.com/ericazy/p/6082194.html 安装1.7 jdk即可 2.selenium 旧版本安装: s ...

  6. 【Python】使用socketserver建立一个异步TCP服务器

    概述 这篇文章是讲解如何使用socketserver建立一个异步TCP服务器,其中Python版本为3.5.1. socketserver主要的类 socketserver模块中的类主要有以下几个:1 ...

  7. JavaScript 推箱子游戏

    推箱子游戏的 逻辑非常简单,但是如果不动手的话,还是不太清楚.我在这里讲一下自己的思路. 制作推箱子,首先要有自己的设计素材.如下我也是网上找的素材 第二步,理清游戏的规则. 游戏规则: 1.小人将箱 ...

  8. yum源文件

    yum源文件 2017年11月22日 11:59:36 cakincqm 阅读数 1052更多 分类专栏: Linux   版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附 ...

  9. INCA二次开发-INCACOM

    1.INCA介绍 INCA是常用的汽车ECU测试和标定的,广泛应用于动力总成等领域.INCA提供了丰富的接口,供用户自动化.定制化.本公众号通过几篇文章,介绍下一些二次开发的方法,本篇介绍INCA-C ...

  10. 安装pyhanlp

    安装pyhanlp pyhanlp是java写的,外层封装了python. 对于新手,在使用的时候稍有难度. 1. 下载源码 https://github.com/hankcs/pyhanlp git ...