Backpack finishing (01 backpacks full backpack, multiple backpack, backpack group) (to be updated)

01 backpack

There are N items V and a capacity of the backpack. I-price items (i.e. volume, the same below) is W [i], is the value of c [i]. Solving the items into a backpack which allows the total cost of these items does not exceed the capacity of the backpack, and the sum of the maximum value.

For each item, we have two choices: put the items into the backpack still hold.
D [i] [v] denotes the front i items, the total volume of exactly placed when the value of v backpack.
State transition equation \ [d [i] [v ] = \ max (d [i - 1] [v], d [i - 1] [v - w [i]] + c [i]) \]

Optimization (space)

\ [D [v] = \ max (d [v], d [v - w [i]] + c [i]) (reverse enumeration) \]

Full backpack

There are N items V and a capacity of the backpack. Price of the i-th article (i.e. volume, the same below) is W [i], is the value of C [i], an infinite number of each item. Solving the items into a backpack which allows the total cost of these items does not exceed the capacity of the backpack, and the sum of the maximum value.

With F [i] [j] denotes the i-front load capacity backpack backpack j can be obtained as the maximum value
for an article, only two cases
  Case 1: i-th element into the hold, then the resulting value is: \ [f [i-1] [v] \]
  case 2: the i pieces into them, then we need to enumerate how many pieces into them, to K, the resulting value is: \ [f [I- 1] [vK * c [i
]] + K * w [i] \] state transition equation is: \ (\ max \ limits_ {0 <= K <= V / W [I]} F [I-. 1] [vK * w [i]] + K * c [i] \)

//最暴力做法
for(int i = 1; i <= n; i++)
    for(int v = V; v >= w[i]; v--)
        for(int k = 1; k <= v/w[i]; k++){
            d[i][v] = max(d[i - 1][v - w[i]*k]+c[i]*k,d[i][v]);
        } 

optimization

\ [d [v] = max (d [v], d [v - w [i]] + c [i]) ( positive sequence enumeration / 1 cycle for each item) \]
can understand how the inductive

Multiple backpack

Multiple backpack into the backpack That would have 01 of each item has a finite number, we put the demolition of a limited number of items into a finite number of different items. 01 and then converted into a backpack.

    //a[i]是拆开后第i件物品本来的序号
    int cnt = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= num[i]; j++){
            a[++cnt] = i;
        }

optimization

Consider a binary split

Packet backpack

There are N items V and a capacity of the backpack. I-price items (i.e. volume, the same below) is W [i], is the value of c [i]. The N items divided into several groups, each group of goods which can not be simultaneously selected.
Solving the items into a backpack which allows the total cost of these items does not exceed the capacity of the backpack, and the sum of the maximum value.

Guess you like

Origin www.cnblogs.com/FoxC/p/11279841.html