Multiple knapsack problems (source: About backpack 9)

Source: Internet
Author: User

Problem:

There are N items and a backpack with a capacity of V. A maximum of n [I] items are available for the I-th item. The cost per item is c [I] and the value is w [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.

Basic Algorithms:

This is similar to a full backpack problem. The basic equation only needs to slightly change the equation of the complete backpack problem, because there are n [I] + 1 strategy for the I-th item: Take 0, take 1 ...... N [I] parts. If f [I] [v] indicates that the first I items are placed in the maximum weight of a backpack with a capacity of v, there is a state transition equation: f [I] [v] = max {f [I-1] [v-k * c [I] + k * w [I] | 0 <= k <= n [i]}. The complexity is O (V * Σ n [I]).

Conversion to 01 backpack problems:

Another basic way to write well is to convert it to 01 backpack solution: Replace the I-th item with the item in the n [I] Piece 01 backpack, then, the 01 backpack problem with the number of items is obtained. The problem is solved directly, and the complexity is still O (V * Σ n [I]). But we expect that it will reduce complexity as if it were a full backpack after converting it into a 01 backpack problem. We still consider the binary idea. We want to replace the I-th item with several items, so that in the original question, the I-th item can be used in every strategy -- 0 .. n [I] parts-can be equivalent to several replacement items. In addition, a policy that exceeds n [I] parts cannot appear. The method is to divide the I-th item into several items, with each item having a coefficient. The cost and value of this item are the original cost and value multiplied by this coefficient. Make these coefficients respectively 1, 2, 4 ,..., 2 ^ (k-1), n [I]-2 ^ k + 1, and k is the maximum integer that satisfies n [I]-2 ^ k + 1> 0. For example, if n [I] is 13, the item is divided into four items with a coefficient of 1, 2, 4, and 6. The coefficient of the divided items is n [I], indicating that the I-th item cannot be obtained more than n [I] items. In addition, this method can ensure that 0 .. each integer between n [I] can be expressed by the sum of several coefficients. This proof can be divided into 0 .. 2 ^ K-1 and 2 ^ k .. n [I] is not difficult to discuss separately. I hope you can think about it yourself. In this way, the I-th item is divided into the O (log n [I]) item, and the original problem is converted to the complexity O (V * Σ log n [I]) 01 backpack problem, is a great improvement. The following describes how O (log amount) processes items in multiple backpacks. amount indicates the number of items:

Procedure MultiplePack (cost, weight, amount) if cost * amount> = V CompletePack (cost, weight) return integer k = 1 while k
I hope you will understand this pseudocode carefully. If you do not understand it, you may translate it into program code, and then execute it several times in a single step, or you may understand it slowly by adding a pen or paper in your mind.

O (vn) algorithm:

Multiple knapsack problems also involve O (VN) algorithms. This algorithm is based on the state transition equation of the basic algorithm, but the monotonic queue method is used to make the values of each State be solved at an average O (1) time. Since the DP optimized with monotonous queue is beyond the scope of NOIP, this article will not explain it further.

Summary:

Here we see the process of improving the complexity of an algorithm from O (V * Σ n [I]) to O (V * Σ log n [I, we also know that there is an O (VN) algorithm that applies knowledge beyond the NOIP range. I hope you will pay special attention to the idea and method of "splitting items", prove its correctness, and write the complete program code.

---------------------------------------------------------------------------

So far, I have learned three simple backpack problems and gradually began to understand the concept of backpack problems... Wow

---------------------------------------------------------------------------

In fact, when you learn a new knowledge or skill, you cannot understand it at the beginning. This is normal. It depends on your perseverance and perseverance. As long as you have enough enthusiasm and passion, and full of confidence, you will win it, and there will be a day of dawn ....

---------------------------------------------------------------------------

Exercise:

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.