-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyshortpath.c
78 lines (61 loc) · 919 Bytes
/
myshortpath.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
#include<stdio.h>
int arcs[10][10];
int n = 0;
int D[10];
int Used[10];
int v, w;
int shortpath()
{
for (int i = 0; i < n; i++)
{
Used[i] = 0; D[i] = arcs[0][i];
}
D[0] = 0; Used[0] = 1;
for (int j = 1; j < n; j++) //control cycle's count
{
int min = 100000;
for (int w = 0; w < n; w++)
{
if (!Used[w])
{
if (D[w] < min)
{
v = w; min = D[w];
}
}
}
Used[v] = 1;
for (int k = 0; k < n; k++)
{
if (!Used[k] && (min + arcs[v][k] < D[k]))
{
D[k] = min + arcs[v][k];
}
}
}
}
int main()
{
n = 6;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arcs[i][j] = 100000;
}
}
arcs[0][2] = 10;
arcs[0][4] = 30;
arcs[0][5] = 100;
arcs[1][2] = 5;
arcs[2][3] = 50;
arcs[3][5] = 10;
arcs[4][3] = 20;
arcs[4][5] = 60;
shortpath();
for (int k = 0; k < n; k++)
{
printf("D[%d] = %d\n", k, D[k]);
}
system("pause");
}