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. schroeder reverb matlab实现

    原理参考:Natural sounding artificial reverberation combFilter.m: function output = combFilter(delay, gai ...

  2. php商城数据库的设计 之无限分类

    商品分类,使用无限分类 即: -------如何创建数据表 pid---父级分类id,如果是顶级分类则为0 path---1,用户分类的排序 . 排序示例: 实现逻辑:获取type表的所有分类,ord ...

  3. web开发中 前端模板->JavaScript->Controller->JavaScript相应 的交互方式

    首先画张图了解当下流行的phpweb 数据交互套路: 1,模板与JavaScript的交互 给HTML标签赋予onlick事件,点击后触发js方法,jQuery收集页面信息,分析信息. 2,js与co ...

  4. sublime 神一样的插件

    专属配置 // 主题 "theme": "Boxy Tomorrow.sublime-theme", "theme_grid_border_size_ ...

  5. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  6. Java+Selenium+Testng自动化测试学习(二)

    Java+Selenium+TestNG自动化测试框架整合 1.简化代码 封装一个定位元素的类,类型为ElementLocation package com.test; import org.open ...

  7. 6_16 单词(UVa10129)<欧拉回路>

    考古学家有时候遇到一些神秘的门,这些门需要解开特定的谜题才能打开.因为没有其他方法可以打开门,这谜题对我们来说非常重要.在门上有许多磁盘,每个盘子上有一个英文单字在上面.这些盘子必须被安排,使得盘子上 ...

  8. 计算几何-HPI

    This article is made by Jason-Cow.Welcome to reprint.But please post the article's address.   在线笛卡尔坐 ...

  9. Java - 闭包

    概述 简单介绍 闭包 1. 聚合关系 概述 常见的 类间关系 场景 类 A 主要类 持有 类B 的实例 有点行为, 需要 类 B 的介入 类 B 有自己的行为 A 会在某些时候调用 B 的行为 代码示 ...

  10. const和defin区别

    (1)类型的安全性检查:const常量有数据类型,而define定义宏常量没有数据类型.则编译器可以对前者进行类型安全检查,而对后者只进行字符替换,没有类型安全检查(字符替换时可能会产生意料不到的错误 ...