Cod sursa(job #2801449)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 16 noiembrie 2021 11:37:01
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>

using namespace std;

#define NMAX 100
#define MAXDIST 10000

int dist[NMAX + 1][NMAX + 1];

ifstream cin ( "royfloyd.in" );
ofstream cout ( "royfloyd.out" );

int main() {
    int n, i, j, k;
    cin >> n;
    for ( i = 0; i < n; i++ ) {
        for ( j = 0; j < n; j++ ) {
            cin >> dist[i][j];
            if (i != j && dist[i][j] == 0)
                dist[i][j] = MAXDIST * n;
        }
    }
    for ( k = 0; k < n; k++ ) {
        for ( i = 0; i < n; i++ ) {
            for ( j = 0; j < n; j++ ) {
                if ( dist[i][k] + dist[k][j] < dist[i][j] )
                    dist[i][j] = dist[i][k] + dist[k][j];
            }
        }
    }
    for ( i = 0; i < n; i++ ) {
        for ( j = 0; j < n; j++ )
            cout << dist[i][j] << " ";
        cout << "\n";
    }
    return 0;
}