Backpack dynamic programming

Knapsack problem dynamic programming is a classic problem

The main problem which the frame: a backpack volume V (spending limit), there are n items, the volume of the i-th items of V [i], the value of W [i], and asked how the maximum value of the discharge.

Of course, a different question items will have different restrictions, such as restrictions on the number of items, restrictions on the relationship between the items, so there will be a different kind of knapsack problem.


First, the 01 backpack

Problems: There is a volume backpack V, there are n items, the volume of the i-th items of V [i], the value of W [i], each item select up once asked without exceeding backpack volume the maximum value can be obtained.

This is the basis of dynamic programming backpack, backpack and some type of dynamic programming can be converted into 01 backpack, which play optimal results.

Is characterized for the first i items, we have only two options, the first one of these items into the backpack, second, and hold such items, obviously, we want to get the most value in the current volume limit If placed in this article

The total value will increase, we will put these items, or hold.

State transition equation dp [i] [v] = max (dp [i-1] [v], dp [i-1] [vw [i] + c [i]]);

1 for(int i=1;i<=n;i++)
2     for(int v=1;v<=vmax,v++)
3     {
4         if(w[i]>v) dp[i][v]=dp[i-1][v];
5         else dp[i][v]=max(dp[i-1][v],dp[i-1][v-w[i]]+c[i]);
6      }

We can optimize away by reusing one-dimensional array, thereby optimizing space in the 01 backpack.

Note that one-dimensional volume 01 backpack needs flashback enumeration, because each item only once, if the positive sequence enumeration, may lead to a certain maximum value is determined by the volume of items that have been updated maximum value transferred from , resulting in

The repeated discharge of the items.

1 for(int i=1;i<=n;i++)
2     for(int v=vmax;v>=w[i];v--)
3         dp[v]=max(dp[v],dp[v-w[i]]+c[i]);

Second, completely backpack

Problems: There is a volume backpack V, there are n items, the volume of the i-th items of V [i], the value of W [i], each item select up numerous asked without exceeding backpack volume under the maximum value can be obtained.

We just discussed the issue volume must flashback enumerated in the 01 backpack from two-dimensional to one-dimensional optimization, when, and why, because in order to prevent duplication of release, and completely put the backpack allow repetition, so you can change the positive sequence enumeration .

State transition equation dp [i] [v] = max (dp [i-1] [v], dp [i] [vw [i]] + c [i]);

 

1 for(int i=1;i<=n;i++)
2     for(int v=w[i];v<=vmax;v++)
3         dp[v]=max(dp[v],dp[v-w[i]]+c[i]);

 

A greedy tips: because complete backpack is no limit to the number of items, so for a two items i, j, if w [i] <= w [j], c [i]> = c [j]; this j items we can lose, because obviously i relatively inexpensive items,

In any case, select items better than i j selected items, because of its small cost, the value of dedication also greater, then the data for this optimization method is very effective.

 


 

 

Third, multiple backpack

Problems: There is a volume backpack V, there are n items, the volume of the i-th items of V [i], the value of W [i], the number of pieces of T [i], Q without exceeding backpack volume the maximum value can be obtained.

First, think of a practice violence, we change what 01 backpack, just add a layer to enumerate circulating about the number, it is not possible to solve this problem.

State transition equation of violence: dp [i] [v] = max (dp [i-1] [v], dp [i-1] [vw [i] * k] + k * c [i], 1 < = k <= t [i]);

 

 1 for(int i=1;i<=n;i++)
 2     for(int v=1;v<=vmax;v++)
 3     {
 4         if(w[i]>v) dp[i][v]=dp[i-1][v];
 5         else
 6         {
 7             for(int k=1;k<=t[i];k++)
 8                 dp[i][v]=max(dp[i-1][v],dp[i-w[i]*k]+k*c[i]);
 9         }
10     }

 

Similarly, such an approach may be a one-dimensional optimization

 

1 for(int i=1;i<=n;i++)
2     for(int v=vmax;v>=0;v--)
3         for(int k=1;k<=t[i]&&k*w[i]<=vmax;k++)
4             dp[v]=max(dp[v],dp[v-w[i]*k]+c[i]*k);

 

Because more than one cycle, so the time complexity, multiple backpack that violence is not necessarily good, we can use two methods to optimize multiple backpack.

1, binary Optimized

 

Guess you like

Origin www.cnblogs.com/Hoyoak/p/11370245.html