Poj 3260 the fewest coins (Backpack)

Source: Internet
Author: User

/*Personal Opinion: I really think poj is more classic than HDU !!! More and more think this way !!!

The same was said yesterday !!! It seems that the center of gravity needs to be shifted !!! */

This is a multi-bag question. It is very good. You can simply change it and you will know that this question is good !!!

Admire those experts who have made questions !!! Haha ·····

Multiple backpack templates :(The template has just been typed. Check whether the template is correct or not.)

@ 1:

# Include <stdio. h>
# Include <string. h>
# Define max (A, B) A> B? A: B

Int vv;
Int BB [10000];
Void Ni (int w, int V)
{
Int I;
For (I = vv; I> = V; I ++)
Bb [I] = max (BB [I], BB [I-v] + W );
}
Void Shun (int w, int V)
{
Int I;
For (I = V; I <= vv; I ++)
Bb [I] = max (BB [I], BB [I-v] + W );
}

Int main ()
{
Int N, amount [100], W [100], V [100], K, I;
Scanf ("% d", & N, & vv );
For (I = 0; I <n; I ++)
Scanf ("% d", & W [I], & V [I], & amount [I]);
Memset (BB, 0, sizeof (bb ));
For (I = 0; I <n; I ++)
{
If (amount [I] * V [I]> = vv)
{
Shun (W [I], V [I]);
}
Else
{
K = 1;
While (amount [I]> K)
{
Amount [I]-= K;
Ni (K * W [I], K * V [I]);
K * = 2;
}
Ni (amount [I] * W [I], amount [I] * V [I]);
}
}
Printf ("% d \ n", BB [VV]);
Return 0;
}

@ 2: (this triple loop is not good)

# Include <stdio. h>
# Include <string. h>
# Define max (A, B) A> B? A: B

Int vv;
Int BB [10000];

Int main ()
{
Int N, amount [100], W [100], V [100], K, I, J;
Scanf ("% d", & N, & vv );
For (I = 0; I <n; I ++)
Scanf ("% d", & W [I], & V [I], & amount [I]);
Memset (BB, 0, sizeof (bb ));
For (I = 0; I <n; I ++)
For (j = vv; j> = V [I]; j --)
For (k = 1; k <amount [I] & J> = amount [I] * k; k ++)
Bb [J] = max (BB [J], BB [J-K * V [I] + K * W [I]);
Printf ("% d \ n", BB [VV]);
Return 0;
}

*/

Code:

# Include <stdio. h>

# Include <string. h>

# Define INF 99999999
# Define max (A, B) A> B? A: B
# Define min (a, B) a <B? A: B

Int main ()
{
Int VV, N, I, J, K, ans;
Int bb1 [20000], BB2 [20000], amount [20000], W [20000];
Scanf ("% d", & N, & vv );
For (I = 0; I <20000; I ++)
{
Bb1 [I] = inf;
BB2 [I] = inf;
}
For (I = 0; I <n; I ++)
Scanf ("% d", & W [I]);
For (I = 0; I <n; I ++)
Scanf ("% d", & amount [I]);
Bb1 [0] = 0;
For (I = 0; I <n; I ++)
For (j = W [I]; j <= 10000; j ++)
Bb1 [J] = min (bb1 [J], bb1 [J-W [I] + 1 );
BB2 [0] = 0;
For (I = 0; I <n; I ++)
For (j = 10000; j> = W [I]; j --)
For (k = 1; k <= amount [I] & J> = K * W [I]; k ++)
BB2 [J] = min (BB2 [J], BB2 [J-W [I] * k] + k );
Ans = BB2 [VV];
For (I = vv + 1; I <= 10000; I ++)
If (ANS> BB2 [I] + bb1 [I-VV])
Ans = BB2 [I] + bb1 [I-VV];
If (ANS = inf)
Printf ("-1 \ n ");
Else
Printf ("% d \ n", ANS );

Return 0;

}

Question link: http://poj.org/problem? Id = 3260

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.