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.