Recently encountered DP problem, did not complete, there is a lot of information on the Internet, reproduced this article from: http://blog.csdn.net/libin56842/article/details/9338841
There are n items and a backpack with a capacity of V. The cost of article I is c[i], the value is w[i]. The sum of the values is maximized by solving which items are loaded into the backpack.
As can be seen from this topic, the 01 backpack is characterized by: only one piece of each item, you can choose to put or not put.
Its state transition equation is:
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
For this equation is not difficult to understand, in the equation, now need to place the first item, the volume of the article is c[i], the value is w[i], so f[i-1][v] is not to put this item into the backpack, and F[i-1][v-c[i]]+w[i] It represents the total value of putting the item I into the backpack, comparing the value of the two, and drawing the greatest value into the present backpack.
After understanding the equation, the equation is put into the application of the actual problem, which can be obtained by:
1 for 1; i<=n; i++) 2{ 3for (j = v; j>=c[i]; j--)// Here, the backpack is placed into the item, the capacity is continuously reduced, until it can not be put into the 4 { 5 F[i][v]=max (f[i- 1] [v],f[i-1][v-c[i]]+w[i]); 6 } 7 }
C language • 01 backpack