fork download
  1. #include<stdio.h>
  2. struct node
  3. {
  4. unsigned dist[20];
  5. unsigned from[20];
  6. }rt[10];
  7. int main()
  8. {
  9. int dmat[20][20];
  10. int n,i,j,k,count=0;
  11. printf("\nEnter the number of nodes : ");
  12. scanf("%d",&n);
  13. printf("\nEnter the cost matrix :\n");
  14. for(i=0;i<n;i++)
  15. for(j=0;j<n;j++)
  16. {
  17. scanf("%d",&dmat[i][j]);
  18. dmat[i][i]=0;
  19. rt[i].dist[j]=dmat[i][j];
  20. rt[i].from[j]=j;
  21. }
  22. do
  23. {
  24. count=0;
  25. for(i=0;i<n;i++)
  26. for(j=0;j<n;j++)
  27. for(k=0;k<n;k++)
  28. if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
  29. {
  30. rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
  31. rt[i].from[j]=k;
  32. count++;
  33. }
  34. }while(count!=0);
  35. for(i=0;i<n;i++)
  36. {
  37. printf("\n\nState value for router %d is \n",i+1);
  38. for(j=0;j<n;j++)
  39. {
  40. printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
  41. }
  42. }
  43. printf("\n\n");
  44. }
  45.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Enter the number of nodes : 
Enter the cost matrix :