Cod sursa(job #2981213)

Utilizator Apyxabcdefghij Apyx Data 17 februarie 2023 15:34:08
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
#define nmax 100

using namespace std;

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

const int inf = 0x3f3f3f3f;

int dist[nmax+5][nmax+5];

int main() {
    ios::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);
    int n; fin >> n;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            fin >> dist[i][j];
            if(!dist[i][j] && i != j) {
                dist[i][j] = inf;
            }
        }
    }
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
    for(int i = 1; i <= n; i++, fout << "\n") {
        for(int j = 1; j <= n; j++) {
            if(dist[i][j] == inf) {
                fout << 0 << " ";
            } else {
                fout << dist[i][j] << " ";
            }
        }
    }
    return 0;
}