练习三 Problem R

原创 2016年05月31日 20:25:58

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. <br><br>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! <br>

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. <br>

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.". <br>

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.

题意:称出背包装物品前后的重量,求最小价值。


思路:将dp的值初始化为无穷大,就有第i件物品可选可不选,根据这个可以计算出所有情况,最后求最优值。


感想:完全背包问题,注意j的值为从小到大。


代码:


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

ACM程序设计题目 Problem R-18

//就是判断一个数是不是半素数。半素数就是只有1和其他两个素数因子,题目意思很简单,但是容易超时,这个提我提交了11次,都是超时下面是通过的代码。 //思路就是先把素数找出来,,存到数组里,然后将他...

Codeforces R374 Div2 - Problem C - Journey

题目:http://codeforces.com/contest/721/problem/C题目大意:给定n个点,m条边,时间t,求 从起点1 到终点n 在时间t内 最多走多少个点。#include ...

课程练习三-1003-problem C

Problem Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is v...

课程练习三-1009-problem I

Problem Description Queues and Priority Queues are data structures which are known to most computer...

课程练习三-1004-problem D

Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. T...

课程练习三-1007-problem G

Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身...

课程练习三-1008-problem H

Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了!可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又...

课程练习一Problem B 木棒

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The st...

多校联合练习赛1 problem1009 I-number 信心题啊。。。

I-number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:练习三 Problem R
举报原因:
原因补充:

(最多只允许输入30个字)