P09: questions about backpacks
All the above problems involve the greatest value that can be obtained under the limitation of the capacity (cost) of the backpack. However, there are still many flexible questions about the backpack. It is worth mentioning here. However, I believe that as long as I thoroughly understand how to find the greatest value of the knapsack problem, even if the question method changes, it is not difficult to come upAlgorithm.
For example, you can find out how many items can be placed at most or how much space can be filled with backpacks. You can obtain the values of all States (array f) based on the preceding equation.
Also, if you require "minimum total value" and "Minimum Total number of items", simply change the max value in the above state transition equation to Min.
The following describes some questions with greater changes.
Output Scheme
Generally, a knapsack problem requires an optimal value. If you want to output the optimal value scheme, you can refer to the general method of dynamically planning the problem output scheme: the optimal value of each State is recorded by which of the state transition equation is introduced. In other words, it is recorded by which policy it is introduced. You can find the previous status based on this policy, and push it forward from the previous status.
Take the 01 backpack as an example. The equation isF [I] [v] = max {f [I-1] [v], F [I-1] [V-C [I] + W [I]}
. Use an array G [I] [v], if G [I] [v] = 0 indicates that the value of F [I] [v] is introduced, the first item of the equation is used (that is, F [I] [v] = f [I-1] [v]), G [I] [v] indicates that the last item of the equation is used. Note that the two items indicate two strategies: the I-th item is not selected and the I-th item is selected. Then the pseudo output SchemeCodeWrite as follows (set the final state to f [N] [v]):
I = N
V = V
While (I> 0)
If (G [I] [v] = 0)
Print "item I not selected"
Else if (G [I] [v] = 1)
Print "select item I"
V = V-C [I]
In addition, the first or last item of the equation can also be obtained in real time based on the value of F [I] [v] in the Process of the output scheme, that is, the G array is not required to be recorded, change G [I] [v] = 0 in the above Code to f [I] [v] = f [I-1] [v], G [I] [v] = 1 to f [I] [v] = f [I-1] [V-C [I] + W [I.
Optimal Solution with minimum output Lexicographic Order
Here, the "minimum Lexicographic Order" means that the selection scheme of the No. 1. N item is sorted out and the Lexicographic Order is the smallest. The following uses the solution of outputting the smallest Lexicographic Order of the 01 backpack as an example.
In general, to find the optimal solution with the smallest Lexicographic Order, you only need to pay attention to the policy during the transfer. First, the definition of the subproblem should be slightly changed. We have noticed that if there is an optimal solution for Selecting item 1, the answer must include item 1. The original problem is converted into a backpack with a capacity of V-C [1]. the item is 2 .. n. Otherwise, if the answer does not include item 1, it is converted into a sub-question with the backpack capacity still V and item 2 .. n. Regardless of the answer, the sub-question items are defined in the form of I. N instead of 1. I, so the state definition and the transfer equation must be changed. However, it may be easier to sort items in reverse order first, and the following items are described in reverse order.
In this case, you can calculate the value according to the previous classical state transition equation, but note the following when outputting the scheme: From n to 1, if f [I] [v] = f [I-1] [I-v] and f [I] [v] = f [I-1] [f-C [I]] + W [I] is also set up, the latter (that is, the item I is selected) should be used to output the scheme.
Total number of solutions
In addition to the maximum value that can be obtained after the value of each item is given, the problem of a backpack with a given capacity, item cost, and item relationship (grouping, dependency, etc, you can also get the total number of solutions that are filled with backpacks or packed into a specific capacity.
For this type of question changing, you only need to change Max in the state transition equation to sum. For example, if each item is an item in a full backpack, the transfer equation is
F [I] [v] = sum {f [I-1] [v], F [I] [V-C [I]}
Initial Condition f [0] [0] = 1.
In fact, the reason for this is that the state transition equation has examined all possible backpack composition solutions.
Total number of Optimal Solutions
The optimal solution here refers to the solution with the largest total item value. Take the 01 backpack as an example.
Combined with the idea of finding the maximum total value and the total number of solutions, the total number of optimal solutions can be calculated as follows: F [I] [v] means the same as above, G [I] [v] indicates the total number of optimal solutions for this subproblem, the pseudo code of G [I] [v] while obtaining f [I] [v] is as follows:
For I = 1. n
For V = 0 .. v
F [I] [v] = max {f [I-1] [v], F [I-1] [V-C [I] + W [I]}
G [I] [v] = 0
If (F [I] [v] = f [I-1] [v])
INC (G [I] [v], G [I-1] [v])
If (F [I] [v] = f [I-1] [V-C [I] + W [I])
INC (G [I] [v], G [I-1] [V-C [I])
If this is the first time you see such a problem, Please carefully understand the above pseudo code.
Obtain the sub-optimal solution and the K-Optimal Solution
For the problem of the sub-optimal solution and the K-optimal solution, if the corresponding optimal solution can write the state transition equation and solve it with dynamic planning, the sub-optimal solution can be solved with the same complexity, the k-th optimal solution is a factor k more than the complexity of the optimal solution.
The basic idea is to convert each State into an ordered queue and convert the max/min in the state transition equation into a merge of ordered queues. Here, we will continue to explain the 0th backpack as an example.
First, let's take a look at the state transition equation for the 01 backpack to find the optimal solution:F [I] [v] = max {f [I-1] [v], F [I-1] [V-C [I] + W [I]}
. If the k-th optimization is required, the state f [I] [v] should be a K-sized array f [I] [V] [1. K]. F [I] [V] [k] indicates the value of the K-optimal solution when the size of the first item and backpack is v. "F [I] [v] is an array of K sizes", which may be easier to understand if you are familiar with C, alternatively, you can simply consider adding one dimension to the original equation. Obviously, F [I] [V] [1 .. k] is arranged in ascending order, so we regard it as an ordered queue.
Then the original equation can be interpreted: f [I] [v] This ordered queue is ordered by F [I-1] [v] and f [I-1] [V-C [I] + W [I] the queue is merged. Ordered queue f [I-1] [v] That f [I-1] [V] [1 .. k], F [I-1] [V-C [I] + W [I] is understood as in F [I-1] [V-C [I] [1 .. k. Merge the two ordered queues and store the first K items of the results to f [I] [V] [1 .. k]. The complexity is O (k ). The final answer is f [N] [V] [K]. The total complexity is O (vnk ).
Why is this method correct? In fact, the process of solving a correct state transition equation traverses all available policies, which overwrites all solutions to the problem. However, the optimal solution is ignored because it is the optimal solution. If each State is represented as a K-sized array, and the first K Optimal Values of the State can be saved in an orderly manner in this array. Then, the max operation for any two States is equivalent to the merge of two ordered queues from large to small.
In addition, pay attention to the definition of "k-optimal solution". The two schemes with different policies but the same weights are considered as the same solution or different solutions. If it is the former, it is necessary to ensure that the number in the queue is not repeated during the maintenance of the ordered queue.
Summary
Obviously, it is impossible to exhaust all questions about Dynamic Planning of backpacks. There is even a kind of problem of Combining dynamic planning of backpacks with other fields (such as number theory and graph theory), which will not be discussed in this article. However, it is not difficult to come up with an algorithm as long as you have a profound understanding of the ideas and state transfer equations of all the aforementioned categories and encounter other deformation questions, as long as the question difficulty is still noip.
It should also be an oier's quality.