Cod sursa(job #2831225)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 10 ianuarie 2022 22:59:18
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
using ll = long long;

const string fn = "royfloyd";


ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

int n;
int a[101][101];
int dist[101][101];
int main() {

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

    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            dist[i][j] = a[i][j];

    for (int k = 1; k <= n; ++k)
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                if (dist[i][j] > dist[i][k] + dist[k][j])
                    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] == 1e9)
                fout << "0 ";
            else fout <<  dist[i][j] << " ";


    fin.close();
    fout.close();
    return 0;
}