C language · 01 Backpack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.