"Backpack Ninth Lecture"
p01:01 knapsack problem
Topic
There are n items and a backpack with a capacity of V. The cost of article I is c[i], the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.
Basic ideas
This is the most basic knapsack problem, characterized by: only one piece of each item, you can choose to put or not put.
Define the state with sub-question: F[i][v] Indicates the maximum value that the first I item can get in a backpack with a capacity of V. The state transfer equation is: F[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}.
This equation is very important, and basically all the equations for knapsack-related problems are derived from it. So it is necessary to explain it in detail: "Put the first I item into a backpack with a capacity of V" This sub-problem, if only consider the article I item strategy (put or not), then can be converted into a only 1 items of the first I-item problem. If you do not put the article I item, then the problem is converted to "the first I-1 items into the capacity of the backpack"; If you put the article I, then the problem is converted to "the first I-1 items into the remaining capacity of v-c[i] backpack", the maximum value can be obtained is F [i-1][v-c[i]] Plus the value obtained by placing the article I item w[i].
Note F[i][v] is meaningful when and only if there is a subset of the first I item, its cost sum is v. So after the recursive completion of this equation, the final answer is not necessarily f[n][v], but the maximum value of f[n][0..v]. If the "just" word in the definition of the state is removed, a f[i][v-1] is added to the transfer equation so that f[n] [v] is the final answer. As for why this is possible, it is up to you to realize it.
Optimization of spatial complexity
The time and space complexity of the above methods are O (N*v), in which the time complexity can no longer be optimized, but the spatial complexity is optimized to O (V).
First consider how to achieve the basic idea above, there must be a main loop I=1..N, each time the two-dimensional array f[i][0..v] all values. So, if you only use an array f [0..V], can you guarantee that the f[v] in the end of the first cycle is the state we defined F[I][V]? F[I][V] (F[i-1][v] and F[i-1][v-c[i]] Two sub-problems are recursive, can you guarantee to push the f[i][v] (also in the main cycle of the first time) can get f[v] and F[i-1][v]] "value?" In fact, this requires us to v=v in each main loop. 0 in the order of the push f[v], so as to ensure that push f[v] f[v-c[i]] saved is the value of state F[i-1][v-c[i]]. The pseudo code is as follows:
For I=1..N
For V=v. 0
F[v]=max{f[v],f[v-c[i]]+w[i]};
One of the F[v]=max{f[v],f[v-c[i]]} is just equivalent to our transfer equation F[i][v]=max{f[i-1][v],f[i-1][v-c[i]]}, because now F[v-c[i]] is equivalent to the original f[i-1][v-c [i]]. If the cycle order of V is changed from the reverse order of the above, then it becomes f[i][v] [f[i][v-c[i]] infer, and this test instructions, but it is another important knapsack problem P02 The simplest solution, so learning to use only one-dimensional array to solve 01 knapsack problem is very necessary.
Summarize
01 knapsack problem is the most basic knapsack problem, it contains the knapsack problem design state, the basic idea of the equation, in addition, other types of knapsack problem can also be converted to 01 knapsack problem solving. Therefore, we must carefully understand the above basic idea of the method, the significance of the state transfer equation, and finally how to optimize the space complexity.
P02: Complete Backpack problem
Topic
There are n items and a backpack with a capacity of V, with unlimited pieces available for each item. The cost of item I is c[i] and the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.
Basic ideas
The problem is very similar to the 01 knapsack problem, the difference is that each item has unlimited pieces. That is, from the point of view of each item, the strategy associated with it has not taken or not taken two kinds, but take 0 pieces, take 1 pieces, take 2 pieces ... And so many kinds. If you still follow the idea of 01 knapsack, make F[i][v] indicate that the first I item fits into a backpack with a capacity of V maximum weight. The state transfer equation can still be written according to the different strategies of each item, like this: F[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k*c[i]<=v}. This is the same as the 01 knapsack problem with O (n*v) states need to solve, but the time to solve each state is not constant, the time to solve the state F[i][v] is O (V/c[i]), the total complexity is more than O (VN).
The basic idea of the 01 knapsack problem is improved, and a clear method is obtained. This shows that the equation for the 01 knapsack problem is really important and can be extended to other types of knapsack problems. But we're still trying to improve the complexity.
A simple and efficient optimization
Complete knapsack problem has a very simple and effective optimization, is this: if two items I, J satisfies C[i]<=c[j] and w[i]>=w[j], then the item J removed, do not consider. The correctness of this optimization is obvious: In any case, the value of the small cost of the high price of J to the inexpensive I, get at least not worse solutions. For randomly generated data, this method tends to significantly reduce the number of items, thus speeding up. This, however, does not improve the complexity of the worst case scenario, as it is possible that specially designed data can not be removed from a single item.
Conversion to 01 knapsack problem solving
Since 01 knapsack problem is the most basic knapsack problem, then we can consider the complete knapsack problem into 01 knapsack problem to solve. The simplest idea is that, considering the maximum selection of v/c [i] for item I, I can convert item I to v/c[i], and then solve the 01 knapsack problem with the same cost and value. This completely does not improve the time complexity of the basic idea, but this gives us the idea of turning the complete knapsack problem into a 01 knapsack problem: To split an item into multiple items.
A more efficient method of conversion is to split the item I into a fee of c[i]*2^k, the value of w[i]*2^k a number of items, where K satisfies c[i]*2^k<v. This is the idea of binary, because no matter the optimal strategy selected a few items I, can always be expressed as a number of 2^k pieces of goods and. It is a great improvement to break each item into an O (log (v/c[i)) piece. But we have better algorithms for O (VN). * O (VN) algorithm This algorithm uses a one-dimensional array, first look at the pseudo-code: <pre Class "Example" > for I=1..N for V=0..vf[v]=max{f[v],f[v-c[i]]+w[i]};
You will find that this pseudo-code differs from the P01 's pseudo-code only in the order of the V loop. Why is such a change feasible? First think about why P01 in accordance with V=V. 0 in reverse order to cycle. This is due to the fact that the state in the first cycle of f[i][v] is recursive by state F[i-1][v-c[i]]. In other words, this is to ensure that each item is selected only once, to ensure that when considering the strategy of "Select Item I", it is based on a sub-result (F[i-1][v-c[i]) that has not been selected for the article I item. And now the full backpack is a unique feature of each item can be selected unlimited, so in the consideration of the "optional item I" This strategy, but I need a may have been selected in the sub-results of the item I (F[i][v-c[i]), so you can and must be in the order of v= 0..V cycle. This is the reason why this simple procedure is set up.
This algorithm can also be derived from other ideas. For example, the state transition equation in the basic train of thought can be equivalent to form this form: F[i][v]=max{f[i-1][v],f[i][v-c[i]]+w[i]}, the equation is implemented with a one-dimensional array, the above pseudo-code.
Summarize
The complete knapsack problem is also a fairly basic knapsack problem, and it has two state transfer equations, respectively, in the "basic idea" and the "O (VN) algorithm" section. I hope you will be able to understand these two state transfer equations carefully, not only to remember, but also to figure out how they come out, it is best to think of a way to get these equations. In fact, it is a good way to deepen the understanding of dynamic programming and improve the dynamic programming skill to think about the meaning and how to get the equation of each dynamic programming topic.
P03: Multiple knapsack problem
Topic
There are n kinds of goods and a backpack with a capacity of V. (i) Items with a maximum of n[i] pieces available, each cost is c[i], the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.
Basic algorithms
This topic is very similar to the complete knapsack problem. The basic equation simply changes the equation of the complete knapsack problem slightly, because there are n[i]+1 strategies for item I: Take 0 pieces, take 1 pieces ... Take n[i] pieces. F[I][V] Indicates the maximum weight of the first I object into a backpack of a capacity of V, then: f[i][v]=max{f[i-1][v-k*c[i]]+ K*w[i]|0<=k<=n[i]}. Complexity is O (V*∑n[i]).
Conversion to 01 knapsack problem
Another good way to write the basic method is to convert to 01 knapsack solution: The item I to n[i] pieces 01 Backpack items, then get the number of items ∑n[i] 01 knapsack problem, the direct solution, the complexity is still O (V*∑n[i]).
But we expect to turn it into a 01 knapsack problem that can reduce complexity like a full backpack. Still taking into account the idea of binary, we consider replacing item I with a number of items, so that each strategy that is desirable for article I in the original question is 0. N[i] pieces--can be equivalent to taking a number of items after substitution. In addition, the strategy of taking more than n[i] pieces will not appear.
The method is to divide the item I into several items, each of which has a factor, and the cost and value of the item are multiplied by this factor. The coefficients are 1,2,4,..., 2^ (k-1), n[i]-2^k+1, and K is the largest integer that satisfies n[i]-2^k+1>0. For example, if N[i] is 13, the items are divided into four items with coefficients of 1,2,4,6 respectively.
The coefficients of these items are divided into n[i], which indicates that it is impossible to take more than n[i] pieces of article I items. In addition, this method can also be guaranteed for 0. N[i] Each integer, can use a number of coefficients and the expression, this proof can be divided into 0. 2^k-1 and 2^k. N[i] Two paragraphs to discuss separately, it is not difficult, I hope you think yourself to try.
In this way, the item I is divided into O (log n[i]) species, the original problem is converted to the complexity of O (V*∑log n[i]) 01 knapsack problem, is a great improvement.
O (VN) algorithm
The multiple knapsack problem also has an O (VN) algorithm. This algorithm is based on the state transition equation of the basic algorithm, but the method of applying the monotone queue allows the value of each state to be solved by averaging O (1) time. Because the DP optimized by the monotone queue is beyond the range of the Noip, this article is no longer explained. I initially learned that this method was on a "man's eight" slide in the building sky.
Summary
Here we see the process of improving the complexity of an algorithm from O (V*∑n[i]) to O (V*∑log n[i]), as well as the existence of an O (VN) algorithm that applies knowledge beyond the Noip range. I hope you pay special attention to the idea and method of "splitting items", prove its correctness, and implement it with the simplest program possible.
P04: Mixed three kinds of knapsack problem
Problem
If P01, P02, P03 are mixed together. That is, some items can only be taken once (01 backpack), some items can be taken unlimited (full backpack), and some items can be taken the number of times there is an upper limit (multiple backpack). How should it be solved?
01 Backpack mixed with a complete backpack
Considering that there is only one difference in the pseudo-code given in P01 and P02, if there are only two categories of items: one can only be taken once and the other is unlimited, then only when the transfer equation is applied to each item, the order or reverse circulation can be selected according to the category of the item, and the complexity is O (VN). The pseudo code is as follows:
For I=1..N
If article I item is 01 backpack
For V=v. 0
F[v]=max{f[v],f[v-c[i]]+w[i]};
else if article I item is complete backpack
For V=0..V
F[v]=max{f[v],f[v-c[i]]+w[i]};
Plus multiple backpacks.
If the addition of some items can be limited to the maximum, then in principle can also give an O (VN) Solution: Encountered multiple knapsack type of items with a monotone queue solution. However, if you do not consider an algorithm that exceeds the Noip range, the method of dividing each such item into an O (log n[i]) 01 backpack in P03 is also excellent.
Summary
Some people say that difficult topics are superimposed on simple topics. Whether or not this sentence is axiomatic, but it has been fully embodied in this lecture. Originally 01 backpack, complete backpack, multi-pack is not a problem, but the simple combination of them later on to get this kind of a certain can frighten many people's topic. But as long as the foundation is solid, comprehend three kinds of basic knapsack question thought, can do to divide the difficult question into the simple question to solve.
P05: knapsack problem with two-dimensional cost
Problem
Two-dimensional knapsack problem refers to: For each item, there are different costs; Select this item must pay both costs; there is a maximum value (backpack capacity) that can be paid for each price. Ask how you choose items to get the most value. The two costs are set at the cost of 1 and the cost of 2 respectively, and the two costs required for article I items are a[i] and b[i]. The maximum value that can be paid for the two costs (two pack sizes) is V and U respectively. The value of the item is w[i].
Algorithm
Cost plus one dimension, just add one dimension to the state. Set F[i][v][u] represents the maximum value that can be obtained when the first I item is paid at two costs, respectively V and U. The state transition equation is: f [i][v][u]=max{f[i-1][v][u],f[i-1][v-a[i]][u-b[i]]+w[i]}. As described above, only two-dimensional arrays can be used: when each item can only be taken once, the variables V and u are in sequential cycles, and when the item is like a complete knapsack problem, the reverse loop is used. Split items when items are like multiple knapsack problems.
Restrictions on the total number of items
Sometimes, the "two-dimensional fee" condition is given in such an implicit way: at most, only m-items can be taken. This is actually the equivalent of a "piece" of each item, the cost of each item is 1, the maximum number of pieces can be paid m. In other words, set f[v][m] to pay the cost V, up to the maximum value of M-pieces can be obtained, then according to the type of goods (01, complete, multiple) in different ways to cycle updates, and finally in the range of f[0..v][0..m] to find the answer.
In addition, if you are asked to "just take M-items", look for answers within the f[0..v][m] range.
Summary
In fact, it is a more common method to add a weft in the original state to meet the new limit when it is found that the problem is distorted by the familiar dynamic programming problem. I hope you can understand this method from the beginning of this lecture.
P06: knapsack problem in groups
Problem
There are n items and a backpack with a capacity of V. The cost of article I is c[i], the value is w[i]. These items are divided into groups in which the items in each group clash with each other, with a maximum of one item selected. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.
Algorithm
This problem becomes a group of items with a number of strategies: choose one of the groups or not. That is, set F[K][V] indicates that the first K-Group Items cost V can obtain the maximum weight, then there are f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]| items I belong to the K group}.
The pseudo-code for using a one-dimensional array is as follows:
For all Groups K
For all I belong to Group K
For V=v. 0
F[v]=max{f[v],f[v-c[i]]+w[i]}
In addition, it is obvious that "a simple and effective optimization" can be applied to the items in each group P02.
Summary
Grouped knapsack problems are called a group of mutually exclusive items, which creates a good model. Many knapsack problems can be transformed into a group of knapsack problems (such as P07), by the group of knapsack problem further can define the concept of "generalized items", is very helpful to solve problems.
P07: A dependent knapsack problem
The problem of simplification
There is some kind of "dependency" between the items in this knapsack problem. That is, I relies on j, indicating that if you select item I, you must select item J. For the sake of simplification, we first set up no item that depends on something else and is dependent on something else, and no item is dependent on more than one item at a time.
Algorithm
The problem was extended by NOIP2006 's budget proposal. In accordance with the wording of the question, items that do not depend on other items are referred to as "main pieces", and items that depend on the main part are referred to as "attachments". The simplified condition of this problem shows that all items consist of several main pieces and a collection of attachments that depend on each main piece.
In accordance with the general idea of knapsack problem, consider only one main piece and its attachment set. However, there are many available strategies, including: One is not selected, only select the main parts, select the main part and then select an attachment, select the main part and then select two attachments ... There is no way to represent so many policies with the state transition equation. (In fact, with n attachments, the strategy has 2^n+1, which is the number of points.) )
Considering that all of these policies are mutually exclusive (that is, you can only select one policy), a master and its attachment collections actually correspond to an item group in P06, and each policy that selects the main part and selects several attachments corresponds to an item in this group of items. Its cost and value are the same as the value of the items in this strategy. But this conversion alone does not give a good algorithm because the items in the item group are as many as the original problem.
Consider the words in P06: "A simple and effective optimization" can be applied to the items in each group P02. This suggests that for items in an item group, all items with the same cost will be one of the most valuable, without affecting the result. Therefore, we can first carry out the "attachment collection" of the main pieces of the 01 backpack, the cost is 0. V-c[i] All these values correspond to the maximum value F ' [0..v-c[i]]. Then the main piece and its attachment set are equivalent to the v-c[i]+1 items, where the cost of c[i]+k items is the value of f ' [K]+w[i]. In other words, there are many strategies in the index level is redundant, through a 01 backpack, the main part I into V-C[I]+1 items of the item group, you can directly apply the P06 algorithm to solve the problem.
A more general question.
The more general problem is that dependencies are given in the form of "forests" in graph theory (a collection of forests that are multi-forked trees), meaning that attachments to the main pieces can still have their own collection of attachments, limiting that each item is dependent on only one item at most (only one main piece) and no cyclic dependencies.
Solving this problem can still be used to convert each master piece and its collection of attachments into an item group. The only difference is that since attachments may also have attachments, each attachment cannot be considered as a generic 01 item in the backpack. If the attachment also has a collection of attachments, it must be converted into an inventory first, and then use the group knapsack problem to solve the main component and its attachment set of attachments corresponding to the corresponding value of the attachment of the various costs.
In fact, this is a tree DP, which is characterized by the need for each parent node to perform a DP of its individual son's properties to obtain its own related properties. This has touched on the idea of "generalization items". After reading the P08, you will find that this "dependency tree" each subtree is equivalent to a generalization of items, to find a node as the root of the sub-tree corresponding to the generalization of the object is the sum of the corresponding generalization of all its sons.
Summary
NOIP2006 's backpack problem I did a lot of failure, wrote hundreds of lines of code, but a point not. Then I realized by thinking that by introducing the concept of "item group" and "dependency", we could deepen the understanding of the problem and solve its generalization problem. Consider the extremely special dependency in the item group: The item cannot be made both as a main part and as an attachment, with a maximum of two attachments per main piece, and a main piece and its two attachments are equivalent to an inventory of four items, revealing some of the nature of the problem.
I want to say: failure is not a shameful thing, from the failure of all no harvest is.
P08: Generalization Items
Defined
Considering such an item, it does not have a fixed cost and value, but its value varies with the cost that you allocate to it. This is the concept of generalized objects.
More strictly defined. In knapsack problem with backpack capacity V, the generalization item is a defined field of 0. The function H of an integer in V, the value that can be obtained is H (v) when it is allocated a cost of V.
This definition has a little abstraction, and another understanding is that a generalization item is an array of h[0..v], giving it the cost of V, which can be valued h[v].
An item with a C value of W, if it is an item in a 01 backpack, then consider it a generalization item, which is a function other than H (c) =w other function values are 0. If it is an item in a complete backpack, it can be seen as a function that only has H (v) =v/c*w when the V is divisible by C and the other function values are 0. If it is an item with a maximum number of repetitions of N in a multi-pack, the function of its corresponding generalization item is H (v) =v/c*w only if V is divisible by C and v/c<=n, the other function values are 0.
An item group can be regarded as a generalization item H. For a 0.. V, if the item in the inventory does not have an item with a fee of V, then h (v) = 0, otherwise h (v) is the maximum value of all items with a cost of V. Each main component and its attachment set in P07 is equivalent to an item group, which can naturally be regarded as a generalization item.
Generalization of goods and
If you are facing two generalization items H and L, how can I get the maximum value from these two generalized items with a given fee? In fact, for a given fee V, simply enumerate how this fee is allocated to two generalization items. Similarly, for 0. V for each integer v, you can obtain the maximum value F (v) of the cost V allocated to H and L. Also F (v) =max{h (k) +l (v-k) |0<=k<=v}. As you can see, F is also a defined field determined by the generalization items H and L for 0. The function of V, that is, F is a generalization item determined by the generalization items H and L.
Thus can define the generalization of the goods and: H, L are generalized articles, if the generalized article F satisfies f (v) =max{h (k) +l (v-k) |0<=k<=v}, then said F is H and L and, that is, f=h+l. The time complexity of this operation is O (v^2).
The definition of generalized items indicates that in a knapsack problem, if the two generalization items are made with their own, they do not affect the answer of the question. In fact, the process of asking for the answer is the process of asking for the sum of all these generalized items, which are the knapsack problem of the generalization of items. Set this and for S, the answer is the maximum value in S[0..V].
Generalized items for knapsack problems
A knapsack problem may give a lot of conditions, including the cost of each item, value and other attributes, the grouping of items, dependencies and other relationships. But you can definitely match the problem to a generalization item. That is, given all the conditions, it is possible to calculate for each nonnegative integer V: If the backpack capacity is V, the maximum value that can be obtained by loading the item into the backpack can be considered as a generalization item defined on the nonnegative integer set. This generalization--or a function that defines the domain as a nonnegative integer--Contains a highly condensed message about the problem itself. Generally, a subdomain of this generalization is obtained (for example, 0. V), it is possible to take the final answer to the knapsack problem based on the function's value.
To sum up, in general, the problem of solving knapsack, that is, to solve the problem of the corresponding function, that is, the problem of generalization items. One way to solve a generalized object is to represent it as a sum of several generalized items and then ask for it.
Summary
This speech can be said to be my own original ideas. Specifically, I am learning the Scheme language of functional programming, using functional programming to examine the theory of various knapsack problems. This is really very abstract, perhaps in the "abstract degree of the model" this aspect has exceeded the requirements of the Noip, so for the moment do not understand also does not matter. believe that as your path of Oi extends, one day you will understand.
I want to say: "Thinking" is one of the most important qualities of a oier. Simple questions, in-depth thinking, you can find more.
P09: Changes in the question of knapsack
The above-mentioned various knapsack problems are required in the backpack capacity (cost) to find the maximum value can be taken, but the knapsack problem there are a lot of flexible ask, here is worth mentioning. But I think, as long as the best way to understand the knapsack problem is the most valuable method, even if the question changes, it is not difficult to figure out the algorithm.
For example, find out how many items can be placed or how many backpacks can be filled. This can be obtained by using the previous equation to find out the values of all States (f-arrays) based on the specific problem.
Also, if the requirement is "minimum total value" "minimum total number of pieces", simply change Max in the state transfer equation above to Min.
Here are some of the more varied questions.
Output Scenarios
In general, the knapsack problem is to ask for an optimal value, if it is required to output the optimal value of the scheme, you can refer to the normal dynamic programming problem output method: The record of each state is the optimal value of the state transfer equation which is introduced, in other words, the record of which strategy is introduced. You can find the previous state based on this policy, then push forward from the previous state.
In the case of 01 backpacks, the equation is f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}. Then use an array g[i][v], set g[i][v]=0 to show the value of F[i][v] is the first item of the equation (also known as f[i][v]=f[i-1][v]), g[i][v] means the latter term of the equation is used. Note that the two options represent two strategies: the item I was not selected and the article I was selected. Then the pseudo-code of the output scheme can be written like this (set the final state to F[n][v]):
I=n
V=v
while (i>0)
if (g[i][v]==0)
Print "Item I items not selected"
else if (g[i][v]==1)
Print "Article I item selected"
V=v-c[i]
In addition, the use of the former or the latter of the equation can also be in the process of the output scheme according to the value of f[i][v] in real time, that is, do not have to record the G array, the above code in the g[i] [v]==0 changed to F[i][v]==f[i-1][v],g[i][v]==1 to f[i][ V]==f[i-1][v-c[i]]+w[i] also available.
Optimal scheme for minimizing output dictionary order
Here the word "minimum Dictionary order" means 1. The selection scheme of the n items is arranged in order to minimize the number of dictionaries. Take the example of a scheme that outputs a 01 backpack minimum dictionary sequence.
In general, it is necessary to pay attention to the strategy when transferring the best scheme with the smallest dictionary order. First, the definition of sub-problem is slightly changed. We note that if there is an optimal option for item 1, then the answer must contain item 1, the original problem is converted to a backpack capacity of v-c[1], the item is 2. The sub-problem of N. Conversely, if the answer does not contain item 1, the conversion to backpack capacity is still V, the item is 2. The sub-problem of N. Regardless of the answer, the items of the sub-problem are in I. n rather than the 1 mentioned above. In the form of I, so the definition of the state and the transfer equation need to be changed. But perhaps the easier way is to put the items in reverse order, the following items have been sorted in reverse order to describe.
In this case, it can be evaluated according to the previous classical state transition equation, only when the output scheme should be noted: when the input from N to 1, if F[I][V]==F[I-V] and F[i][v]==f[i-1][f-c[i]]+w[i] are simultaneously established, The scheme should be output according to the latter (that is, item I is selected).
Total number of solutions
For a knapsack problem given the knapsack capacity, the cost of goods, the relationship between items (grouping, dependence, etc.), in addition to the maximum value that can be obtained after the value of each item is given, you can also get the total number of packages filled with backpacks or packaged back to a specified capacity.
For this kind of change to ask the question, generally only need to change the state transition equation max to sum. For example, if each item is an item in a 01 backpack, the transfer equation is f[i][v]=sum{f[i-1][v],f[i-1][v-c[i]]+w[i]} and the initial condition is f[0][0]=1.
In fact, the reason for this is that the state transition equation has examined all possible knapsack composition schemes.
Total number of best practices
The best solution here is to refer to the total value of the item. Take 01 backpacks as an example.
Combined with the idea of finding the maximum total value and two solutions, the total number of optimal schemes can be as follows: F[i][v] meaning, G[i][v] represents the total number of optimal schemes for this sub-problem, then the pseudo-code for G[I][V] while seeking F[I][V] is the following:
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 have seen such a problem, please carefully understand the pseudocode above.
Summary
Obviously, it is impossible to exhaust all the questions of knapsack dynamic programming. There is even a problem of combining knapsack dynamic programming problems with other fields (such as number theory, graph theory), which is not addressed in this article on knapsack problems. But as long as the above-mentioned all kinds of knapsack problem and state transfer equation, encountered other deformation ask, as long as the problem is NOIP, it should not be difficult to come up with algorithms.
Comprehend by analogy, extrapolate, should also be a oier should be the quality of it.
Reprint: "Backpack Ninth lecture"