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.

【题意】给出一个罐子,空的时候和满的时候的质量,给出n个已知质量和价值的东西,求装满罐子的最小价值

【思路】完全背包

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=;
int val[N],w[N];
int e,f,n;
int dp[]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&e,&f);
int sum=f-e;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&val[i],&w[i]);
}
memset(dp,inf,sizeof(dp));
dp[]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=sum;j++)
{
if(j>=w[i])
{
dp[j]=min(dp[j],dp[j-w[i]]+val[i]);
}
}
}
if(dp[sum]!=inf)
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[sum]);
else printf("This is impossible.\n");
}
return ;
}

Piggy-Bank_完全背包的更多相关文章

  1. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  2. HDU 3535 AreYouBusy (混合背包)

    题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...

  3. HDU2159 二维完全背包

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. CF2.D 并查集+背包

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  5. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

  6. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  7. POJ1717 Dominoes[背包DP]

    Dominoes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6731   Accepted: 2234 Descript ...

  8. HDU3466 Proud Merchants[背包DP 条件限制]

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  9. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  10. UVA - 11137 Ingenuous Cubrency[背包DP]

    People in Cubeland use cubic coins. Not only the unit of currency iscalled a cube but also the coins ...

随机推荐

  1. hdu 4405Aeroplane chess(概率DP)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  3. IO流--流转换

    import java.io.*; //键盘读入 字节流转换成字符流操作 提高效率 public class io { public static void main(String[] args) t ...

  4. IO流--字节流

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import ...

  5. 关于 iOS 批量打包的总结

      关于 iOS 批量打包的总结 本文作者: 伯乐在线 - Tsui YuenHong .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 如果你曾经试过做多 target 的项目,到了测试人员要 ...

  6. sql里Where条件顺序

    以前的理解: sql语句里where后面的条件是否分先后顺序的 ,比如 A and B and C和 C and B and A 是一样,不像C语言 A && B 与B &&a ...

  7. visual2012 快捷键

    代码格式化:Ctrl+K+D 注释:VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C) 反注释:VS2010是(Ctrl+E,U),VS2012是(Ctrl+K, Ct ...

  8. 使用WBI SAP Adapter 实现IDoc的同步处理(转)

    1. 应用背景 某汽车制造企业(以下称为厂商A)与其仓储系统提供商(以下称为厂商B)需要进行数据交换.汽车厂商A使用SAP系统作ERP管理,所有数据都要进入SAP进行处理,仓储系统提供商使用的是自有的 ...

  9. jsp作用域

    1.page: JSP页面内所有实例的默认作用域都是page,仅限于本页面使用 2.request: 同一次请求所涉及的服务器资源(可能是页面.Servlet等),例如,程序使用<jsp:for ...

  10. 文件浏览器及数码相框 -2.3.2-freetype_arm-1

    交叉编译:tar xjf freetype-2.4.10.tar.bz2 ./configure --host=arm-linuxmakemake DESTDIR=$PWD/tmp install f ...