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. C基础-对malloc的使用与理解

    一.malloc函数分析 1.函数原型 void * malloc(size_t  size); 2.Function(功能) Allocates a block of size bytes of m ...

  2. [洛谷3930]SAC E#1 - 一道大水题 Knight

    Description 他们经常在一起玩一个游戏,不,不是星际争霸,是国际象棋.毒奶色觉得F91是一只鸡.他在一个n×n的棋盘上用黑色的城堡(车).骑士(马).主教(象).皇后(副).国王(帅).士兵 ...

  3. 数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换

    参考来源:http://blog.csdn.net/qq924862077/article/details/54286976?utm_source=gold_browser_extension Req ...

  4. mysqlbinlog(日志管理工具)

    mysqlbinlog用于处理二进制的日志文件,如果想要查看这些日志文件的文本内容,就需要使用mysqlbinlog工具. 1.mysqlbinlog命令的语法 shell > mysqlbin ...

  5. 创建maven项目遇到的问题

    1.新建完成的maven项目,缺少src/main/java 解决方案:把项目中的jre换成eclipse中默认的jre. 另外还可以参考:解决Eclipse建立Maven项目后无法建立src/mai ...

  6. mybatis使用中类属性名和数据库表字段名问题

    起初我以为上述二者必须一致,后来发现也是可以像Hibernate那样在xml文件中进行映射的. <mapper namespace="com.tenghu.mybatis.model. ...

  7. delphi 字符串处理中的怪异现象与处理

    1, 怪异现象:字符串相加操作不正常! 以上代码,明显输出字符串应含有后缀“.jpg”,但实际输出却不含后缀(如下),字符串加法操作似乎不起作用了! 采用showMessage进行输出,看看结果如何? ...

  8. thinkphp信息修改和分页

    关联两个数据表,在Model里建立StuModel.class.php: <?php //Belongs_to 关联表示当前模型从属于另外一个父对象 namespace Admin\Model; ...

  9. 左耳听风 ARTS Week 002

    要求:1.每周至少做一个 leetcode 的算法题 2.阅读并点评至少一篇英文技术文章 3.学习至少一个技术技巧 4.分享一篇有观点和思考的技术文章 1.每周至少做一个 leetcode 的算法题 ...

  10. MyEclipse 快捷键方法

    MyEclipse企业级工作平台(My Eclipse Enterprise Workbench,简称MyEclipse)是对EclipseIDE的扩展,利用它可以在数据库和J2EE的开发.发布,以及 ...