Piggy-Bank

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

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.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
int n,W;
int v[MAXN],w[MAXN];
int dp[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int e,f;
scanf("%d%d",&e,&f);
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&v[i],&w[i]);
}
W=f-e;
for(int i=;i<MAXN;i++)
{
dp[i]=INF;//大前提:背包装满.因为求获得的价值最小,所以需要将dp初始化正无穷.若求价值最大,则将dp初始化为负无穷
}
dp[]=;//背包需要装满,则将dp[0]初始化为0
for(int i=;i<n;i++)
{
for(int j=w[i];j<=W;j++) //完全背包:逆序
dp[j]=min(dp[j-w[i]]+v[i],dp[j]);//求最小的价值
}
if(dp[W]==INF)
{
printf("This is impossible.\n");
}
else
{
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]);
}
} return ;
}
 

HDU1114(完全背包装满问题)的更多相关文章

  1. HDU-1114 完全背包+恰好装满问题

    B - Piggy-Bank Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  2. hdu1114 完全背包

    题意:给出钱罐的重量,然后是每种钱的价值和重量,问钱罐里最少可能有多少钱. 完全背包. 代码: #include<iostream> #include<cstdio> #inc ...

  3. 01背包模板、全然背包 and 多重背包(模板)

    转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...

  4. hdu3496 二维01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M ...

  5. Partial Tree---hdu5534(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意:有n个节点,让这n个节点形成一棵树,这棵树的种类有很多种,现在告诉n-1个f[i],表示度 ...

  6. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

  7. Piggy-Bank(完全背包)

    /* http://acm.hdu.edu.cn/showproblem.php?pid=1114 完全背包问题 再判断背包能否装满 题意: 给出存空钱罐的重量以及装满时的重量 给出每种硬币的面值以及 ...

  8. HDU 2546 饭卡(0-1背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2546 题意: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金 ...

  9. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

随机推荐

  1. SocketAsyncEventArgs里的AcceptSocket能独立存在吗?

    独立存在是什么意思? 先来看一个例子.我们知道一个Socket对象(我们叫他ListenScoket)可以调用AcceptAsync并接受一个SocketAsyncEventArgs对象,如果操作成功 ...

  2. 我的Android进阶之旅------>FastJson的简介

    在最近的工作中,在客户端和服务器通信中,需要采用JSON的方式进行数据传输.简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON字符串,出错率就非常大了.并且工作效率也 ...

  3. JVM类加载器

    系统中的类加载器 1.BootStrap ClassLoader a.启动ClassLoader b.加载rt.jar 2.Extension ClassLoader a.扩展ClassLoader ...

  4. 跟我一起用Symfony写一个博客网站;

    我的微信公众号感兴趣的话可以扫一下, 或者加微信号   whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...

  5. java 从零开始 第三天

    2015年5月2日 51刚过一天,电脑坏了.不开心,就没有更新了 Java中的类型转换 自动类型 在 Java 程序中,不同的基本数据类型的数据之间经常需要进行相互转换.例如: , 代码中 int 型 ...

  6. SAP内表转XML文件

    今天有个兄弟问如何实现以XML的方式输出内表的内容,这个问题我以前好像没有写过.倒不是不会写,而是写的方法太多了,有极其简单的,也有很复杂的,而且网上资料也很多. 找到以前写的一个程序,稍微修改了一下 ...

  7. 【SHARE】WEB前端学习资料

    参考资料:https://github.com/karlhorky/learn-to-program 学习网站:http://www.codecademy.com/learn https://www. ...

  8. 基于CocoaPods的iOS项目模块化实践

    什么是CocoaPods? CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has ove ...

  9. UIImageView 获取图片的 宽 高

    该文章纯属这两天开发的经验之谈 并且也是平常没注意 这回发现的一个小方法 并且很实用 在开发中 提高了很大的效率 更加符合高保真的要求 通常 美术 切的一些图片 需要 :1还原的 现在 我们一般支持i ...

  10. Java 9 逆天的十大新特性

    在介绍java9之前,我们先来看看java成立到现在的所有版本. 1990年初,最初被命名为Oak: 1995年5月23日,Java语言诞生: 1996年1月,第一个JDK-JDK1.0诞生: 199 ...