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. Codeforces Round #567 (Div. 2) A.Chunga-Changa

    原文链接:传送 #include"algorithm" #include"iostream" #include"cmath" using n ...

  2. 数据库程序接口——JDBC——API解读第三篇——处理结果集的核心对象

    核心对象 处理结果集的核心对象有ResultSet和RowSet.其中ResultSet指定关系型数据库的结果集,RowSet更为抽象,凡是由行列组成的数据都可以. ResultSet ResultS ...

  3. Ubuntu18.04安装Vim-plug与YCM

    由于个人强迫症的原因,之前的ycm是通过vundle来管理的,这次想更新一下ycm发现问题太多,于是就重新装了个Ubuntu虚拟机,用vim-plug来进行管理ycm及其他插件. 首先要换一下Ubun ...

  4. linux 配置php环境变量

    vim /etc/profile //加上 export PATH=$PATH:/usr/local/php/bin 保存退出 source /etc/profile php -v 注:该配置对所有用 ...

  5. [Jenkins] Jenkins的启动停止并修改默认端口

    在Win系统下面,经常使用Jenkins今天自动化测试工作,但是在搭建的时候还是有些坑 1.选择性安装: 一般会选择windows,会下载一个压缩包,然后step by step就可以安装成功,这个方 ...

  6. Python Django中一些少用却很实用的orm查询方法

    一.使用Q对象进行限制条件之间 "或" 连接查询 from django.db.models import Q from django.contrib.auth.models im ...

  7. css与js基础

    CSS样式 1 宽高设置 块元素可使用 wid 1字体 font-family :  文本类型 font-size     字体大小 font-style 字体样式 斜体italic   正常norm ...

  8. C:作用域

    作用域 C语言变量的作用域分为: 代码块作用域(代码块是{}之间的一段代码) 函数作用域 文件作用域 局部变量 局部变量也叫auto自动变量(auto可写可不写),一般情况下代码块{}内部定义的变量都 ...

  9. 用xshell连接VMware虚拟机中安装的Centos7系统

    首先要保证你安装的Centos7系统的网路适配器使用的桥接模式,这个模式允许你安装再虚拟机中的Centos系统有一个自己的ip地址. 然后再虚拟机中登录你的Centos系统,用ip addr命令查看你 ...

  10. Laravel Vuejs 实战:开发知乎 (2)用户登录

    1.安装一个给用户提示的扩展包: 二选一: https://github.com/laracasts/flash [我选的这个]https://github.com/oanhnn/laravel-fl ...