Cod sursa(job #2573962)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 5 martie 2020 19:47:59
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
const int MAXN = 105, INF = 0x3f3f3f3f;

int mat[MAXN][MAXN], dp[MAXN][MAXN], n;

void read()
{
    fin >> n;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) {
            fin >> mat[i][j];
            if (i != j && mat[i][j] == 0)
                dp[i][j] = INF;
            else
                dp[i][j] = mat[i][j];
        }
}

void solve()
{
    for (int k = 1; k <= n; ++k)
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                if (i != k && j != k)
                    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}

void print()
{
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            if (dp[i][j] != INF)
                fout << dp[i][j] << " \n"[j == n];
            else
                fout << '0' << " \n"[j == n];
}

int main()
{
    read();
    solve();
    print();
    return 0;
}