-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-2.cpp
More file actions
194 lines (192 loc) · 3.72 KB
/
Copy path6-2.cpp
File metadata and controls
194 lines (192 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef int Typew;
typedef int Typep;
class Object{
friend Typep Knapsack(Typew *, Typep *, Typew, int, int *);
public:
int operator <= (Object a) const{
return (d >= a.d);
}
private:
int ID;
float d;
};
class bbnode{
friend class Knap;
friend Typep Knapsack(Typew *, Typep *, Typew, int, int *);
private:
bbnode *parent;
int LChild;
};
class HeapNode{
friend class Knap;
friend class MaxHeap;
public:
operator Typep()const{ return uprofit; };
friend bool operator > (const HeapNode &n1, const HeapNode &n2){
return n1.uprofit<n2.uprofit;
}
private:
Typep uprofit,
profit;
Typew weight;
int level;
bbnode *elemPtr;
};
class Knap{
friend Typep Knapsack(Typew *, Typep *, Typew, int, int *);
public:
Typep MaxKnapsack();
private:
Typep Bound(int i);
void AddLiveNode(Typep up, Typep cp, Typew cw, int ch, int level);
bbnode *E;
Typew c;
int n;
Typew *w;
Typep *p;
Typew cw;
Typep cp;
int *bestx;
priority_queue<HeapNode, vector<HeapNode>, greater<HeapNode> > H;
};
Typep Knap::MaxKnapsack(){
bestx = new int[n + 1];
int i = 1;
E = 0;
cw = 0;
cp = 0;
Typep bestp = 0;
Typep up;
while (i != n + 1) {
Typew wt = cw + w[i];
if (wt <= c){
if (cp + p[i] > bestp)
bestp = cp + p[i];
AddLiveNode(up, cp + p[i], cw + w[i], 1, i);
}
up = Bound(i + 1);
if (up > bestp)
AddLiveNode(up, cp, cw, 0, i);
HeapNode N;
N = H.top();
H.pop();
E = N.elemPtr;
cw = N.weight;
cp = N.profit;
up = N.uprofit;
i = N.level + 1;
}
for (int j = n; j > 0; j--){
bestx[j] = E->LChild;
E = E->parent;
}
return cp;
}
Typep Knap::Bound(int i){
Typew cleft = c - cw;
Typep b = cp;
while (i <= n && w[i] <= cleft){
cleft -= w[i];
b += p[i];
i++;
}
if (i <= n) b += p[i] / w[i] * cleft;
return b;
}
void Knap::AddLiveNode(Typep up, Typep cp, Typew cw, int ch, int level){
bbnode *b = new bbnode;
b->parent = E;
b->LChild = ch;
HeapNode N;
N.uprofit = up;
N.profit = cp;
N.weight = cw;
N.level = level;
N.elemPtr = b;
H.push(N);
}
Typep Knapsack(Typew *w, Typep *p, Typew c, int n, int *bestx){
Typew W = 0;
Typep P = 0;
Object *Q = new Object[n];
int i;
for (i = 1; i <= n; i++){
Q[i - 1].ID = i;
Q[i - 1].d = 1.0*p[i] / w[i];
P += p[i];
W += w[i];
}
if (W <= c){
for (int i = 1; i <= n; i++)
bestx[i] = p[i];
return P;
}
for (i = 1; i<n; i++)
for (int j = 1; j <= n - i; j++){
if (Q[j - 1].d < Q[j].d){
Object temp = Q[j - 1];
Q[j - 1] = Q[j];
Q[j] = temp;
}
}
Knap K;
K.p = new Typep[n + 1];
K.w = new Typew[n + 1];
for (i = 1; i <= n; i++){
K.p[i] = p[Q[i - 1].ID];
K.w[i] = w[Q[i - 1].ID];
}
K.cp = 0;
K.cw = 0;
K.c = c;
K.n = n;
Typep bestp = K.MaxKnapsack();
for (i = 1; i <= n; i++)
bestx[Q[i - 1].ID] = K.bestx[i];
delete[] Q;
delete[] K.w;
delete[] K.p;
delete[] K.bestx;
return bestp;
}
int main(){
int N,bestp;
Typew c;
cin>>N>>c;
int *bestx = new int [N+1];
Typep *p = new Typep[N+1];//{ -1, 6, 4, 7, 4 }
Typew *w = new Typew[N+1];//{ -1, 2, 3, 5, 2 }
for (int i=1;i<=N;i++)
cin>>p[i]>>w[i];
bestp = Knapsack(w, p, c, N, bestx);
cout << "Total number of items = " << N << ",The capacity of knapsack = " << c << endl;
int i;
for (i = 1; i <= N; i++){
if (i == 1) cout << "Array of weight:";
cout << w[i];
if (i != N) cout << ",";
else
cout << endl;
}
for (i = 1; i <= N; i++){
if (i == 1) cout << "Array of price:";
cout << p[i];
if (i != N) cout << ",";
else
cout << endl;
}
for (i = 1; i <= N; i++)
{
if (i == 1) cout << "Whether to select:";
cout << bestx[i];
if (i != N) cout << ",";
else
cout << endl;
}
cout << "The best price of the knapsack:" << bestp << endl;
return 0;
}