01 backpack Problems

Source: Internet
Author: User

Question:
There are N items and a backpack with a capacity of V. The consumption of item I is c [I], and the value is w [I]. Solving which items are loaded into a backpack can maximize the total value. (Note: each item has only one item. You can choose to put it in a backpack or not)

Problem solving process:
Let's assume that value [I, v] indicates the maximum value that can be obtained by placing the first I item into a backpack with a capacity of v;
So value [I, v] = max (value [I-1, v], value [I-1, v-c [I] + w [I])
This statement can be interpreted as whether to put the I-th item into the backpack, if you do not put item I into the backpack, then the former I-1 items into the capacity of the v backpack (value [I-1, v]), if the first item is placed in a backpack, the first I-1 item can only be placed in a backpack with a capacity of v-c [I] (value: value [I-1, v-c [I] + w [I], w [I] is the value of the I-th item ).
For (I = 1; I <= N; I ++)
For (v = c [I]; v <= V; v ++)
Value [I, v] = max (value [I-1, v], value [I-1, v-c [I] + w [I]);

 

 

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.