Advertisement
hegemon88676

floyd warshall

Feb 23rd, 2018
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int n, cost[101][101];
  6.  
  7. void citire()
  8. {
  9. ifstream f("grafFW.in");
  10. int i, j;
  11. f>>n;
  12. for(i=1;i<=n;i++)
  13. for(j=1;j<=n;j++)
  14. f>>cost[i][j];
  15. }
  16.  
  17. int main()
  18. {
  19. citire();
  20. int i, j, k;
  21. for(k=1;k<=n;k++)
  22. for(i=1;i<=n;i++)
  23. for(j=1;j<=n;j++)
  24. if(i!=j && i!=k && k!=j && cost[i][k]!=-1 && cost[k][j]!=-1 && (cost[i][j]>cost[i][k]+cost[k][j] || cost[i][k]==-1))
  25. cost[i][j]=cost[i][k]+cost[k][j];
  26.  
  27. for(i=1;i<=n;i++)
  28. {
  29. for(j=1;j<=n;j++)
  30. cout<<cost[i][j]<<" ";
  31. cout<<endl;
  32. }
  33.  
  34. }
  35. /*
  36. 5
  37. -1 -1 -1 -1 10
  38. -1 -1 -1 10 -1
  39. -1 -1 -1 -1 10
  40. 10 10 -1 -1 10
  41. 50 -1 10 10 -1
  42. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement