-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdijkstra.c
46 lines (46 loc) · 929 Bytes
/
dijkstra.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
#include <stdio.h>
void dij(int n,int source,int cost[10][10],int dist[])
{
int i,u,count,w,visited[10],min;
for(i=1;i<=n;i++)
{
visited[i]=0;
dist[i]=cost[source][i];
}
visited[source]=1;
dist[source]=0;
count=2;
while(count<=n)
{
min=999;
for(w=1;w<=n;w++)
if(dist[w]<min && !visited[w])
min=dist[w],u=w;
visited[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !visited[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,source,i,j,cost[10][10],dist[10];
printf("Enter the number of nodes\n");
scanf("%d",&n);
printf("Enter the cost matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
printf("Enter the source node\n");
scanf("%d",&source);
dij(n,source,cost,dist);
printf("Shortest path\n");
for(i=1;i<=n;i++)
if(i!=source)
printf("%d->%d,cost=%d\n",source,i,dist[i]);
}