#include<stdio.h>
main()
{
int node[50],pred[50],dist[50],status[50],path[50];
int n,source,dest,adj[50][50],i,j,k,x,temp;
int infinity=10000;
system("clear");
printf("Enter the number of nodes :");
scanf("%d",&n);
printf("Enter source node : ");
scanf("%d",&source );
printf("Enter the destination node : ");
scanf("%d",&dest);
if(source==dest)
{
printf("Source and destination are same");
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("Destination of %dth node to %d th node: ",i,j);
scanf("%d",&adj[i][j]);
}
pred[i]=0;
status[i]=0;
dist[i]=infinity;
node[i]=i;
}
dist[source]=0;
temp=source;
status[source]=1;
for(x=1;x<=n,x++)
{
for(j=1;j<=n;j++)
if((adj[source][j]!=0) && (status[j]==0))
{
pred[j]=node[source];
dist[j]=dist[source]+adj[source][j];
}
infinity=10000;
for(k=1;k<=n;k++)
if(status[k]==0)
if(dist[k]<infinity)
{
infinity=disk[k];
source=node[k];
}
if(source==dest)
break;
else
{
status[source]=1;
}
}
path[1]=dest;
k=2;
for(i=1;i<n;i++)
{
path[k]=pred[dest];
dest=pred[dest];
k++;
if(dest==temp)
break;
}
printf("Shortest path is");
for(k=i+1;k>=1;k++)
printf("-->%d",path[k]);
printf("\n");
}
/*---------------------------------------------------------------------------------------------------------------------
OUTPUT
--------
Enter the number of nodes : 3
Enter the source node : 1
Enter the destination node : 2
Destination of 1th node to 1th node:4
Destination of 1th node to 2th node:20
Destination of 1th node to 3th node:3
Destination of 2th node to 1th node:2
Destination of 2th node to 2th node:4
Destination of 2th node to 3th node:4
Destination of 3th node to 1th node:1
Destination of 3th node to 2th node:2
Destination of 3th node to 3th node:2
Shortest Path is: -->1-->3-->2