Cod sursa(job #2543468)

Utilizator BluThund3rRadu George BluThund3r Data 11 februarie 2020 10:27:37
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#define Nmax 101
using namespace std;

ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
const int INF = 1e4 + 4;
int dist[Nmax][Nmax], n;

void load()
{
    in >> n;

    for(int i = 1; i <= n; ++ i)
        for(int j = 1; j <= n; ++ j)
            dist[i][j] = INF;

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

void shortestPath()
{
    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];
}

void output()
{
    for(int i = 1; i <= n; ++ i)
    {
        for(int j = 1; j <= n; ++ j)
            if(dist[i][j] == INF)
                out << 0 << ' ';
            else
                out << dist[i][j] << ' ';

        out << '\n';
    }
}

int main()
{
    load();

    shortestPath();

    output();

    return 0;
}