-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexact_algo.cpp
More file actions
75 lines (66 loc) · 2.05 KB
/
exact_algo.cpp
File metadata and controls
75 lines (66 loc) · 2.05 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
#include "graph_template.h"
struct ExactSolution : Tour
{
vector<vector<ld>> dist;
vector<vector<int>> prev;
int final_vertex;
void reconstruct_path()
{
int current_vertex = final_vertex;
int current_mask = (1 << n) - 1;
while (current_mask > 0)
{
path.push_back(current_vertex);
int temp_vertex = current_vertex;
current_vertex = prev[current_mask][current_vertex];
current_mask ^= (1 << temp_vertex);
}
path.push_back(final_vertex);
}
ExactSolution (vector<pld> cities, function<ld(pld, pld)> temp_dist) : Tour(cities, temp_dist)
{
dist = vector<vector<ld>> ((1 << n), vector<ld> (n, INF));
prev = vector<vector<int>> ((1 << n), vector<int> (n));
dist[1][0] = 0;
for (int mask = 0; mask < (1 << n); mask++)
{
for (int j = 0; j < n; j++)
{
if (mask & (1 << j))
{
for (int k = 0; k < n; k++)
{
if (j != k && mask & (1 << k))
{
ld new_dist = dist[mask^(1 << k)][j] + adj_matrix[j][k];
if (new_dist < dist[mask][k])
{
dist[mask][k] = new_dist;
prev[mask][k] = j;
}
}
}
}
}
}
ld min_dist = INF;
final_vertex = -1;
for (int i = 0; i < n; i++)
{
if (dist[(1 << n) - 1][i] + adj_matrix[i][0] < min_dist)
{
min_dist = dist[(1 << n) - 1][i] + adj_matrix[i][0];
final_vertex = i;
}
}
path_length = min_dist;
reconstruct_path();
}
};
int main()
{
init("small_city_list.txt");
ExactSolution tour = ExactSolution(COORDINATE_LIST, earth_dist);
tour.print_path();
return 0;
}