Hdoj 2191 mourn 512 .. [Multiple backpacks] + [binary decomposition]

Source: Internet
Author: User

The question is really long...

Question: Chinese question, you know ..

Policy: Multiple knapsack problems.The problem of converting multiple backpacks to 01 is that multiple backpacks are initialized, and the number of its parts is a power of 2.The number can be combined into a set of several pieces, which can be any number smaller than or equal to C and will not be repeated. It is called binary decomposition because it can be decomposed in this way.

It is interpreted in the binary form of a number.

For example, if the binary value of 7 is 7 = 111, it can be divided into 001, 010, 100. Combine them into any number less than or equal to 7, and each combination gets a different number. 15 = 1111 can be divided into 0001 0010 0100 1000 four numbers If 13 = 1101, it is divided into 0001 0010 0100. The first three digits can be combined Any number within 7, plus 0110 = 6 can be combined into any one greater than 6 less than 13 Although there are duplicates, we can always consider all the numbers within 13. Thought (Binary thinking) to convert multiple items into a variety of items, you can use 01 backpack to solve the problem.Link: Click to open the link code:
# Include <stdio. h> # include <string. h> # include <math. h> int DP [555], W [555], V [555]; // W indicates the quality, and V indicates the money to be spent. Int main () {int t, n, m, P, h, C, cou; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); memset (DP, 0, sizeof (DP); cou = 0; int I, j; while (M --) {scanf ("% d", & P, & H, & C);/* for (I = 1; I <= C; I + = (INT) Pow (2.0, I) {// POW is always wa. Check that POW returns not int type, but double type, although the test data can pass, however, it may be wrong to get an integer. Therefore, we use an integer to determine V [cou] = I * P; W [cou ++] = I * h; C-= I ;} */int l = 1; while (C> = L) {v [cou] = L * P; W [cou ++] = L * h; C-= L; L <= 1 ;}if (c) {v [cou] = C * P; W [cou ++] = C * H ;}} for (I = 0; I <cou; I ++) {for (j = N; j> = V [I]; j --) {If (DP [J] <DP [J-V [I] + W [I]) DP [J] = DP [J-V [I] + W [I] ;}} printf ("% d \ n", DP [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.