Getting started with a classic full backpack
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1114
Piggy-bank
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 5450 accepted submission (s): 2714
Problem descriptionbefore 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 shoshould be enough cash in the piggy-bank to pay everything that needs
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 pig!
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 varous 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
310 11021 130 5010 11021 150 301 6210 320 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.
Sourcecentral Europe 1999
Recommendeddy
Very classic full backpack entry question
It's not a problem with a backpack !!!
Evaluate the minimum value by claiming the weight before and after the packed items.
Below I post a kb God code, thanks to the powerful kb God... Source code comes from http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127887.html by kb God
# Include <stdio. h> # include <string. h> # define INF 0x7ffffff # define maxn 10000int DP [maxn + 10]; // the minimum value of the content loaded when the capacity of the DP [I] Table is I. Int main () {int W1, W2; int P, W; int t, n; int I, j; scanf ("% d", & T); While (t --) {scanf ("% d", & W1, & W2); scanf ("% d", & N); for (I = 1; I <= w2-w1; I ++) DP [I] = inf; // initialize to infinity DP [0] = 0; while (n --) {scanf ("% d ", & P, & W); for (I = W; I <= w2-w1; I ++) if (DP [I]> DP [I-W] + p) DP [I] = DP [I-W] + P;} If (DP [w2-w1] = inf) printf ("This is impossible. \ n "); else printf (" the minimum amount of money in the piggy-bank is % d. \ n ", DP [w2-w1]);} return 0 ;}
Paste the regular code of the array based on the above ideas.
// AC 93 Ms 292 K 15 kb (by C ++) # include <stdio. h> # include <string. h> int P [505], W [505]; int DP [10005]; int min (int A, int B) {return a <B? A: B;} int main () {int test; int empty, full; int N; int I, j, k; scanf ("% d", & test ); while (test --) {scanf ("% d", & empty, & Full); scanf ("% d", & N); for (I = 1; I <= N; I ++) scanf ("% d", & P [I], & W [I]); memset (DP, 1, sizeof (DP); DP [0] = 0; for (I = 1; I <= N; I ++) for (j = W [I]; j <= Full-empty; j ++) /// unfinished DP [J] = min (DP [J], DP [J-W [I] + P [I]); If (DP [full-empty] = 16843009) printf ("This is impossible. \ n "); else printf (" the minimum amount of money in the piggy-bank is % d. \ n ", DP [full-empty]);} return 0 ;}