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 shoshould be enough cash in the piggy-bank
To payeverything that needs to be paid.
But there is a big problem with piggy-banks. it is not possible to determinehow much money is inside. so we might break the pig into pieces only to findout that there is not enough money. clearly, we want to avoid this unpleasantsituation. the only possibility
Is to weigh the piggy-bank and try to guess howmany coins are inside. assume that we are able to determine the weight of thepig exactly and that we know the weights of all coins of a given currency. thenthere 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 ofcash inside the piggy-Bank. We need your help. No more prematurely broken pig!
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.
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
The given total weight. If theweight 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 inthe piggy-bank is 60.
The minimum amount of money inthe piggy-bank is 100.
This is impossible.
Question Description: Empty piggy bank weight E and full weight F. Now there are n kinds of coins, the value is P, the weight is W. Ask how much it costs.
Method: A simple DP. State transition equation: F [J] = min {f [J], F [J-A [I]. W] + A [I]. p }.
#include<stdio.h>#include<stdlib.h>#include<string.h>#define INF 99999999struct coin{int p, w;};typedef struct coin C;C a[1000];int f[100000];int cmp(const void *a,const void *b) { struct coin *c,*d; c = (struct coin *)a; d = (struct coin *)b; return c->w > d->w ?1 :-1; };int main(){int T, E, F, N, i, j, sum, M;scanf("%d",&T);while(T--){scanf("%d%d",&E,&F);M = F - E;scanf("%d",&N);for(i = 0;i<N;i++){scanf("%d%d",&a[i].p,&a[i].w);}qsort(a, N,sizeof(a[0]), cmp);for(i = 1;i<=M;i++){f[i] = INF;}f[0] = 0;for(i = 0;i<N;i++){for(j = a[i].w;j<=M;j++){if(f[j] > f[j-a[i].w] + a[i].p){f[j] = f[j-a[i].w] + a[i].p;}}}if(f[M] == INF){printf("This is impossible.\n");}else{printf("The minimum amount of money in the piggy-bank is %d.\n",f[M]);}}return 0;}