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. centos7 源码编译安装nginx教程 nginx安装脚本 lua-nginx-module

    安装nginx需要pcre zlib openssl的库,下文都是在官网直接下载用作编译安装 该nginx安装教程,有安装maxmind IP 库 该nginx安装教程有安装lua-nginx-mod ...

  2. 转载:Laplace 变换

    转自: https://www.zhihu.com/question/22085329 https://wenku.baidu.com/view/691d4629640e52ea551810a6f52 ...

  3. centos7也支持service命令启动服务吗,对于centos7 中的systemctl和旧的service命令的区别和联系

    一.centos7也支持service命令启动服务吗 CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服 ...

  4. 560. 和为K的子数组

    Q: A: 1.暴力找所有可能的子数组,n^2个子数组,最长长度n,则n ^3. 2.n^2解法 从1~n-1各起点开始,一直找到结尾,n^2 class Solution { public: int ...

  5. Git 常用命令总结,掌握这些,轻松驾驭版本管理

    原创 最近公司的代码管理工具要从SVN转到Git上,因此虽然之前用过Git,但是都是一些简单的推送提交,因此还是有必要进行一些系统的学习,这里做一下笔记,以备后询,且不定期更新. 关于SVN和Git的 ...

  6. 大数据的特征(4V+1O)

    数据量大(Volume):第一个特征是数据量大,包括采集.存储和计算的量都非常大.大数据的起始计量单位至少是P(1000个T).E(100万个T)或Z(10亿个T). 类型繁多(Variety):第二 ...

  7. Springboot项目搭建(3)-shiro登录

    shiro简述+实现简单登录:https://www.jianshu.com/p/7f724bec3dc3

  8. 503,display:none;与visibility:hidden;的区别

    联系:他们都能让元素不可见 区别: display:none:会让元素从渲染树中消失,渲染的时候不占据任何空间: visibility:hidden:不会让元素从渲染树中消失,渲染时袁旭继续占据空间, ...

  9. Codeforces Round #618 (Div. 1)B(几何,观察规律)

    观察猜测这个图形是中心对称图形是则YES,否则NO #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace ...

  10. 一些 乱码 GPU的问题

    # # s = '网站地图' 原始 # s1 = s.encode('utf-8') # print(s1.decode('gbk')) #res 缃戠珯鍦板浘 # s = '缃戠珯鍦板浘' 原始 # ...