This algorithm is poisonous, accidentally stained on the undead also peeling!
In the knapsack problem this to ask the K big value to harass me all day, make me uneasy, feel uncomfortable--!
I see this kind of writing is the original dp[x] plus one dimension to dp[x][y],x to determine the current backpack capacity, Y is the Y value, respectively, the record from the largest to the first K, (because only ask K, so those smaller values will not be recorded)
Next is the discussion of this dp[x][y], which can be understood as It is the value of the first Y when the backpack capacity is X , while dp[x][] can be understood as the value of all backpack capacity X , so it is easy to understand the following.
The 01 knapsack formula used in general knapsack problem solving is aboutDp[x]=max (dp[x],dp[x-v]+w), this is a simplified version, the original two-dimensional switch to one-dimensional, have done 01 of the backpack know, and then add "K value" this condition, then changed into a dp[x][k]={dp[x][1], dp[x-v][1]+w ,dp[x][2], dp[x-v][2]+w, .... , Dp[x][k], dp[x-v][k]+w} Such a collection, do not understand? is for each dp[x][k] can be obtained two values, one is not put the current item s (V,W) <v is the capacity, W is value > put in the value of dp[x][k], and put this item in the value of Dp[x-v][k]+w, the second value of the moral refers toforthe volume after placing this item just to reach The capacity value of the backpack currently under considerationof theBackpackineach of the valuesall put in itemsThe resulting value, because dp[x][] records all the values that a backpack with a capacity of X can get, so understand? Let's draw a picture! I like to use the diagram to speak, I learn the algorithm for all things with the figure is surprisingly fast, quack!
Again, dp[x][] records all the data that can be contained in a backpack with a capacity of X, and retains only the first k when it asks for a large value of K.
Knapsack problem--------max value of K