Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define ld long double
- #define pb push_back
- #define fe first
- #define se second
- #define nl "\n"
- #define pp pair < ll , ll >
- #define sz(x) (ll)x.size()
- #define st(x) sort(x.begin(),x.end())
- #define rst(x) sort(x.rbegin(), x.rend())
- #define all(x) x.begin(),x.end()
- long double pi = 3.14159265358979323;
- const double EPS = 1e-12;
- const int N = 1e2 + 5;
- const int mod = 1e9 + 7;
- ll mat[N][N] , dis[N][N];
- ll n;
- bool floyd(){
- //All pair shortest path in a directed graph
- //
- for(int k = 0 ; k < n ; k++ ){
- for(int i = 0 ; i < n ; i++ ){
- for(int j = 0 ; j < n ; j++ ){
- dis[i][j] = min(dis[i][j] , dis[i][k] + dis[k][j]);
- }
- }
- }
- //Negative Edge cycle
- for(int i = 0 ; i < n ;i++ ){
- if(dis[i][i] < 0)return 1;
- }
- return 0 ;
- }
- int main()
- {
- fast;
- cin >> n ;
- for(int i = 0 ; i < n ;i++ ){
- for(int j = 0 ; j < n ;j++ ){
- cin >> mat[i][j];
- dis[i][j] = mat[i][j];
- }
- }
- floyd();
- for(int i = 0 ; i < n ;i++ ){
- for(int j = 0 ; j < n ;j++ ){
- cout << dis[i][j] << " ";
- }
- cout << "\n";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment