-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSP.cpp
More file actions
35 lines (26 loc) · 720 Bytes
/
Copy pathTSP.cpp
File metadata and controls
35 lines (26 loc) · 720 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int n = 4;
const int MAX = 1000000;
int dist[n][n] = { { 0, 10, 15, 20 },
{ 10, 0, 25, 25 },
{ 15, 25, 0, 30 },
{ 20, 25, 30, 0 },
};
int tsp(int mask, int pos) {
if (mask == ((1 << n) - 1))
return dist[pos][0];
int res = MAX;
for (int j = 1; j < n; j++) {
if ((mask & (1 << j)) == 0) {
int newMask = mask | (1 << j);
res = min(res, dist[pos][j] + tsp(newMask, j));
}
}
return res;
}
int main() {
int ans = tsp(1, 0);
printf("The cost of most efficient tour = %d\n", ans);
return 0;
}