Cod sursa(job #3135459)

Utilizator sebuxSebastian sebux Data 3 iunie 2023 12:28:29
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
#define optim ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define let auto
#define popcount __builtin_popcount
#define ctzll __builtin_ctzll
#define clzll __builtin_clzll

using namespace std;
string filename = "royfloyd";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

int M[101][101];

int main()
{
    int n;
    fin >> n;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            fin >> M[i][j];

    for (int k = 1; k <= n; ++k)
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                if ((M[i][j] > M[i][k] + M[k][j] || M[i][j] == 0) && M[k][j] && M[i][k] && i != j)
                    M[i][j] = M[i][k] + M[k][j];

    for (int i = 1; i <= n; ++i, fout << '\n')
        for (int j = 1; j <= n; ++j)
            fout << M[i][j] << ' ';

    return 0;
}