Backpack problems (01 backpack, full backpack, multiple backpacks)

Source: Internet
Author: User
Preface

Recently, the following things are annoying:

I almost had to submit a project application to the school. I originally wanted to create a multi-mode IM system. However, compared with projects that passed the review in the past, there was no innovation or research value, therefore, you need to do more work in the document.

  • This is the case for a large number of jobs in each phase.
  • The open source code of directui has never been improved.
  • Prepare for the software design competition at the end of May.
  • A good dish for Warcraft.
  • Empty and lonely, looking for a girlfriend...
About a backpack. 01 backpack

 

The Lich King of the undead sent a salary pull, and the Death Knight received a N yuan bill (remember, there was only one bill). To prevent him from dying frequently during the battle, he decided to buy some items for him, so he came to the dejing commodity store.

Death Knight: "I want to buy items! "

Taobao Merchants: "We have three items here: one for 150 blood bottles, one for 200 magic medicines, and one for 350 for invincible potions ."

Death Knight: "OK, give me a blood bottle ."

Then he took out the n-dollar bill and handed it to the goblin merchant.

Taobao Merchants: "I forgot to remind you that we don't have the habit of looking for money here. We accept a tip for a lot of money ."

Death Knight :"......Your sister"

The Death Knight thought, instead of sending money as a tip to him, it would be better to buy more items on his own. He would like to buy them all in the future and put them at home early, but try to make him as little tip as possible.

Now, the Death Knight hopes that you can help him calculate the minimum tip he wants to tip the goblin merchant.

The above is a 01 backpack problem. The problem above can be described as follows:

There are n items. The weight of each item is weight [I], and the value of each item is value [I]. There is a backpack that can hold the total weight. Q: What is the maximum value that your backpack can take when you face so many valuable items?

Idea: each item is nothing more than a backpack or a backpack, so one item is put into a backpack one by one. Yes

Tab [I] [J] = max (Tab [I-1] [J-weight [I] + value [I], Tab [I-1] [J]) ({I, j | 0 <I <= N, 0 <= j <= total })

Where I indicates placing the I-th item, and J indicates the weight of the backpack, then tab [I-1] [J-weight [I] + value [I] indicates to put the item I, there will be questions at the beginning of contact, tab [I-1] [J-weight [I] This value can be understood as follows: tab [I-1] [J] is the best value for mounting an item to the backpack J Capacity, If I requireThe value of putting the current I item in J CapacityIs it necessary to first obtain the value when the capacity is (J-weight [I]), that is, first obtainTab [I-1] [J-weight [I]So tab [I-1] [J-weight [I] + value [I] is the value of putting item I; tab [I-1] [J] is not placed in the I-th item.

The Thinking of dynamic planning is embodied here, that is, Tab [I-1] [J] is the optimal solution of tab [I] [J] (I think the above ideas are easier to understand ).

Example: five items (weight, value): (), (), (6 ).

Backpack capacity

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

5 items

0

0

0

0

0

0

6

12

12

15

15

18

22

22

25

25

4 items

0

0

3

3

3

3

3

12

12

15

15

18

22

22

25

25

3 items

0

0

0

0

0

0

0

12

12

15

15

15

22

22

22

22

2 items

0

0

0

0

3

12

12

12

12

15

15

15

15

15

15

15

1 item

0

0

0

0

0

12

12

12

12

12

12

12

12

12

12

12

There are:

For I = [weight [0], total] tab [n-1] [I] = weight [0]; // n indicates the number of items for I = [1, n) for J = [weight [I], total] tab [n-i-1] [J] = max (Tab [n-I] [J-weight [I] + value [I], tab [n-I] [J])/* print tab [0] [total] */
Full backpack

The Lich King of the undead sent a salary pull, and the Death Knight received a N yuan bill (remember, there was only one bill). To prevent him from dying frequently during the battle, he decided to buy some items for him, so he came to the dejing commodity store.

Death Knight: "I want to buy items! "

Goblin Merchants: "We have three kinds of items here. There are no limit to 150 blood bottles, 200 magic medicines, and 350 invincible potions ."

Death Knight: "OK, give me a blood bottle ."

Then he took out the n-dollar bill and handed it to the goblin merchant.

Taobao Merchants: "I forgot to remind you that we don't have the habit of looking for money here. We accept a tip for a lot of money ."

Death Knight: "...... your sister"

The Death Knight thought, instead of sending money as a tip to him, it would be better to buy more items on his own. He would like to buy them all in the future and put them at home early, but try to make him as little tip as possible.

Now, the Death Knight hopes that you can help him calculate the minimum tip he wants to tip the goblin merchant.

The above World of Warcraft scenario descriptions keep up with only a small difference, that is, one item changes to an infinite number, which is a complete backpack problem. A complete backpack problem can be described as follows:

There are n kinds of items, each of which has an infinite number. The weight of each item is weight [I], and the value of each item is value [I]. There is a backpack that can hold the total weight. Q: What is the maximum value that your backpack can take when you face so many valuable items?

With the style of the above 01 backpack, this question will become much easier to understand, but this style requires a small change. 01 A backpack is operated on a two-dimensional array to prevent an item from being placed multiple times. Therefore, one-dimensional arrays can satisfy the problem of full backpacks. As follows:

Tab [J] = max (Tab [J-weight [I] + value [I], Tab [J]); ({I, j | 0 <I <= N, 0 <= j <= total })

The same is true, but items can be placed multiple times.

Backpack capacity

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

I items

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

There are:

for i=[0,n)    for(j=weight[i]; j<=total; j++)        tab[j] = max(tab[j-weight[i]]+value[i],tab[j])/*    print tab[0][total]    */ 
Multiple backpacks

The Lich King of the undead sent a salary pull, and the Death Knight received a N yuan bill (remember, there was only one bill). To prevent him from dying frequently during the battle, he decided to buy some items for him, so he came to the dejing commodity store.

Death Knight: "I want to buy items! "

Goblin merchant: "We have three items here: 150 blood bottles, 200 magic medicines, 350 B magic potions, and invincible potions ."

Death Knight: "OK, give me a blood bottle ."

Then he took out the n-dollar bill and handed it to the goblin merchant.

Taobao Merchants: "I forgot to remind you that we don't have the habit of looking for money here. We accept a tip for a lot of money ."

Death Knight: "...... your sister"

The Death Knight thought, instead of sending money as a tip to him, it would be better to buy more items on his own. He would like to buy them all in the future and put them at home early, but try to make him as little tip as possible.

Now, the Death Knight hopes that you can help him calculate the minimum tip he wants to tip the goblin merchant.

The above World of Warcraft scenario description has a small difference with the above, that is, one item has changed to a limited one, and the problem has become a problem of multiple backpacks.

There are n kinds of items, each of which has amount [I], the weight of each item is weight [I], and the value of each item is value [I]. There is a backpack that can hold the total weight. Q: What is the maximum value that your backpack can take when you face so many valuable items?

Multiple and completely closer to each other. When the quantity limit is exceeded, a Count [N] Count array is used to limit the number of item I. When the I-th item is the optimal value, Count [I] = count [I-weight [I] + 1; this is because, the operation for placing the I-th item is based on count [I-weight [I]. Therefore, when count [I-weight [I]> = amount [I, it is necessary to prevent putting in even if the I-th item is a superior value. There are:

For I = [0, N)/* clears the Count array */For (j = weight [I]; j <= total; j ++) if Count [I-weight [I] <amout [I] tab [J] = max (Tab [J-weight [I] + value [I], tab [J]); if tab [J] = tab [J-weight [I] + value [I] // It is better to solve the problem of putting I into the table. Count [I] = count [I-weight [I] + 1 else if tab [J] = 0 // prevent installation of 1st items and other items tab [J] = tab [J-1], count [J] = count [J-1] else count [J] = count [J-1]/* print tab [0] [total] */
Summary

The summary is above.

 

Attachment:

Question --01}-- .rar

 

Sunday, May 06,201 2

Spoof http://daoluanxiaozi.cnblogs.com/

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.