-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp.c
48 lines (38 loc) · 938 Bytes
/
tsp.c
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
#include <stdio.h>
#define N 4
#define INF 99999
int tsp_g[N][N] = {
{0, 15, 10, 20},
{6, 0, 18, 12},
{7, 14, 0, 28},
{8, 24, 32, 0}
};
int visited[N] = {0};
int cost = 0;
void travelling_salesman(int start) {
int i, j, k, min, next_city;
visited[start] = 1;
printf("Path: %d -> ", start + 1);
j = start;
for (i = 1; i < N; i++) {
min = INF;
for (k = 0; k < N; k++) {
if (!visited[k] && tsp_g[j][k] && tsp_g[j][k] < min) {
min = tsp_g[j][k];
next_city = k;
}
}
visited[next_city] = 1;
printf("%d -> ", next_city + 1);
cost += min;
j = next_city;
}
cost += tsp_g[j][start];
printf("%d\n", start + 1);
}
int main() {
int start = 0;
travelling_salesman(start);
printf("Minimum cost: %d\n", cost);
return 0;
}