Description:
The weight of a heap of coins is m (<10000), and there may be n coins (n <500). The value and weight are VI and WI, respectively. What is the minimum value of the coin? If the output "This is impossible." cannot be obtained ."
Analysis:
It can be seen that this question is the deformation of the 01 backpack problem, that is
1. Each item has an unlimited number
2. The entire backpack must be filled.
That is, the 01 backpack with two restrictions.
The most common method I use does not affect the sorting of items.
/*
Zju2014 piggy-bank
*/
# Include <stdio. h>
# Include <string. h>
# Define n 501
# Define M 10001
# Define CLR (a) memset (A, 0, sizeof ())
Struct nod {
Int V, W;
};
Typedef struct nod node;
Node A [n];
Char E [m];
Int B [m], maxm;
Int M, N;
Int CMP (void const * P, void const * q ){
Node * x = (node *) P;
Node * Y = (node *) q;
If (X-> W = Y-> W ){
Return X-> V-y-> V> 0? 1:-1;
} Else return y-> W-X-> W;
}
Int main ()
{
Int I, J, K, T;
Scanf ("% d", & T );
While (t --)
{
Int W1, W2;
// Init
CLR (E );
CLR (B );
// Input
Scanf ("% d", & W1, & W2 );
M = w2-w1;
Scanf ("% d", & n );
For (I = 0; I <n; I ++)
Scanf ("% d", & a [I]. v, & a [I]. w );
// Qsort (a, n, sizeof (a [0]), cmp );
// DP
E [0] = 1;
B [0] = 0;
For (I = 0; I <n; I ++) // each icon
{
For (j = 0; j <= m; j ++)
{
If (e [j]) {
K = j + a [I]. w;
If (k <= m &&(! E [k] | B [j] + a [I]. v <B [k]) {
B [k] = B [j] + a [I]. v;
E [k] = 1;
}
}
}
}
If (! E [m]) printf ("This is impossible./N ");
Else printf ("the minimum amount of money in the piggy-bank is % d./N", B [m]);
}
// System ("pause ");
Return 0;
}
/*
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.
*/