Knapsack problem thinking summary

The main problem to explain, see "benevolent machine test Guide" backpack article

A question: why the state transition equation 0-1 is backpack dp [i] [j] = max {dp [i-1] [jw] + v, dp [i-1] [j]} Why comparison right? item is dp [i-1] [j] instead dp [i-1] [jw]?

Because you are here to be updated is dp [i] [j], and not just only consider dp [i-1] [jw], if that is the case dp [i-1] [jw] + v is definitely better than dp [ i-1] [jw] great, much better than anything else. The purpose is to write the value after the total addition of the i-th item but the item was not added to achieve the same total value to achieve the same volume as compared to the backpack, greater value if the i-added items.

Such as have been previously circulating the volume of the article 10, 30, 40 when the value reaches 4, consider now the volume 40, the value of the article 3, the same volume of its value added not as high as the former, it is not placed in a backpack.

Question two: Why backpack j 0-1 to reverse the cycle?

Because each item can only be used once. For example, we have five items, volume 10, 30, 20, 40, valued at 1, 2, 4. Consider equations 0-1 knapsack positive sequence after the optimization cycle is substituted with dp [10] = 1 when you place the first article, since I have only one article, so when computing dp [20] I hope dp [10] or the initial state, but in this case dp [20] will be affected dp [10] becomes 2.

Reverse circulation would not have this problem, because jw <j, so every time reverse cycle time can be guaranteed dp [jw] are not affected by the cycle before.

Question three: how to solve each backpack full unlimited number of items that problem?

Just use the question two, I do not limit the number of each item, then the positive sequence loop just to take into account the effect of the use of superimposed articles.

Question 4: how to split multiple backpack?

An article such as the number of 10, split into 1,2,4,8,3, five indeed may be combined into any number integer of 1-10.

Guess you like

Origin www.cnblogs.com/yun-an/p/10962252.html