Backpack nine stresses three: multiple knapsack problem: a finite number of items allowed to choose

There are N kinds of goods and a backpack capacity of V.
Up to the i-th item si pieces, each volume is VI, a value wi.
Which solving the items into the backpack, can not exceed the total volume of the article capacity of the backpack, and the sum of the maximum value.
The maximum output value.
Input Format
The first line of two integers, N, V, separated by spaces, each represent a kind of goods and a backpack volume.
Then there are N rows, each row of three integers vi, wi, si, separated by spaces, respectively, represents the volume of the i-th item, the value and quantity.
Output Format
Output An integer representing the maximum value.
data range
0<N≤1000
0<V≤20000
0<vi,wi,si≤20000
SAMPLE INPUT
4 5
1 2 3
2 4 1
3 4 3
4 5 2
Sample Output
10
Only the equivalent of multiple knapsack problem knapsack problem will be completely in the number of available items by the infinite becomes finite. So before the full knapsack problem the easiest way is to not re-cycle optimization third changed a bit:
for (int k = 1; k <= s[i] && k <= j / v[i]; ++k)
1, binary optimization method, time complexity: o (N * logS * V)
. 1 #include <the iostream>
 2 #include <Vector>
 . 3 #include <algorithm>
 . 4  the using  namespace STD;
 . 5  struct Good {
 . 6      int VI, Wi;
 . 7  };
 . 8  const  int ARRAY_SIZE = 2001 ;
 . 9  int F [ARRAY_SIZE], N, V, V, W, S;
 10 Vector <Good> Vessel;
 . 11  / * 
12 is  binary optimization method
 13 is  V, W, S
 14  each article is split into parts of s, it takes a maximum number of log (s) represented
 15  Example: a total of seven items, it may be 1,2,4 (pow (2, k) ) in combination
 16 Respectively to an article, the article 2, as a group of four articles,
 17  is about seven article is divided into three items, volume and value, respectively:
 18 is  V, W; V 2 * 2 * W;. 4 * v, 4 * w. Translated into 01 knapsack problem.
. 19  * / 
20 is  int main () {
 21 is      CIN N >> >> V;
 22 is      for ( int I = 0 ; I <N; ++ I) {
 23 is          CIN W >> >> >> V S;
 24          for ( int K = . 1 ; K <= S; K * = 2 ) {
 25              S - = K;
 26 is              vessel.push_back (V * {K, K * W});
 27          }
 28         if (s > 0)
29             vessel.push_back({ s * v,s * w });
30     }
31     //01背包问题算法
32     for (Good good : vessel)
33         for (int j = V; j >= good.vi; --j)
34             f[j] = max(f[j], f[j - good.vi] + good.wi);
35     cout << f[V];
36 }
2, queue optimization monotone
NOTE: Monotone Queue: Get the current minimum or maximum sequence length "m" of the sub-sequences. Monotonous increment and decrement the queue only two kinds of queues. Monotone push into the queue is the index value instead of the original sequence.
When push the subscript "i":
(1) When deque.front () <time = im, pop it;
(2) When f [deque.back ()]> f [i], pop, the pop is bigger then the little so far;
(3)push下标"i"。
Finally deque.front () is the current minimum sequence length "m" of the sub-sequences.
Test cases:
10 100
8 7 1
2 2 5
2 2 3
8 6 250
10 7 200
5 2 400
2 1 948
1 1 1
7 4 285
1 1 3
#include <the iostream> 
#include <algorithm>
 the using  namespace STD;
 / * 
F: before the i-th item, backpacks volume 0-V, corresponding to the maximum value 
g: i-1 before one article, backpacks volume 0-V, the corresponding The maximum value 
* / 
const  int ARRAY_SIZE = 20001 ;
 int F [ARRAY_SIZE], G [ARRAY_SIZE], Q [ARRAY_SIZE], N, V, V, W, S;
 int main () { 
    CIN >> N >> V;
     for ( int I = 0 ; I <N; ++ I) { 
        CIN >> >> W >> V S; 
        Copy (F, F + ARRAY_SIZE, G);
         for ( int= J 0 ; J <V; ++ J) {
             int HH = 0 , TT = - . 1 ;
             for ( int K = J; K <= V; = K + V) { 
                F [K] = G [K] ;
                 IF (HH <&& TT = K - V S *> Q [HH])
                     ++ HH; // remove the head of the queue elements, within the current queue sequence header portion is the index of a certain range of the maximum value 
                IF (HH <= TT ) 
                    F [K] = max (F [K], G [Q [HH]] + (K - Q [HH]) / V * W); // with the maximum value update some F [K] 
                the while (HH < = tt && g [q [tt ]] - (q [tt] - j) / v * w <= g [k] - (k - j) / v *W)
                     --tt; // remove the queue will not be used in the element 
                Q [TT ++] = K; // Push new index value 
            } 
        } 
    } 
    COUT << F [V]; 
} 
const  int ARRAY_SIZE = 20001 ;
 int F [ARRAY_SIZE], G [ARRAY_SIZE], N, V, V, W, S; 
the deque < int > Q;
 int main () { 
    CIN >> N >> V;
     for ( int I = 0 ; I <N; ++ I) { 
        CIN >> >> W >> V S;
        Copy (F, F + ARRAY_SIZE, G);
         for ( int J = 0 ; J <V; ++ J) {
             for ( int K = J; K <= V; = K + V) { 
                F [K] = G [K];
                 IF (q.empty () && K - V S *>! q.front ()) 
                    q.pop_front (); // remove the head of the queue element, the current head of the queue portion of a certain range is the maximum sequence subscript 
                IF (! q.empty ()) 
                    F [K] = max (F [K], G [q.front ()] + (K - q.front ()) / V * W); // updating the maximum value at F [K] 
                the while(q.empty () && G [q.back ()] -! (q.back () - J) / V * W <= G [K] - (K - J) / V * W) 
                    q.pop_back (); // removed from a queue will not be used 
                q.push_back (K); // Push new index value 
            } 
        } 
    } 
    COUT << F [V]; 
}

Guess you like

Origin www.cnblogs.com/xiehuazhen/p/12464870.html