背包问题非常适合动态规划的初学者。
Knapsack problem is very suitable for dynamic programming(DP) beginners.
首先我们适当讨论下动态规划。采用动态规划方法解决最优化问题的两个基本要素,是最优子结构和重叠子问题。
First of all, let's talk something about DP. The optimal substructure and overlapping sub-problems are the two basic elements to solve the optimization problem by DP method.
如果一个问题最优解包含了子问题的最优解,称其具有最优子结构。DP以自底向上的方法利用最优子结构,即先找子问题最优解,再找当前问题最优解。问题解代价为子问题代价与转移代价。DP与贪心的区别在于,DP根据子问题最优解进行转移,而贪心直接转移,属于自顶向下的方式。另外,子问题间要相互独立,即所谓的“无后效性”,否则无法计算子问题,也谈不上求解当前问题。
If a question's optimal solution needs its sub-optimal solutions, we call it has an optimal substructure. DP utilizes it by bottom-up method, which means finding sub-problems' optimal solutions firstly, then itself. The cost is the sum of sub-problems' cost and transferring. The difference between DP and greedy is, the former one transfers on the