Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1114
The weight e of a pig Qian Ang is given, and the weight of a pig Qian ang filled with money is f-e. Then there are n currencies, each of which has two attributes: nominal value + weight. After the question is filled (note the Keyword: Exactly), the minimum amount of money required corresponds to the amount of money (a bit of mouth = .) Taking the first group of data as an example,
10 110
2
1 1
30 50
Of course we use two 30 s to fill this can only hold 100 of the weight. If we use a currency with a nominal value of 1, we need 100, obviously not enough.
Well, back to the question, this question is a full backpack question. The inf file used to initialize the DP array must be large ~~~ Otherwise, it will... wa!
There should be no strangers to those I have learned. If I have just gotten in touch with me, I can look at dd Daniel's backpack (actually I don't know who is here ).
Or the captain gave: http://love-oriented.com/pack/pack2rc.pdf (this guy... don't know if it's dd Daniel)
This is what I understand most (I am stupid anyway): http://wenku.baidu.com/link? Url = yHMBToaaKpk8mRFn0aCCcq02MTyCIjGQ8npyI-XDfkAvkLqNRKpxLkNnJf0s3l-XdZK99XwQZiEZ6hqxFt0WZbRMu3ZaNxdE-1o0ZI4ssq3
Another important thing is initialization !! Note that if it is full, 0 cannot be used to initialize the DP array, but INF is used. (DD is written by Daniel, but I still don't know why)
1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 using namespace STD; 5 6 const int maxn = 1e4 + 10; 7 const int INF = 100000000; // This INF must be great and big !!!! 8 9 int W [maxn], V [maxn]; 10 int DP [maxn]; 11 12 INT main () 13 {14 int t, n, m, E, F; 15 while (scanf ("% d", & T )! = EOF) 16 {17 while (t --) 18 {19 scanf ("% d", & E, & F); 20 int maxweight = f-E; 21 scanf ("% d", & N); 22 for (INT I = 0; I <n; I ++) 23 scanf ("% d ", & V [I], & W [I]); 24 for (INT I = 1; I <= maxweight; I ++) 25 DP [I] = inf; 26 dp [0] = 0; 27 for (INT I = 0; I <n; I ++) 28 {29 for (Int J = W [I]; j <= maxweight; j ++) 30 {31 DP [J] = min (DP [J], DP [J-W [I] + V [I]); // select the minimum value of 32} 33} 34 if (DP [maxweight]! = Inf) 35 printf ("the minimum amount of money in the piggy-bank is % d. \ n ", DP [maxweight]); 36 else37 printf (" This is impossible. \ n "); 38} 39} 40 return 0; 41}