forked from AutoProving/3TST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt.cpp
350 lines (314 loc) · 11.1 KB
/
opt.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "opt.hpp"
volatile sig_atomic_t tle = false;
void signalHandler(int) { tle = true; }
inline void initPriorityQueue(const Tree &T, const Vertex root,
const vector<map<Vertex, Weight>> &adjList,
vector<Weight> &min_distance,
vector<Vertex> &origin,
set<pair<Weight, Vertex>> &active_vertices) {
queue<Vertex> next;
next.push(root);
while (!next.empty()) {
Vertex current = next.front();
next.pop();
for (map<Vertex, Weight>::const_iterator it = adjList[current].begin();
it != adjList[current].end(); ++it) {
set<Vertex>::const_iterator son =
T.tree[current].children.find(it->first);
if (son != T.tree[current].children.end()) {
next.push(*son);
active_vertices.erase({min_distance[it->first], it->first});
min_distance[it->first] = 0;
origin[it->first] = current;
} else if (min_distance[it->first] > it->second) {
active_vertices.erase({min_distance[it->first], it->first});
active_vertices.insert({it->second, it->first});
min_distance[it->first] = it->second;
origin[it->first] = current;
}
}
}
}
inline Vertex deleteUpPath(Vertex v, const vector<int> &terminalsMap, Tree &T) {
while (T.tree[v].children.empty() && terminalsMap[v] == -1 &&
T.tree[v].parent >= 0) {
Vertex parent = T.tree[v].parent;
T.tree[parent].children.erase(v);
T.tree[v].parent = -2;
v = parent;
}
return v;
}
inline void buildDownPath(const vector<map<Vertex, Weight>> &adjList,
const vector<Weight> &min_distance,
const vector<Vertex> &origin, Vertex cible, Tree &T) {
while (origin[cible] != -1) {
if (T.tree[origin[cible]].parent >= 0) {
T.tree[T.tree[origin[cible]].parent].children.erase(origin[cible]);
}
T.tree[cible].children.insert(origin[cible]);
T.tree[origin[cible]].parent = cible;
if (T.tree[origin[cible]].parent == -2) {
T.tree[origin[cible]].weight =
min_distance[cible] - min_distance[origin[cible]];
} else {
T.tree[origin[cible]].weight = adjList[cible].find(origin[cible])->second;
}
cible = origin[cible];
}
}
inline void buildUpPath(const vector<map<Vertex, Weight>> &adjList,
const vector<Weight> &min_distance,
const vector<Vertex> &origin, Vertex current, Tree &T) {
while (origin[current] != -1) {
if (T.tree[current].parent == origin[current]) {
current = origin[current];
} else {
if (T.tree[current].parent >= 0)
T.tree[T.tree[current].parent].children.erase(current);
T.tree[origin[current]].children.insert(current);
T.tree[current].parent = origin[current];
if (T.tree[current].parent >= 0) {
T.tree[current].weight =
min_distance[current] - min_distance[origin[current]];
} else {
T.tree[current].weight = adjList[current].find(origin[current])->second;
}
current = origin[current];
}
}
}
inline void opt_d3(Vertex root, Vertex v1, Vertex v2,
const vector<map<Vertex, Weight>> &adjList,
vector<Vertex> terminalsMap, Tree &T) {
if (T.tree[v1].parent >= 0) {
T.tree[T.tree[v1].parent].children.erase(v1);
deleteUpPath(T.tree[v1].parent, terminalsMap, T);
}
if (T.tree[v2].parent >= 0) {
T.tree[T.tree[v2].parent].children.erase(v2);
deleteUpPath(T.tree[v2].parent, terminalsMap, T);
}
while (T.tree[v1].children.size() == 1 && terminalsMap[v1] == -1) {
T.tree[v1].parent = -2;
Vertex tmp = v1;
v1 = *T.tree[v1].children.begin();
T.tree[tmp].children.clear();
}
T.tree[v1].parent = -1;
while (T.tree[v2].children.size() == 1 && terminalsMap[v2] == -1) {
T.tree[v2].parent = -2;
Vertex tmp = v2;
v2 = *T.tree[v2].children.begin();
T.tree[tmp].children.clear();
}
T.tree[v2].parent = -1;
// Dijkstra from v1
set<pair<Weight, Vertex>> active_vertices;
vector<Weight> min_distance1(adjList.size(), MAX_WEIGHT);
vector<Vertex> origin1(adjList.size(), -1);
min_distance1[v1] = 0;
initPriorityQueue(T, v1, adjList, min_distance1, origin1, active_vertices);
Vertex c1 = dijkstra(adjList, min_distance1, origin1, T, active_vertices);
// Dijkstra from v2
active_vertices.clear();
vector<Weight> min_distance2(adjList.size(), MAX_WEIGHT);
vector<Vertex> origin2(adjList.size(), -1);
min_distance2[v2] = 0;
initPriorityQueue(T, v2, adjList, min_distance2, origin2, active_vertices);
Vertex c2 = dijkstra(adjList, min_distance2, origin2, T, active_vertices);
// Dijkstra from root
active_vertices.clear();
vector<Weight> min_distance0(adjList.size(), MAX_WEIGHT);
vector<Vertex> origin0(adjList.size(), -1);
min_distance0[root] = 0;
initPriorityQueue(T, root, adjList, min_distance0, origin0, active_vertices);
Vertex c0 = dijkstra(adjList, min_distance0, origin0, T, active_vertices);
Vertex r1;
if (min_distance2[c1] == 0) {
r1 = v2;
} else {
r1 = root;
}
Vertex r2;
if (min_distance1[c2] == 0) {
r2 = v1;
} else {
r2 = root;
}
Vertex r0;
if (min_distance1[c0] == 0) {
r0 = v1;
} else {
r0 = v2;
}
Vertex intersect = -1;
Weight distIntersect = MAX_WEIGHT;
if (r1 == root || r2 == root) {
distIntersect = min_distance1[c1] + min_distance2[c2];
} else if (r0 == v1) {
distIntersect = min_distance0[c0] + min_distance2[c2];
} else {
distIntersect = min_distance0[c0] + min_distance1[c1];
}
for (unsigned int i = 0; i < adjList.size(); ++i) {
if (min_distance2[i] != MAX_WEIGHT && min_distance1[i] != MAX_WEIGHT &&
min_distance0[i] != MAX_WEIGHT &&
min_distance0[i] + min_distance1[i] + min_distance2[i] <
distIntersect) {
intersect = i;
distIntersect = min_distance0[i] + min_distance1[i] + min_distance2[i];
}
}
if (intersect != -1) {
// Build the tree with origin1
buildDownPath(adjList, min_distance1, origin1, intersect, T);
// Build the tree with origin2
buildDownPath(adjList, min_distance2, origin2, intersect, T);
// Build the tree with origin0
buildUpPath(adjList, min_distance0, origin0, intersect, T);
} else {
if (r2 == root) {
// Build the tree with origin1
buildDownPath(adjList, min_distance1, origin1, c1, T);
// Build the tree with origin2
buildDownPath(adjList, min_distance2, origin2, c2, T);
} else if (r1 == root) {
// Build the tree with origin2
buildDownPath(adjList, min_distance2, origin2, c2, T);
// Build the tree with origin1
buildDownPath(adjList, min_distance1, origin1, c1, T);
} else {
// Build the tree with origin1
if (r0 == v1) {
// Build the tree with origin2
buildDownPath(adjList, min_distance2, origin2, c2, T);
// Build the tree with origin1
buildDownPath(adjList, min_distance1, origin1, c0, T);
} else {
// Build the tree with origin1
buildDownPath(adjList, min_distance1, origin1, c1, T);
// Build the tree with origin2
buildDownPath(adjList, min_distance2, origin2, c0, T);
}
// Build the tree with origin0
buildUpPath(adjList, min_distance0, origin0, c0, T);
}
}
}
inline void opt_d3_parent(Vertex node, Tree &T,
const vector<map<Vertex, Weight>> &adjList,
const vector<int> &terminalsMap) {
vector<Vertex> backup_children(T.tree[node].children.begin(),
T.tree[node].children.end());
for (vector<Vertex>::iterator it1 = backup_children.begin();
it1 != backup_children.end(); ++it1) {
if (T.tree[node].parent < 0 || tle)
return;
set<Vertex>::iterator tmp_it = T.tree[node].children.find(*it1);
if (tmp_it == T.tree[node].children.end())
return;
Vertex v1 = *tmp_it;
Vertex v2 = node;
opt_d3(T.root, v1, v2, adjList, terminalsMap, T);
}
}
inline void opt_d3_son(Vertex node, Tree &T,
const vector<map<Vertex, Weight>> &adjList,
const vector<int> &terminalsMap) {
vector<Vertex> backup_children(T.tree[node].children.begin(),
T.tree[node].children.end());
for (vector<Vertex>::iterator it1 = backup_children.begin();
it1 != backup_children.end(); ++it1) {
if (T.tree[node].parent == -2)
return;
for (vector<Vertex>::iterator it2 = it1 + 1; it2 != backup_children.end();
++it2) {
if (tle)
return;
if (T.tree[node].parent == -2)
break;
if (T.tree[node].children.find(*it1) == T.tree[node].children.end())
break;
if (T.tree[node].children.find(*it2) == T.tree[node].children.end())
continue;
opt_d3(T.root, *it1, *it2, adjList, terminalsMap, T);
}
}
}
void apply_opt(Tree &T, const Graph &G) {
vector<Vertex> next_opt;
next_opt.reserve(G.adjList.size());
{
queue<Vertex> next;
next.push(T.root);
// We start the optimisation by the leaves
// Fill next_opt with the order
while (!next.empty()) {
Vertex current = next.front();
next.pop();
for (set<Vertex>::iterator it = T.tree[current].children.begin();
it != T.tree[current].children.end(); ++it) {
next.push(*it);
if (tle)
return;
}
next_opt.push_back(current);
}
}
// Apply optimisation following the next_opt order
for (vector<Vertex>::reverse_iterator it = next_opt.rbegin();
it != next_opt.rend(); ++it) {
if (T.tree[*it].parent > -2) {
// Optimisation for node of degree 3 or more
if (tle)
return;
if (T.tree[*it].children.size() > 1) {
opt_d3_son(*it, T, G.adjList, G.terminalsMap);
}
if (tle)
return;
if (T.tree[*it].children.size() > 2 ||
(T.tree[*it].children.size() == 2 && G.terminalsMap[*it] != -1)) {
opt_d3_parent(*it, T, G.adjList, G.terminalsMap);
}
}
}
}
void full_d3(Tree &T, const Graph &G) {
vector<Vertex> next_opt;
next_opt.reserve(G.adjList.size());
{
queue<Vertex> next;
next.push(T.root);
while (!next.empty()) {
Vertex current = next.front();
next.pop();
for (set<Vertex>::iterator it = T.tree[current].children.begin();
it != T.tree[current].children.end(); ++it) {
if (T.tree[*it].children.size() > 1 || G.terminalsMap[*it])
next_opt.push_back(*it);
next.push(*it);
if (tle)
return;
}
}
}
for (vector<Vertex>::iterator it1 = next_opt.begin(); it1 != next_opt.end();
++it1) {
for (vector<Vertex>::iterator it2 = it1 + 1; it2 != next_opt.end(); ++it2) {
if (tle)
return;
if (T.tree[*it1].parent != -2) {
if (T.tree[*it2].parent != -2) {
if ((T.tree[*it1].children.size() > 1 ||
G.terminalsMap[*it1] != -1) &&
(T.tree[*it2].children.size() > 1 ||
G.terminalsMap[*it2] != -1)) {
opt_d3(T.root, *it1, *it2, G.adjList, G.terminalsMap, T);
}
}
}
}
}
}