Advertisement
Guest User

ddd

a guest
Jan 29th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define oo 1e9
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("roy-floyd.in");
  7. ofstream fout("roy-floyd.out");
  8.  
  9. int a[105][105],n,m,cost;
  10.  
  11. int main()
  12. {
  13. int i,k,j;
  14. fin>>n>>m;
  15. ///initializare matrice
  16. for(i=1;i<n;i++)
  17. for(j=i+1;j<=n;j++)
  18. a[i][j]=a[j][i]=oo;
  19. ///citire arce
  20. for(k=1;k<=m;k++)
  21. {
  22. fin>>i>>j>>cost;
  23. a[i][j]=cost;
  24. }
  25. ///Roy-Floyd
  26. for(k=1;k<=n;k++)
  27. for(i=1;i<=n;i++)
  28. for(j=1;j<=n;j++)
  29. if(i!=j && i!=k && j!=k && a[i][j]>a[i][k]+a[k][j])
  30. a[i][j]=a[i][k]+a[k][j];
  31. ///afisare
  32. for(i=1;i<=n;i++)
  33. {
  34. for(j=1;j<=n;j++)
  35. if(a[i][j]==oo) fout<<"-1 ";
  36. else fout<<a[i][j]<<" ";
  37. fout<<"\n";
  38. }
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement