Backpack study notes

Foreword: The backpack has been learned countless times, this time I can finally remember something QAQ

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

Backpack is an important and special model in linear DP.

0/1 backpack

Given $ n $ items, the $ i $ item has a volume of $ w_ {i} $ and a value of $ c_ {i} $. Now give you a backpack with a volume of $ m $, how to choose to maximize the total value of the items?

We first consider the naive algorithm.

Let $ f [i] [j] $ be the maximum value obtained by considering the first $ i $ items and selecting the items with a total volume of $ j $ to put in the backpack.

If you do n’t select the $ i $ item, then $ f [i] [j] = f [i-1] [j] $.

If you select the $ i $ item, then $ f [i] [j] = f [i-1] [j-w_ {i}] + c_ {i} (j \ geq w_ {i}) $. Take the maximum of the two.

Consider optimization: because when considering the $ i $ item, it only depends on the situation when considering the $ i-1 $ item, so the first dimension of the $ f $ array above can be omitted.

即$f[j]=max(f[j],f[j-w[i]]+c[i])$。

Please note: $ j $ loops in reverse order. Assuming a positive sequence loop: for example, to update $ jw {i} $ with the situation of $ j-2w_ {i} $, at this time $ jw {i} $ has transitioned to the $ i $ th stage and is recycled to $ jw { When i} $, the situation that "the $ i $ th stage updates the $ i $ th stage" occurs, which violates the principle of linear DP. The reverse order cycle ensures that each item will only be considered once, in line with the principle of linear DP.

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

Complete backpack

It is similar to the 0/1 backpack, except that a condition is added: each item can be selected countless times.

 

The only difference from the 0/1 backpack: $ j $ is a positive sequence cycle. Because each item can be selected countless times.

$f[j]=max(f[j],f[j-w_{i}]+c_{i}$。

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

Multiple backpack

The conditions are different from a full backpack: each item can be selected $ t_ {i} $ times.

The simplest way is of course to loop from $ 0 $ to $ t_ {i} $, and try again in each case. But the complexity is higher.

We can use the binary split method . As we all know, $ 2 ^ 0,2 ^ 1,2 ^ 2, ..., 2 ^ k-1 $ can represent all integers from $ 0 $ to $ 2 ^ k -1 $. So we can use this method to split the items in binary. If $ r_ {i} = c_ {i} -2 ^ 0-2 ^ 1 -...- 2 ^ p $, it can be split into $ p + 2 $ items, and the volume is $ 2 ^ 0 * w_ {i}, 2 ^ 1 * w_ {i}, ..., 2 ^ p * w_ {i}, r_ {i} * w_ {i} $. This method splits the items into $ logc_ {i} $, which is more efficient.

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

Group backpack

Condition changes: There are $ i $ groups of items, each group of items has $ k $ items, and each group can select at most one item for maximum value.

Directly make $ k $ cycle selection, as long as the selection is completed, it will immediately transition from the $ i $ stage to the $ i + 1 $ state.

for (int i=1;i<=n;i++)
    for (int j=m;j>=0;j--)
        for (int k=1;k<=c[i];k++)
            if (j>=v[i][k]) f[j]=max(f[j],f[i-v[i][k])+w[i][k]

 

Note that the $ k $ cycle must be within the $ j $ cycle.

Guess you like

Origin www.cnblogs.com/Invictus-Ocean/p/12694265.html