DP Backpack Basics

Source: Internet
Author: User

P01 01 Backpack

topics

There are n items and a backpack with a capacity of V. The cost of article I is c[i], the value is w[i]. The sum of the values is maximized by solving which items are loaded into the backpack.

Basic Ideas

The equation of state transfer is:

f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}

This equation is very important, and basically all the equations for knapsack-related problems are derived from it. So it is necessary to explain it in detail: "Put thefirst I item into a backpack with a capacity of V" This sub-problem is equivalent to not put article I items, then the problem is converted to "the first I-1 items into the backpack of the capacity V", the value of f[i-1][v]; Put the article I item, then the problem is converted to " The first I-1 items are placed in the rest of the v-c[i] backpack ", the maximum value that can be obtained at this time is f[i-1][v-c[i]", plus the value obtained by placing the article I item w[i].

Attention:

1. If the requirement is exactly full pack, then at initialization, except f[0] 0 other F[1..V] are set to-∞, so that the resulting f[n] is an exact full pack of the best solution.

2. If it is not required to fill the backpack, but only want the price as large as possible, the initialization should be F[0..V] all set to 0.

3. The basis of all knapsack problems. P02 Full Backpack

topics

There are n items and a backpack with a capacity of V, with unlimited pieces available for each item. The cost of item I is c[i] and the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

Basic Ideas

The problem is very similar to the 01 knapsack problem , and the difference is that there are unlimited pieces for each item. That is, from the point of view of each item, the strategy associated with it has not taken or not taken two kinds, but take 0 pieces, take 1 pieces, take 2 pieces ... And so many kinds. If you still follow the idea of 01 knapsack, make F[i][v] indicate that the first I item fits into a backpack with a capacity of V maximum weight. The state transfer equation can still be written according to the different strategies of each item, like this:

f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k*c[i]<=v}   k为该物品件数;

Convert to 01 backpack


P03 Multiple Backpack

topics

There are n kinds of goods and a backpack with a capacity of V. (i) Items with a maximum of n[i] pieces available, each cost is c[i], the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

basic Algorithms

This topic is very similar to the complete knapsack problem. Because there are n[i]+1 strategies for article I items: take 0 pieces, take 1 pieces ... Take n[i] pieces. F[I][V] indicates that the first I item fits into the maximum weight of a backpack with a capacity of V, then there is a state transition equation:

f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k<=n[i]}   k为该物品件数;


modified on the basis of a complete backpacknot to be continued ...

DP Backpack Basics

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.