P04: mixed three backpack Problems

Source: Internet
Author: User
P04: Mixed backpack Problems

If you mix p01, p02, and P03. That is to say, some items can be taken only once (01 backpack), some items can be taken unlimited times (full backpack), and some items can be taken up to a maximum of times (multiple backpacks ). How should we solve it?

01 mixing a backpack with a full backpack

Take into account the pseudo values given in p01 and p02CodeOnly one item is different, so if there are only two types of items: one item can only be retrieved once, and the other item can be retrieved infinitely, you only need to apply the transfer equation to each item, select an order or backward Loop Based on the item category. The complexity is O (VN ). The pseudocode is as follows:

 
For I = 1. n
 
If the I-th item belongs to the 01 backpack
 
For v = V .. 0
 
F [v] = max {f [v], F [V-C [I] + W [I]};
 
Else if the I-th item belongs to the full backpack
 
For V = 0 .. v
 
F [v] = max {f [v], F [V-C [I] + W [I]};
Plus multiple backpacks

If you can add up to a limited number of items, you can also give an O (VN) solution in principle: You can solve multiple types of items in a monotonous queue. However, if the value exceeds the noip rangeAlgorithmThen, you can use P03 to divide each of these items into items in the O (log n [I]) 001 backpack.

Of course, a clearer way of writing is to call the three relevant processes we mentioned earlier.

 
For I = 1. n
If the I-th item belongs to the 01 backpack
 
Zeroonepack (C [I], W [I])
 
Else if the I-th item belongs to the full backpack
 
Completepack (C [I], W [I])
 
Else if the I-th item belongs to multiple backpacks
 
Multiplepack (C [I], W [I], n [I])

When we first wrote these three processes, we probably did not think they would be mixed here. I think this shows the power of abstraction in programming. If you have been writing each type of backpack problem in this "abstract process" way, you are also very aware of the differences in their implementations, so when we encounter the problem of mixing three backpacks, we will be able to quickly think of the simple solution above, right?

Summary

Some people say that difficult questions are all superimposed by simple questions. Whether or not this sentence is still true or not, but it has been fully reflected in this lecture. Ben lai01's backpacks, full backpacks, and multiple backpacks are not difficult, but after they are simply Combined, they will have such a question that will surely scare many people. However, as long as you have a solid foundation and understand the ideas of the three basic knapsack problems, you can split the difficult questions into simple ones to solve them.

 

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.