Multiplepack coming ~ ^. ^

Source: Internet
Author: User

Multiple backpacks:

Basic Ideas:

Let's take a look at an example: there are n items and a backpack with a capacity of v. A maximum of N [I] items are available for the I-th item. The cost per item is C [I] and the value is W [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest. This type of backpack is characterized by a limited number of items.

It is similar to a full backpack. The basic equation only needs to slightly change the equation of the complete backpack problem, because there is n [I] + 1 strategy for the I-th item: Take 0 pieces, 1 piece... n [I] pieces. If f [I] [v] indicates that the first I items are placed in the maximum weight of a backpack with a capacity of V, there is a state transition equation: f [I] [v] = max {f [I-1] [V-K * C [I] + K * W [I] | 0 <= k <= N [i]} the range of K in the complete backpack is (0 <= k <= V/C [I]). complexity is O (V * Σ N [I]),

The difference between the Code and the full backpack is only in the internal loop

1 for(k = 1; k <= j/weight[i]; ++k)

Change

1 for(k = 1; k <=n[i] && k<=j/weight[i]; ++k)

Binary splitting of multiple backpacks

In the same way as a backpack, the binary idea is used to split N [I] items into several items, the purpose is that any number in 0-N [I] can be exchanged with these items. In addition, policies that exceed n [I] parts are not allowed.

The method is to divide item I into several items, each of which has a coefficient. The cost and value of this item are the original cost and value multiplied by this coefficient, make these coefficients respectively 1, 2, 4 ,..., 2 ^ (k-1), N [I]-2 ^ k + 1, and K satisfies the maximum integer of N [I]-2 ^ k + 1> 0. For example, if n [I] = 13, the item is split into four items with a coefficient of 1, 2, 4, and 6. The coefficient of the divided items is n [I], indicating that the I-th item cannot be obtained more than N [I] items. In addition, this method ensures that each integer between 0 .. n [I] can be expressed by the sum of several coefficients.

1 # include <iostream> 2 using namespace STD; 3 4/* multi-backpack binary splitting 5 * time complexity greater than O (N * V) 6 * space complexity O (N * V) 7 * Set v <= 200 n <= 10, total number of items after splitting <50 8 * each item has log n [I] Status 9 */10 11 int maxv [201]; 12 INT weight [50]; /* record the weight of the object after splitting */13 int value [50];/* record the value of the object after splitting */14 int V, N; 15 16 void main () 17 {18 int I, j; 19 scanf ("% d", & V, & N); 20 int weig, Val, num; 21 int COUNT = 0; 22 23 for (I = 0; I <N; ++ I) 24 {25 scanf ("% d", & weig, & Val, & num); 26 27 for (j = 1; j <= num; j <= 1) // binary split 28 {29 weight [count] = J * weig; 30 value [count ++] = J * val; 31 num-= J; 32} 33 If (Num> 0) 34 {35 weight [count] = num * weig; 36 value [count ++] = num * val; 37} 38} 39 for (I = 0; I <count; ++ I) // use 01 backpack 40 {41 for (j = V; j> = weight [I]; -- j) 42 {43 int TMP = maxv [J-weight [I] + value [I]; 44 maxv [J] = maxv [J]> T MP? Maxv [J]: TMP; 45} 46} 47 printf ("% d", maxv [v]); 48} 49 50/* 51 [input sample] 52 4 2053 3 9 354 5 9 155 9 4 256 8 1 357 [output sample] 58 4759 */

The following describes how o (log amount) processes items in multiple backpacks. amount indicates the number of items:

 1 void multiplepack(int cost, int weight, int amount) 2 { 3     if(cost*amount>=V) 4         cmpletepack(cost,weight); 5     else 6     { 7         int k=1; 8         while(k<amount) 9         {10             zeroonepack(k*cost,k*weight);11             amount-= k;12             k=k*2;13         }14         zeroonepack(amount*cost,amount*weight);15     }16 }

Conclusion ~ I can't answer questions when I want to go home ~ Xiaoxu ~ But I am still insisting on updating my blog every day ~ *. *

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.