-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSP1.cpp
More file actions
59 lines (51 loc) · 1.23 KB
/
Copy pathTSP1.cpp
File metadata and controls
59 lines (51 loc) · 1.23 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 8;
const int INF = 1000000;
double dist[MAX][MAX];
int n;
double shortestPath(vector<int>&path, vector<bool>&visited, double currentLength) {
//기저사례 : 경로를 다 찾은 경우 처음으로 돌아가고 종료
if (path.size() == n)
return currentLength;
//+ dist[path[0]][path.back()];
double ret = INF;
for (int next = 0; next < n; next++) {
if (visited[next]) continue;
int here = path.back();
path.push_back(next);
visited[next] = true;
double cand = shortestPath(path, visited, currentLength + dist[here][next]);
ret = min(ret, cand);
visited[next] = false;
path.pop_back();
}
return ret;
}
int main() {
int c;
cin >> c;
while (c--) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> dist[i][j];
}
}
double ret = INF;
for (int i = 0; i < n; i++) {
vector<bool>visited;
for (int j = 0; j < n; j++) visited.push_back(false);
visited.push_back(false);
vector<int>path;
path.push_back(i);
visited[i] = true;
ret = min(shortestPath(path, visited, 0),ret);
}
printf("%.10lf\n", ret);
}
cin >> n;
return 0;
}