Cod sursa(job #2964874)

Utilizator SmauSmau George Robert Smau Data 14 ianuarie 2023 09:22:19
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
#define NMAX 128
#define INF 100000000

using namespace std;

ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

int c[NMAX][NMAX], n;

int main() {
    fin >> n;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++) {
            fin >> c[i][j];
            if(c[i][j] == 0 && i != j) c[i][j] = INF;
        }

    for(int z = 1; z <= n; z ++)
        for(int x = 1; x <= n; x ++)
            for(int y = 1; y <= n; y ++)
                c[x][y] = min(c[x][y], c[x][z] + c[z][y]);

    for(int i = 1; i <= n; i ++, fout << '\n')
        for(int j = 1; j <= n; j ++) 
            if(c[i][j] == 0 && i != j) fout << "0 ";
            else fout << c[i][j] << ' ';

    return 0;
}