Cod sursa(job #3139663)

Utilizator PostoacaMateiMatei Postoaca PostoacaMatei Data 30 iunie 2023 17:31:43
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, dist[105][105];
const int inf = 1e9;

void RoyFloyd() {
    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++)
        for (int j = 1; j <= n; j++)
            if (dist[i][j] == inf && i != j)
                dist[i][j] = 0;
}

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            fin >> dist[i][j];
            if (i != j && dist[i][j] == 0)
                dist[i][j] = inf;
        }
    RoyFloyd();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            fout << dist[i][j] << " ";
        fout << "\n";
    }
    return 0;
}