Zju2014 piggy-bank-classic Dynamic Planning

Source: Internet
Author: User

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.

*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.