-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_approx.cpp
84 lines (73 loc) · 1.7 KB
/
2_approx.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
#include "graph_template.h"
struct TwoApprox : Tour
{
DSU dsu;
vector<vector<int>> adj_list;
vector<Edge> e;
vector<int> pv;
int prev_val;
void dfs (int u, int p)
{
pv[u] = prev_val;
prev_val = u;
for (auto x : adj_list[u])
{
if (x != p)
{
dfs(x,u);
}
}
}
void reconstruct_path()
{
int current_vertex = prev_val;
while (current_vertex >= 0)
{
path.push_back(current_vertex);
current_vertex = pv[current_vertex];
}
path.push_back(prev_val);
}
void gen_MST()
{
ld mst_weight = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
e.push_back(Edge(i, j, adj_matrix[i][j]));
}
}
sort(e.begin(),e.end(), [&](Edge &x, Edge &y)
{
return x.weight < y.weight;
});
for (auto [x,y,w] : e)
{
if (dsu.unite(x,y))
{
mst_weight += w;
adj_list[x].push_back(y);
adj_list[y].push_back(x);
}
}
//~ cout << mst_weight << "\n";
}
TwoApprox (vector<pld> cities, function<ld(pld, pld)> temp_dist) : Tour(cities, temp_dist)
{
dsu = DSU(n);
pv = vector<int> (n);
adj_list = vector<vector<int>> (n);
gen_MST();
prev_val = -1;
dfs(0, -1);
reconstruct_path();
}
};
int main()
{
init("city_list.txt");
TwoApprox g(COORDINATE_LIST, earth_dist);
g.print_path();
return 0;
}