Cod sursa(job #3325376)

Utilizator temp1234Temp Mail temp1234 Data 25 noiembrie 2025 13:11:33
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, costs[101][101], d[101][101];

const int inf = 1e9;

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

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

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