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!

InputThe 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. 
OutputPrint 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<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
using namespace std;
#define MAXN 508
#define INF 0x3f3f3f3f
/*
F - Piggy-Bank 典型的完全背包问题,给定容量和不同硬币(价值,重量),求最小价值
*/
int value[MAXN],cost[MAXN],dp[];
int main()
{
int t,e,f,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&e,&f);
memset(dp,INF,sizeof(dp));
int w = f-e;
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%d%d",&value[i],&cost[i]);
}
dp[] = ;
for(int i=;i<m;i++)
for(int v=cost[i];v<=w;v++)
dp[v] = min(dp[v],dp[v-cost[i]]+value[i]);
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 ;
}

F - Piggy-Bank 完全背包问题的更多相关文章

  1. 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 F - Piggy-Bank 【完全背包问题】

    https://vjudge.net/contest/68966#problem/F http://blog.csdn.net/libin56842/article/details/9048173 # ...

  2. ACM Piggy Bank

    Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...

  3. 购物单 && 动态规划 && 背包问题

    题目叙述的言语倒是蛮多的: 王强今天很开心,公司发给N元的年终奖.王强决定把年终奖用于购物,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子: 主件 附件 电脑 ...

  4. silicon labs 代理商

      http://www.silabs.com/buysample/pages/contact-sales.aspx?SearchLocation=China       Silicon Labs A ...

  5. Elasticsearch实践(二):搜索

    本文以 Elasticsearch 6.2.4为例. 经过前面的基础入门,我们对ES的基本操作也会了.现在来学习ES最强大的部分:全文检索. 准备工作 批量导入数据 先需要准备点数据,然后导入: wg ...

  6. Android开发训练之第五章第五节——Resolving Cloud Save Conflicts

    Resolving Cloud Save Conflicts IN THIS DOCUMENT Get Notified of Conflicts Handle the Simple Cases De ...

  7. 直接抱过来dd大牛的《背包九讲》来做笔记

    P01: 01背包问题 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本思路 这是最 ...

  8. 2016summer 训练第一场

    A.http://acm.hdu.edu.cn/showproblem.php?pid=5538 求表面积,只需要将所有的1*1的小块扫描一遍.将每一个块与他相邻四周进行比较,如果该快高度大,则将该快 ...

  9. luogu P3420 [POI2005]SKA-Piggy Banks

    题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can either be opened with its correspon ...

  10. 洛谷 P3420 [POI2005]SKA-Piggy Banks

    P3420 [POI2005]SKA-Piggy Banks 题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can eith ...

随机推荐

  1. AGC16E Poor Turkeys

    输入样例: 10 10 8 9 2 8 4 6 4 9 7 8 2 8 1 8 3 4 3 4 2 7 输出样例#6: 5 话说这题虽然不是很OI但是确实挺锻炼思维的 一开始以为是用并查集之类的东西维 ...

  2. 在dataGridView空间中添加数据

    //查询信息sql语句 string sql = "select studentName,addres from student"; SqlDataAdapter adapter ...

  3. Java虚拟机内存详解

    概述 Java虚拟机会自动管理内存,不容易出现内存泄漏和内存溢出问题.Java虚拟机会在执行过程中将管理的内存分为若干个不同的数据区域. 运行时数据区域 在jdk1.8之前的版本与1.8版本略有不同, ...

  4. Android OKHttp网络框架

    好久没逛简书了.这周公司的项目也已经愉快的迭代了新版本,对于之前一直存留的东西一直没怎么梳理,今天想说说这两年特别火的网络框架.okhttp我想大部分Android开发者都不陌生,因为它的到来.是我们 ...

  5. 面试中的一些小问题之html5和html4的区别?

    HTML5建立的一些新规则: 新特性应该基于HTML.CSS.DOM.JavaScript: 减少对外部插件的需求,如flash将会用video标签和audio标签代替: 更加优秀的错误处理: 更多取 ...

  6. Memcached通信协议

    英文水平很烂,做梦都想着能把英语学习,可以使用一口流利的英文和洋鬼子交流,顺便忽悠下自己的同胞.没有地方学习英语,看还可以,网上有很多关于计算机的英文文献,写还行,说就完全不可能了.在以后的工作中慢慢 ...

  7. js让页面逐渐变透明,直到消失

    在gitHub闲逛的时候,无意间发现了些好玩的代码,一个歪果仁写的,这里我做了些修改和优化,gitHub链接已经忘了,是一段恶搞的js代码,假如你的顾客或者老板拖欠你工资,那你就可以让项目页面在浏览器 ...

  8. RocketMQ学习笔记(15)----RocketMQ的消息模式

    在前面学习ActiveMQ时,看到ActiveMQ可以是队列消息模式,也可以是订阅发布模式. 同样,在RocketMQ中,也存在两种消息模式,即是集群消费模式和广播消费模式. 1. 集群消费模式 跟A ...

  9. day17-常用模块II (hashlib、logging)

    目录 hashlib模块 撞库破解hash算法加密 logging模块 配置日志文件 hashlib模块 一般用于明文加密,其实就是一个自定义的字符编码表.原来0和1转换成字符,而现在的是字符转成另一 ...

  10. CAD把控件图形变成二进制流输出(com接口VB语言)

    主要用到函数说明: _DMxDrawX::WriteBinStream 把控件图形变成二进制流输出,详细说明如下: 参数 说明 VARIANT* pVarBinArray 输出二进制数组.是个byte ...