fork download
  1. #include<stdio.h>
  2. int min(int a, int b)
  3. {
  4. return(a < b ? a : b);
  5. }
  6. void floyd(int D[][10],int n)
  7. {
  8. for(int k=1;k<=n;k++)
  9. for(int i=1;i<=n;i++)
  10. for(int j=1;j<=n;j++)
  11. D[i][j]=min(D[i][j],D[i][k]+D[k][j]);
  12. }
  13. int main()
  14. {
  15. int n, cost[10][10];
  16. printf("Enter no. of Vertices: ");
  17. scanf("%d",&n);
  18. printf("Enter the cost matrix\n");
  19. for(int i=1;i<=n;i++)
  20. for(int j=1;j<=n;j++)
  21. scanf("%d",&cost[i][j]);
  22. floyd(cost,n);
  23. printf("All pair shortest path\n");
  24. for(int i=1;i<=n;i++)
  25. {
  26. for(int j=1;j<=n;j++)
  27. printf("%d ",cost[i][j]);
  28. printf("\n");
  29. }
  30. }
Success #stdin #stdout 0.01s 5316KB
stdin
4
999 8 4 999
999 999 1 999
4 999 999 999
999 2 9 998
stdout
Enter no. of Vertices: Enter the cost matrix
All pair shortest path
8 8 4 999 
5 13 1 999 
4 12 8 999 
7 2 3 998