-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortestPath.c
81 lines (67 loc) · 1.43 KB
/
ShortestPath.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
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
#include <stdio.h>
#define MAX 100
int dist[6], found[6], via[6];
int n = 6;
int cost[6][6] = {
{0, 50, 10, MAX, 45, MAX},
{MAX, 0, 15, MAX, 10, MAX},
{20, MAX, 0, 15, MAX, MAX},
{MAX, 20, MAX, 0, 35, MAX},
{MAX, MAX, MAX, 30, 0, MAX},
{MAX, MAX, MAX, 5, MAX, 0}};
int select()
{
int min = MAX, minPos = -1;
for (int i = 0; i < n; i++)
if (dist[i] < min && !found[i])
{
min = dist[i];
minPos = i;
}
return minPos;
}
void printVia(int i)
{
int j = via[i];
if (via[j] != -1)
printVia(j);
printf("V%d ", j);
}
void printRes()
{
for (int i = 0; i < n; i++)
if (dist[i] != MAX && dist[i] != 0)
{
printf("%d : V0 ", dist[i]);
if (via[i] != -1)
printVia(i);
printf("V%d \n", i);
}
else
printf("V%d access impossible\n", i);
}
int main(void)
{
for (int i = 0; i < n; i++)
{
found[i] = 0;
dist[i] = cost[0][i];
via[i] = -1;
}
found[0] = 1;
dist[0] = 0;
int x;
for (int i = 0; i < n - 1; i++)
{
x = select();
found[x] = 1;
for (int j = 0; j < n; j++)
if (!found[j])
if (dist[x] + cost[x][j] < dist[j])
{
dist[j] = dist[x] + cost[x][j];
via[j] = x;
}
}
printRes();
}