01 backpack TEMPLATE 1 2 Summary

Source: Internet
Author: User

Item quality W [0] W [1] W [2] ...... W [N] backpack capacity C t

Item value V [0] V [1] V [2] ...... V [N] item type N

 

(1) set dp (x, y)First x itemsLoadY-sized backpackThe maximum value of an item.

First x items (0 ---- X-1, x) to installBackpack with size of Y

 

| -------- 0 when x = 0 or Y = 0

DP (x, y) | dp (x-1, Y) without the X

| Dp (x-1, Y-W [x]) + V [x] Y-W [x]> = 0 x

 

 

 

 

 

 

 

 

 

(2) Set dp (x, y)Item X startsToItem nLoading an itemY-sized backpack.

SlaveItem XOpenStart(X, x + 1, x + 2, x + 3,... n)

 

 

 

 

 

| -------- 0 when x = m + 1 or Y = 0

DP (x, y) | dp (x + 1, Y) without the X

| Dp (x + 1, Y-W [x]) + V [x] Y-W [x]> = 0 x

 

 

 

Idea 1 of Dynamic Planning Algorithm:

M [I] [J] is used to represent the maximum value of a backpack loaded with J in the former I item area.

Where the range of I is 0 to n, where the range of J is 0 to C, and the solution to the program is m [N] [C].

There are three possible situations:

① M [0] [J]: The value of all J values is 0 (the item type is 0 ), M [I] [0] All I values are 0 (volume is 0)

② When the current volume J is greater than or equal to W [I], M [I] [J] is the maximum of the following two quantities: M [I-1] [J] (not loaded) and M [I-1] [J-W [I-1] + V [I] (assembly)

③ When the current volume J is less than W [I], M [I] [J] is equal to M [I-1] [J]

 

 

 

 

 

 

 

 

 

 

 

Dynamic Planning Algorithm concept 2:

Set M [I] [J] to indicate the maximum value obtained by loading an item into a J-sized backpack from item I to item n.

The range of I is 0 to n-1, where the range of J is 0 to C, and the solution to the program is m [0] [C].

It can be divided:

① M [n-1] [J] When J> = 0 and j <W [n-1] is equal to 0 (cannot be loaded); otherwise, it is equal to V [n-1] (can be installed)

② When the current volume J is greater than or equal to W [I], M [I] [J] is the maximum of the following two quantities: M [I + 1] [J] and M [I + 1] [J-W [I] + V [I]

③ When the current volume J is less than W [I], M [I] [J] equals m [I + 1] [J]

 

 

 

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.