Problem Q

Time Limit : 2000/1000ms (Java/Other)   Memory Limit :65536/32768K (Java/Other)

Total Submission(s) : 2   Accepted Submission(s) : 1

Problem Description

Before ACM can do anything, abudget must be prepared and the necessary financial support obtained. The mainincome for this action comes from Irreversibly Bound Money (IBM). The ideabehind is simple. Whenever some ACM member has any small money, he takes allthe coins and throws them into a piggy-bank. You know that this process isirreversible, the coins cannot be removed without breaking the pig. After asufficiently long time, there should be enough cash in the piggy-bank to payeverything that needs to be paid. <br><br>But there is a big problemwith piggy-banks. It is not possible to determine how much money is inside. Sowe might break the pig into pieces only to find out that there is not enoughmoney. Clearly, we want to avoid this unpleasant situation. The onlypossibility is to weigh the piggy-bank and try to guess how many coins areinside. Assume that we are able to determine the weight of the pig exactly andthat we know the weights of all coins of a given currency. Then there is someminimum amount of money in the piggy-bank that we can guarantee. Your task isto find out this worst case and determine the minimum amount of cash inside thepiggy-bank. We need your help. No more prematurely broken pigs! <br>

Input

The input consists of T testcases. 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. Theyindicate the weight of an empty pig and of the pig filled with coins. Bothweights 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 aninteger number N (1 <= N <= 500) that gives the number of various coinsused in the given currency. Following this are exactly N lines, each specifyingone 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, Wis it's weight in grams. <br>

Output

Print exactly one line ofoutput for each test case. The line must contain the sentence "The minimumamount of money in the piggy-bank is X." where X is the minimum amount ofmoney that can be achieved using coins with the given total weight. If theweight 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 inthe piggy-bank is 60.

The minimum amount of money inthe piggy-bank is 100.

This is impossible.

``````#include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int a[502],v[502],f[10002];
int n,vp,vm,V;;
cin>>vp>>vm;
V=vm-vp;
cin>>n;
int i,j;
for(i=0;i<n;i++)
cin>>a[i]>>v[i];
for(i=0;i<=V;i++)
f[i]=99999999;//背包要求装满,恰好这题也是求最小值
f[0]=0;//重要的初始化
for(i=0;i<n;i++)
for(j=v[i];j<=V;j++)
{
f[j]=min(f[j-v[i]]+a[i],f[j]);
}

if(f[V]!=99999999)
cout<<"The minimum amount of money in the piggy-bank is "<<f[V]<<"."<<endl;
else
cout<<"This is impossible."<<endl;

}
return 0;
}``````