Cod sursa(job #2078983)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 30 noiembrie 2017 13:06:03
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int n, a[101][101];

void citire()
{
    in >> n;
    FOR(i, 1, n)
        FOR(j, 1, n)
            in >> a[i][j];
}

void royfloyd()
{
    FOR(k, 1, n)
        FOR(i, 1, n)
            FOR(j, 1, n)
                if(a[i][k] && a[k][j] &&
                   (a[i][j] > a[i][k] + a[k][j] || !a[i][j])
                   && i != j)
                    a[i][j] = a[i][k] + a[k][j];
}

void afisare()
{
    FOR(i, 1, n)
    {
        FOR(j, 1, n)
            out << a[i][j] << ' ';
        out << '\n';
    }
}

int main()
{
    citire();
    royfloyd();
    afisare();
    return 0;
}