Cod sursa(job #3231315)

Utilizator schema_227Stefan Nicola schema_227 Data 25 mai 2024 17:17:44
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>

using namespace std;

const int NMAX = 257;

const int INF = 1e9;

int main()
{
    int dist[NMAX][NMAX];
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin >> dist[i][j];
            if(i != j && dist[i][j] == 0)
            dist[i][j] = INF;
        }
    }
    
    for(int k = 1 ; k <= n ; k ++)
        for(int i = 1 ; i <= n ; i ++)
            for(int j = 1 ; j <= n ; j ++)
                if(dist[i][j] > dist[i][k] + dist[k][j])
                    dist[i][j] = dist[i][k] + dist[k][j];
    
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(dist[i][j] == INF)
                cout << 0 << " ";
            else
                cout << dist[i][j] << " ";
            cout << '\n';
        }
    }
    return 0;
}