Knapsack problem Summary


Backpack nine stresses - the whole chapter and achieve a detailed understanding of the code

https://blog.csdn.net/yandaoqiusheng/article/details/84782655


01 details backpack initialization of
the optimal solution of the knapsack problem we see in the title, in fact, there are two less the same question is asked. Some questions asked during the optimal solution "just filled backpack", and some topics are not required to be filled backpack. The difference between the two is somewhat different question is asked during initialization.
If the first question is asked, requiring just filled backpack, then the initialization except f [0] to 000 other f [1 ... V] are set to -∞, so that you can ensure that the resulting f [ N] is the optimal solution is just one kind of full backpack.
If the backpack is not required to be filled, but only want the price to as large as possible, initialization should be f [0 ... V] are all set to 00.
why? Can be understood: to initialize an array of f ff fact is, that nothing can be placed in a legal state when the backpack. If you require a backpack full of just, so this time only capacity backpack 0 may be the value of nothing 0 "just filled with" the capacity of other backpackers were no legal solution, it belongs to an undefined state, their values are It should be a -∞. If the backpack does not have to be filled, then any capacity backpack has a legitimate solution "nothing loaded," The value of this solution is 0, so the value of the initial state of it all is a 0. This trick can be extended to other types of knapsack problem, the back will no longer be initialized before the state transition to explain.
----------------
Disclaimer: This article is CSDN blogger "good Mio two months' original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/yandaoqiusheng/article/details/84782655

 

Guess you like

Origin www.cnblogs.com/3200Pheathon/p/11615946.html