Cod sursa(job #3030159)

Utilizator Elvis_CostinTuca Elvis-Costin Elvis_Costin Data 17 martie 2023 15:40:17
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
using namespace std;
string np = "royfloyd";
ifstream f(np + ".in");
ofstream g(np + ".out");

// #define f cin
// #define g cout

int n, mat[103][103];

int main()
{
    ios_base::sync_with_stdio(false);
    f.tie(nullptr);

    f >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            f >> mat[i][j];

    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (i != j and mat[i][k] and mat[k][j])
                    if (!mat[i][j] or mat[i][j] > mat[i][k] + mat[k][j])
                        mat[i][j] = mat[i][k] + mat[k][j];

    for (int i = 1; i <= n; i++, g << '\n')
        for (int j = 1; j <= n; j++)
            g << mat[i][j] << " ";

    return 0;
}