Cod sursa(job #3234353)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 8 iunie 2024 21:43:09
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
const int inf = 1e7 + 2;
int n, i, j, k, d[102][102];

int main() {
    fin >> n;
    for(i = 1; i <= n; i++) {
        for(j = 1; j <= n; j++) {
            fin >> d[i][j];
            if(d[i][j] == 0) d[i][j] = inf;
        }
    }

    for(k = 1; k <= n; k++) {
        for(i = 1; i <= n; i++) {
            for(j = 1; j <= n; j++) {
                if(i != j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }


    for(i = 1; i <= n; i++) {
        for(j = 1; j <= n; j++) {
            if(d[i][j] == inf) fout << "0 ";
            else fout << d[i][j] << " ";
        }
        fout << "\n";
    }

    return 0;
}