Cod sursa(job #2518252)

Utilizator rexlcdTenea Mihai rexlcd Data 5 ianuarie 2020 13:20:16
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>

#define INF 0x3f3f3f3f

using namespace std;

int d[102][102];

int main()
{
    ifstream f("royfloyd.in");
    ofstream g("royfloyd.out");
    int n; f >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            int x; f >> x;
            if(i == j)  d[i][j] = 0;
            else if(!x) d[i][j] = INF;
            else        d[i][j] = x;
        }

    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            g << (d[i][j] == INF ? 0 : d[i][j]) << ' ';
        g << '\n';
    }

    f.close();
    g.close();
    return 0;
}