01 study notes knapsack problem

Better reading experience

DP knapsack problem dynamic programming algorithm is more classic in a class of models, from time to time in the examination room on the ground NOIP bit, it can not figure out, but once he learned you can in just ten minutes, the cut he, to save time, but once someone heavyweights AC purposes .--

01 backpack Full backpack Multiple backpack Packet backpack Mixed backpack
For an article or only select two cases 0 For unlimited items selected, you can not vote For most items can be selected [i] a from s, also is optional Some bundled items, each group of articles can only select one item 1 can select some items, some items can be selected numerous, some items can be selected s [i] th namely: full backpack 01 + + multiple Backpack backpack.
Scroll array Scroll array Scroll array Scroll array Scroll array
No No Optimization of binary or monotonically queue optimization No Wherein multiple reference multiple backpack backpack portion Optimization

Those are the times of the mind map that learning framework.
First we have to 01 backpack, the basis of all the backpack to begin this learning!

01 backpack

Title links
have \ (N \) items, and a capacity \ (V \) backpack. Each item can only be used once.

The first \ (I \) volume of items is \ (V_I \) , the value is \ (W_i \) .

Which solving the items into the backpack, these items make the total volume does not exceed the capacity of the backpack, and the total value of the maximum.
The maximum output value.

Input format
of the first two integers line, \ (N, V \) , separated by spaces, respectively, and the number of articles backpack volume.

Then there \ (N \) lines of two integers \ (V_I, W_i \) , separated by spaces, each represents \ (I \) the volume and the value of the items.

Output format
output an integer representing the maximum value.

Data range
\ (0 \ lt N, V \ Le 1000 \)
\ (0 \ lt V_I, W_i \ Le 1000 \)

SAMPLE INPUT

4 5
1 2
2 4
3 4
4 5

Sample output:

8
Simple ideas

This question first obvious idea is that for an item, we have only two choices.

  1. You can choose to put his backpack
  2. Not choose him, not into the backpack.
    So a simple idea is, we can use binary enumeration, enumeration of each item, we in the end is to select the item, or do not select this item.
    Time complexity \ (O ( 2 ^ N) \)
Thought DP \ (O (n \ times m ) \)

? For the above mentioned complexity, we obviously are not satisfied, then they're at it, how are we going to optimize it we think he dynamic programming (DP), because This question has several special place :

  1. We choose the first i items, and items for the back of no effect , that no after-effects.
  2. It's very obvious decision, which is our only, select this item or do not choose the items both scenarios.

In summary, we can first think about DP algorithm.
Thinking Process topic so now we will start dynamic programming algorithm.

  1. How to define an array of state
  2. What stage is the
  3. What is the decision-making
  4. What is the boundary
  5. State transition equation
The definition of an array of state

The definition of the status of the array: solving is often subject to our final answer, we can roughly define the status of such an array.
For \ (f [i] [J] \) , it is expressed as, first i select a number of items, capacity has to spend the maximum profit j.
for example, \ (F [. 3] [5] \) represents the previous three items, we select 0 or 1 or 2 or 3, and 5 have been spent the point of maximum profit capacity.
If you feel bad example to explain, you can not see, fear of misunderstanding

stage

According to our state of the array, so we can choose the stage, that is, \ (1 \ Le i \ the n-Le \) , is to the article is a stage.

decision making

Decision obviously, is selected or not selected selected

Boundary conditions

f all the empty array just fine, because we are asking for is to maximize profits.

State transition equation
f[i][j]=max(f[i-1][j],f[i][j-v[i]]+w[i];

\ (f [i-1] [j] \) is the former i-1 th article, take the maximum profit j-th capacity, where we can understand, do not select the i-th article (ie select ago i-1 th ) the maximum profit.
\ (f [i] [jv [i]] + w [i] \) is to select the i-th article, take the maximum profit of the j-th capacity, then why you wrote?
because we want to put into this article, then at least we have to need \ (v [i] \) with a capacity, so we can only \ (jv [i] \) the capacity to push over, or we can not let go of this backpack.
\ (+ w [ i] \) it is clear that select this item, of course, the value of the item you want to add.

Scroll array optimization

Optimization Step:
First, for the status array, we only use the \ (f [i] [j ] \) and \ (f [i-1] [j] \) as shown in the following example.

  1. When i is 1 when we use the \ (f [0] [j ] \) and \ (f [1] [j ] \)
  2. When i is 1 when we use the \ (f [1] [j ] \) and \ (f [2] [j ] \)
  3. When i is 1, when we use the \ (f [2] [j ] \) and \ (f [3] [j ] \)

So in fact, we only need to use \ (f [0] [j ] \) and \ (f [1] [j ] \) just fine, actually represents i-1,1 0 actually represents i.
The second step optimization:
in fact, we do not necessarily from the j \ (V [I],. 1 + V [I], V 2 + [I] ... m \) , but may be from \ (m, m-1. m-2 ... v [i] \) because then, we can from \ (f [2] [n ] \) to \ (f [n] \) a. since the beginning of the second process is not when decisions, \ (f [1] [J] \) is certainly equal to \ (f [0] [J] \) , and then to us from m \ (v [i] \) , start from top to bottom to start the decision-making , you can ignore \ (f [0] \) this dimension of.
Speaking of not very good, it was suggested itself simulate it again = ¯ω¯ =

Specific code implementation
#include <bits/stdc++.h>
using namespace std;
const int N=1100;
const int M=2e5;
int n,m,i,j,k,a[N],f[M];
int main()
{
    ios::sync_with_stdio(false);
    cin>>m>>n;//读入最大容量和物品个数
    for(int i=1;i<=n;i++)
        cin>>a[i]; 
    memset(f,0xcf,sizeof(f));//我这里是初始化为-INF,其实初始化为0就好了.
    f[0]=0;
    for(int i=1;i<=n;i++)//选取i个物品
        for(int j=m;j>=a[i];j--)//容量设定
            f[j]=max(f[j],f[j-a[i]]+a[i]);//转移
    int ans=0;
    for(int i=0;i<=m;i++)//f[n][i]表示选取n个物品,消耗容量为i的最有价值.
        ans=max(ans,f[i]);
    cout<<ans;//最优解
    return 0;
}

Guess you like

Origin www.cnblogs.com/gzh-red/p/11011573.html