Four backpack nine stresses: Mixed knapsack problem: 01, complete, multi-mixed

There are N kinds of goods and a backpack capacity of V.
There are three categories of goods:
* The first article can only be used once (backpack 01);
* The second type article can be unlimited (full backpack);
* The third most items can only be used once si (multiple backpack);
Each volume is vi, the value is 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.
* Si = -1 denotes the i-th item can only be used once;
* Si = 0 indicates the i-th item may be unlimited;
* Si> 0 indicates the i-th sub-si article may be used;
Output Format
Output An integer representing the maximum value.
data range
0<N,V≤1000
0 <we, wi≤1000
−1≤si≤1000
SAMPLE INPUT
4 5
1 2 -1
2 4 1
3 4 0
4 5 2
Sample Output
8
 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 const int array_size = 1001;
 6 int f[array_size], N, V, v, w, s;
 7 struct goods {
 8     int v, w, s;
 9 };
10 vector<goods> vessel;
11 int main() {
12     cin >> N >> V;
13     for (int i = 0; i < N; ++i) {
14          CIN W >> >> >> V S;
 15          IF (S <= 0 )
 16              vessel.push_back (Goods {V, W, S}); // Push into 01 knapsack problem knapsack problem and complete items 
. 17          the else {
 18 is              for ( int K = . 1 ; K <= S; K * = 2 ) { // the multiple knapsack problem Switch 01 then push into the knapsack problem 
. 19                  S - = K;
 20 is                  vessel.push_back (Goods {K * V , W * K, - . 1 });
 21 is              }
 22 is              IF (S> 0 )
 23 is                 vessel.push_back(goods{ s * v,s * w,-1 });
24         }
25     }
26     for (goods g : vessel) {
27         if (g.s == -1)  //01背包问题
28             for (int i = V; i >= g.v; --i)
29                 f[i] = max(f[i], f[i - g.v] + g.w);
30         else            //完全背包问题
31             for (int i = g.v; i <= V; ++i)
32                 f[i] = max(f[i], f[i - g.v] + g.w);
33     }
34     cout << f[V];
35 }

Guess you like

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