nrk009

FloydWarshall

May 22nd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1.  
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  7. #define ll long long
  8. #define ld long double
  9. #define pb push_back
  10. #define fe first
  11. #define se second
  12. #define nl "\n"
  13. #define pp pair < ll , ll >
  14. #define sz(x) (ll)x.size()
  15. #define st(x) sort(x.begin(),x.end())
  16. #define rst(x) sort(x.rbegin(), x.rend())
  17. #define all(x) x.begin(),x.end()
  18. long double pi = 3.14159265358979323;
  19.  
  20. const double EPS = 1e-12;
  21. const int N = 1e2 + 5;
  22. const int mod = 1e9 + 7;
  23.  
  24. ll mat[N][N] , dis[N][N];
  25. ll n;
  26.  
  27. bool floyd(){
  28.  
  29.     //All pair shortest path in a directed graph
  30.     //
  31.     for(int k = 0 ; k < n ; k++ ){
  32.         for(int i = 0 ; i < n ; i++ ){
  33.             for(int j = 0 ; j < n ; j++ ){
  34.                     dis[i][j] = min(dis[i][j] , dis[i][k] + dis[k][j]);
  35.             }
  36.         }
  37.     }
  38.  
  39.     //Negative Edge cycle
  40.     for(int i = 0 ; i < n  ;i++ ){
  41.         if(dis[i][i] < 0)return 1;
  42.     }
  43.     return 0 ;
  44.  
  45.  
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51.     fast;
  52.     cin >> n ;
  53.     for(int i = 0 ; i < n ;i++ ){
  54.         for(int j = 0 ; j < n ;j++ ){
  55.                      cin >> mat[i][j];
  56.                      dis[i][j] = mat[i][j];
  57.           }      
  58.     }
  59.     floyd();
  60.     for(int i = 0 ; i < n ;i++ ){
  61.         for(int j = 0 ; j < n ;j++ ){
  62.                      cout << dis[i][j] << " ";
  63.          }      
  64.          cout << "\n";
  65.     }
  66.    
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.    
  89.     return 0;
  90.    
  91. }
Add Comment
Please, Sign In to add comment