Piggy-Bank
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10830   Accepted: 5275

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.

Source

开始错了几次,没留意到怎么判读刚好装够,直到加了个if判读(已经处理过的,已有的 可以继续装,否则不能)

 //176K    94MS    C++    650B
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define M -0x7fffffff
int dp[];
int p[],w[];
int main(void)
{
int cas,e,f,n;
scanf("%d",&cas);
while(cas--){
scanf("%d%d",&e,&f);
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&p[i],&w[i]);
} for(int i=;i<=f;i++){
dp[i] = M;
}
dp[] = ;
for(int i=;i<n;i++){
for(int j=w[i];j<=f-e;j++){
if(dp[j-w[i]] != M)
dp[j] = max(dp[j], dp[j-w[i]] - p[i]);
}
}
if(dp[f-e]==M){
puts("This is impossible.");
}else{
printf("The minimum amount of money in the piggy-bank is %d.\n", -dp[f-e]);
}
}
return ;
}

poj 1384 Piggy-Bank(完全背包)的更多相关文章

  1. poj 1384 Piggy-Bank(全然背包)

    http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...

  2. POJ 1384 Piggy-Bank (完全背包)

    Piggy-Bank 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/F Description Before ACM can d ...

  3. POJ 1384 Piggy-Bank【完全背包】+【恰好完全装满】(可达性DP)

    题目链接:https://vjudge.net/contest/217847#problem/A 题目大意:   现在有n种硬币,每种硬币有特定的重量cost[i] 克和它对应的价值val[i]. 每 ...

  4. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  5. POJ 3181 Dollar Dayz(全然背包+简单高精度加法)

    POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...

  6. POJ 3211 Washing Clothes(01背包)

    POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...

  7. POJ 1384 POJ 1384 Piggy-Bank(全然背包)

    链接:http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...

  8. POJ 1384 Piggy-Bank (ZOJ 2014 Piggy-Bank) 完全背包

    POJ :http://poj.org/problem?id=1384 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode ...

  9. POJ 1384 Piggy-Bank 背包DP

    所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...

随机推荐

  1. shell中判读文件存在的方法

    单个文件较简单 if [[ -f filename ]]; then echo exist fi 文件存在并且有内容 if [[ -s filename ]]; then echo exist fi ...

  2. ant批量执行Jmeter脚本

    JDK,Jmeter默认已经装了 ANT下载:http://ant.apache.org/bindownload.cgi ant环境变量需要配置 ant_home,你解压之后的地址 然后PATH环境变 ...

  3. nodejs在同一台服务器上部署并同时运行两个或以上服务端时,一个服务用户登录后会挤掉另一个用户的问题

    问题描述:一台服务器,部署了两个或以上不同的Web服务,服务A的用户在登陆后,服务B的用户也登陆,此时服务A的用户在点击页面时,会返回登陆页面. 问题根源:浏览器保存的session相同,即cooki ...

  4. 再谈vertical-align与line-height

    每次遇到多个inline-block元素排列的怪异垂直位置的问题的时候都可以通过经验,设置vertical-align来解决,没深入研究过,现在需要分析总结下这个问题. 问题引出 有小鲜肉新做了个页面 ...

  5. JS传递对象数组为参数给后端,后端获取

    前端JS代码: var conditons = []; var test1 = new Object(); test1.name="1"; test1.id="2&quo ...

  6. JS不支持正则中的负向零宽断言

    今天在项目中用到了正则表达式,并且需要用负向零宽断言 (?<=exp) 进行筛选,结果运行时报 Invalid group 错,一开始以为是自己很久没用表达式写错了,查阅了一下正则语法后发现并没 ...

  7. 【MySQL】编译安装

    安装所需环境: yum install cmake make glibc gcc gcc-c++ libstdc++* sysstat lrzsz libtool libxml* libtool-lt ...

  8. wc命令

    wc命令的功能为统计指定文件中的字节数.字数.行数, 并将统计结果显示输出. 语法:wc [选项] 文件- 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同 ...

  9. 排序算法(JAVA)

    import java.util.Random;      /**  * 排序测试类  *   * 排序算法的分类如下:  * 1.插入排序(直接插入排序.折半插入排序.希尔排序):  * 2.交换排 ...

  10. android 存储目录

    之前一直不知道 sdcard/Android目录什么作用,我做的项目里面缓存数据到本地一般都是在sdcard上面建一个文件,然后把数据放在这个文件夹下面的子文件夹下.下面介绍一种更好的解决方法. 应用 ...