HDU 1059 dividing (multiple backpack template questions, changed conditions, more flexible)

Source: Internet
Author: User
Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1059 at first glance, I did not think of the type of backpack, so how do we analyze it as a problem of multiple backpacks? The first thing we can know is that if we want to divide these coins into two equal copies of the value, we can use sum to save the total value, using the total value as the size of the backpack, we can use half of the size of the backpack to hold half of the worth of marbles, of course, the remaining half of the backpack can be loaded with the other half worth the marbles. Restrictions, Valuable, Capacity availableSo we use multiple backpacks. AC code: it is a set of multiple backpack templates. This is a template that I think is better understood. The dynamic equation is actually a template of 10 backpacks, full backpacks. The most important thing is that multiple backpacks Binary processing.If you are not quite clear about the problem, you can view the package uploaded from the Internet.
# Include <iostream> # include <stdio. h> using namespace STD; int num [7]; int sum; int DP [1, 100000]; int max (int A, int B) {If (A> B) return; elsereturn B;} void zeroonepack (INT cost, int weight) {int I; for (I = sum; I> = cost; I --) DP [I] = max (DP [I], DP [I-cost] + weight);} void completepack (INT cost, int weight) {int I; for (I = cost; I <= sum; I ++) DP [I] = max (DP [I], DP [I-cost] + weight );} void multiplepack (INT cost, int weight, int amount) {If (C Ost * Amount> = sum) {completepack (cost, weight);} else {int K = 1; while (k <= Amount) {zeroonepack (K * cost, K * weight); amount-= K; k <= 1; // * 2} zeroonepack (Amount * cost, amount * weight);} int main () {int I; int case = 1; while (1) {memset (DP, 0, sizeof (DP); memset (Num, 0, sizeof (Num )); sum = 0; for (I = 1; I <= 6; I ++) {scanf ("% d", & num [I]); sum + = num [I] * I;} If (sum = 0) break; printf ("collection # % d: \ n", Case ++ ); if (sum % 2 = 1) {printf ("can't be divided. \ N "); continue;} sum/= 2; for (I = 1; I <= 6; I ++) {If (Num [I] = 0) continue; multiplepack (I, I, num [I]);} If (DP [Sum]! = Sum) // convert it to whether or not half of the value of an item can be loaded into a half-capacity package. Printf ("can't be divided. \ n"); elseprintf ("can be divided. \ n");} 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.