-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijikstra.cpp
74 lines (69 loc) · 1.41 KB
/
dijikstra.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXNODE 30
#define MAXCOST 1000
int dist[MAXNODE],cost[MAXNODE][MAXNODE],n=5;
void dijkstra(int v0)
{
int s[MAXNODE];
int mindis,dis,i,j,u;
for(i=1;i<=n;i++)
{
dist[i]=cost[v0][i];
s[i]=0;
}
s[v0]=1;
for(i=1;i<=n;i++)
{
mindis=MAXCOST;
for(j=1;j<=n;j++)
{
if(dist[j]<mindis&&s[j]==0)
{
u=j;
mindis=dist[j];
}
}
s[u]=1;
for(j=1;j<=n;j++)
{
if(s[j]==0)
{
dis=dist[u]+cost[u][j];
dist[j]=(dist[j]<dis)?dist[j]:dis;
}
}
}
}
void display_path(int vo)
{
int i;
printf("The shortest path is follow from point %d to other points:\n",vo);
for(i=1;i<=n;i++)
{
printf("<%d> --> <%d> ",vo,i);
if(dist[i]==MAXCOST)
printf("No Path!\n");
else
printf("%d\n",dist[i]);
}
}
int main()
{
int i,j,v0=1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cost[i][j]=MAXCOST;
for(i=1;i<=n;i++)
cost[i][i]=0;
cost[1][2]=10;
cost[1][5]=100;
cost[1][4]=30;
cost[2][3]=50;
cost[4][3]=20;
cost[3][5]=10;
cost[4][5]=60;
dijkstra(v0);
display_path(v0);
}