Originally also felt that the 01 backpack is the dynamic planning of the relatively basic part, did not expect to look at a moment to feel good difficult ...
This question is 01Knapsack question, I refer to Hawstein's blog, first to give some examples:
Let me assume that the current backpack capacity is c=10;
Item Number: 1 2 3
Item Weight: 5 6 4
Item Value: 20 10 12
Use V[i] to express the value of the item, W[i] means the weight of the item, to maximize the value of the items put into the backpack, we know that greedy is not possible!
--------------------------------------------------------------------------------------------------------------- ---
So let's move on to the rules:
First define the state Dp[i][j] with J for the capacity for the maximum value of the first I items (according to I from small to large order), then I=1, put the item 1, this time is definitely the best!
Then consider J,j is the current capacity, if j<5, then is not can not put, Dp[1][j] (j<5) = 0; that if j>5, you can put, Dp[1][j] (j>=5) = 20;
Then i=2 put two items, ask is dp[2][j], when j<5, is not the same dp[2][j] (j<5) equals 0; that when the j<6 is not put the second, can only put the first;
What about the j>6? Is it possible to put the second one? Yes, but obviously not the optimal, with the brain to think about it, found Dp[2][j] (j>6) = 20, this 20 how to come, of course, is the former state (note here can be divided into two cases): one is to choose the second item to put, the other or select the previous item;
Let's assume that j=10, it might be better to understand! This time: dp[2][10] = max (dp[1][10-w[2]) +v[2],dp[1][10]);
DP[2][10] = max (dp[1][4]) +10,dp[1][10]);
is not very obvious, dp[1][4]) +10 is the choice of the second, so the capacity is reduced to 4, before the dp[1][4]=0 has been obtained, that is, the selection of goods 2, items 1 can not be selected; dp[1][10] is not the second choice, only select the first dp[1][10] is equal to 20, and then came to dp[2][10]=20;
Here you can, and so on, the dynamic transfer equation is: dp[i][j] = max (Dp[i-1][j-w[i])) +v[i],dp[i-1][j]);
But there seems to be some problems that have not been considered ...
Look back at the example:
Item Number: 1 2 3
Item Weight: 5 6 4
Item Value: 20 10 12
What is the time we know Dp[1][j] (j<5) =20,dp[2][j] (j=5)? We see that the dynamic transfer equation does not consider j<w[i], but we can add it, because dp[2][5] we see is equal to 5, why? Because you can't choose the second one, you can only choose the first one, so ... dp[2][5] is not exactly equal to dp[1][5]! So when J<w[i], dp[i][j] = dp[i-1][j] just fine, isn't it amazing!
1#include <iostream>2 3 using namespacestd;4 5 intw[ the], val[ the];6 intdp[ the][1005];7 8 intMain ()9 {Ten intT, M, res=-1; OneCIN >> T >>m; A for(intI=1; i<=m; i++) -CIN >> W[i] >>Val[i]; - the for(intI=1; i<=m; i++)//Items - for(intj=t; j>=0; j--)//capacity - { - if(J >=W[i]) +DP[I][J] = max (dp[i-1][j-w[i]]+val[i], dp[i-1][j]); - Else +DP[I][J] = dp[i-1][j]; A } atcout << Dp[m][t] <<Endl; - return 0; -}
Sicily 1146 Medicine (01 backpack)