Cod sursa(job #2235984)

Utilizator theoioanaTheodoraD theoioana Data 27 august 2018 15:35:13
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
#define INF 10000000
using namespace std;

int main(){

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

    int a[110][110];

    int n; cin >> n;

    for( int i = 0; i < n; i++){
        for( int j = 0; j < n; j++){
            cin >> a[i][j];
            if( a[i][j] == 0){
                a[i][j] = INF;
            }
        }
    }

    for( int k = 0; k < n; k++){
        for( int i = 0; i < n; i++){
            for( int j = 0; j < n; j++){
                if( i != j && a[i][j] > a[i][k] + a[k][j] ){
                    a[i][j] = a[i][k] + a[k][j];
                }
            }
        }
    }

    for( int i = 0; i < n; i++){
        for( int j = 0; j < n; j++){
            if( a[i][j] == INF)
                cout << 0 << " ";
            else
                cout << a[i][j] << " ";
        }
        cout << '\n';
    }

    return 0;
}