-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtsp.cpp
90 lines (67 loc) · 1.53 KB
/
tsp.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include "mst.cpp"
using namespace std;
vector<Edge> double_tsp(int n, vector<vector<float> >& cost)
{
vector<Edge> tree = kruskal(n, cost);
vector<int> adj[n];
vector<int> ham_tour;
bool visited[n];
stack<int> to_visit;
to_visit.push(0); //first node
for(int i=0; i<(int)tree.size();i++){//N-1
int h = tree[i].head;
int t = tree[i].tail;
adj[h].push_back(t);
adj[t].push_back(h);
}
cout<<"Tour:"<<endl;
//depth first search is enough
while(!to_visit.empty()){
int top = to_visit.top();
to_visit.pop();
visited[top]=true;
cout<<top<<endl;
ham_tour.push_back(top);
for(int i=0;i<(int)adj[top].size();i++){
int cur=adj[top][i];
if(visited[cur]!=true)
to_visit.push(cur);
}
}
return tree;
};
vector<Edge> christo(int n, vector<vector<float> >& cost)
{
vector<Edge> tree = kruskal(n, cost);
vector<int> adj[n];
vector<int> ham_tour;
bool visited[n];
stack<int> to_visit;
to_visit.push(0); //first node
for(int i=0; i<(int)tree.size();i++){//N-1
int h = tree[i].head;
int t = tree[i].tail;
adj[h].push_back(t);
adj[t].push_back(h);
}
cout<<"Tour:"<<endl;
//depth first search is enough
while(!to_visit.empty()){
int top = to_visit.top();
to_visit.pop();
visited[top]=true;
cout<<top<<endl;
ham_tour.push_back(top);
for(int i=0;i<(int)adj[top].size();i++){
int cur=adj[top][i];
if(visited[cur]!=true)
to_visit.push(cur);
}
}
return tree;
};