Poj 1276 cash machine (multiple backpacks)

Source: Internet
Author: User
Http://poj.org/problem? Id = 1276

There is a cash machine containing T currencies with a nominal value of N [I], each with a V [I. The maximum amount that can be exchanged if the exchange amount does not exceed the cash RMB.

 

Multiple backpacks are converted into 01 backpacks, which require binary optimization. 9. The answer to this story is quite good. Add a coefficient before the value of each item and ensure that the combination of these coefficients includes all the numbers of 1. N and the sum is N.

Code:

# Include <cstdio>
# Include <cstring>
# Define max (A, B) A> B? A: B
Int DP [100005];
Int Val [105];
Int main (){
Int cash, N, V, G, I, J, count;
While (~ Scanf ("% d", & cash, & N )){
If (! Cash |! N ){
While (n --)
Scanf ("% d", & G, & V );
Printf ("0 \ n ");
Continue;
}
Count = 0;
Memset (DP, 0, sizeof (DP ));
While (n --){
Scanf ("% d", & G, & V );
// Binary Optimization
I = 1;
While (G> = I ){
Val [count ++] = I * V;
G-= I;
I * = 2;
}
If (g) Val [count ++] = V * g;
}
// 01 solve a backpack
For (I = 0; I <count; I ++ ){
For (j = cash; j> = Val [I]; j --){
DP [J] = max (DP [J], DP [J-Val [I] + val [I]);
}
}
Printf ("% d \ n", DP [Cash]);
}
Return 0;

}

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.