P05: Two-Dimensional knapsack problems

Source: Internet
Author: User
P05: Two-Dimensional bag Problems

The two-dimensional bag problem refers to: for each item, there are two different charges; the two costs must be paid at the same time when this item is selected; there is a maximum charge (Backpack capacity) for each price ). Ask how to select an item to maximize the value. Set these two costs to price 1 and price 2, respectively. The two costs required for item I are a [I] and B [I]. The maximum value (two types of backpack capacity) can be paid at two costs: V and U. The value of an item is W [I].

Algorithm

The fee is added to one dimension. You only need to add one dimension to the status. If f [I] [V] [u] is set, it indicates the maximum value that can be obtained when the cost of the first I item is V or U. The state transition equation is:

F [I] [V] [u] = max {f [I-1] [V] [u], f [I-1] [V-A [I] [U-B [I] + W [I]}

As mentioned above, only two-dimensional arrays can be used: when each item can only be retrieved once, the V and U variables adopt a backward loop, when an item is like a full backpack, use a sequential loop. Split an item when there are multiple backpack problems. Here we will not give any more false information.CodeI believe that with the Foundation above, you can achieve this problem on your own.Program.

Limit on the total number of items

Sometimes, the "two-dimensional fee" condition is given in an implicit way: a maximum of m items can be obtained. In fact, this is equivalent to an additional "number of items" for each item. The cost of each item is 1, and the maximum number of items that can be paid is M. In other words, if f [v] [m] is set, it indicates the maximum value that can be obtained when the cost is paid V and the maximum number of M parts is selected. Then, according to the type of the product (01, full, multiple) update cyclically using different methods, and then in F [0 .. v] [0 .. m.

Backpack problems in the complex field

Another way of thinking is to look at the problem of two-dimensional backpacks as a problem in the complex field. That is to say, the capacity of the backpack and the cost of each item are all one complex. The common one-dimensional backpacking problem is a real-world backpacking problem. (Note: The above statements are not rigorous, because in fact we only process integers .) Therefore, the thought methods of one-dimensional backpacks can often be used to solve the problem of two-dimensional backpacks, because the number of fields is extended.

As an exercise of this idea, you can try to extend the "subset and problem" mentioned in P11 to the complex field (two-dimensional) and try to solve it with the same complexity.

Summary

When a problem is found to be deformed by a familiar dynamic planning question, adding a latitude to the original state to meet new restrictions is a common method. I hope you will first understand this method.

 

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.