Cod sursa(job #2686190)

Utilizator mihai_radulescuMihai Radulescu mihai_radulescu Data 18 decembrie 2020 17:18:16
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 1e2 + 1;
const int inf = 10000000;

int n, rin[nmax][nmax];

int main()
{
    in >> n;

    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            if (i!=j) rin[i][j]=inf;
                 else rin[i][j]=0;

    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            in >> rin[i][j];

    for(int k = 1; k <= n; ++k)
        for(int i = 1; i <= n; ++i)
        {
            if(i == k) continue;

            for(int j = 1; j <= n; ++j)
            {
                if(j == i) continue;
                if(j == k) continue;
                if((!rin[i][j] || rin[i][j] > rin[i][k] + rin[k][j]) && rin[i][k] && rin[k][j])
                    rin[i][j] = rin[i][k] + rin[k][j];
            }
        }

    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
            if(rin[i][j] != inf) out << rin[i][j] << ' ';
                        else out << "-1 ";
        out << '\n';
    }
}