Cod sursa(job #2209851)

Utilizator DordeDorde Matei Dorde Data 4 iunie 2018 21:49:11
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>

using namespace std;
int const NM = 101 , inf = (1 << 30);
int v [NM][NM];
char const in [] = "royfloyd.in";
char const out [] = "royfloyd.out";
int main()
{
    fstream f , g;
    f . open (in , ios :: in);
    int n , i , j , k;
    f >> n;
    for(i = 1 ; i <= n ; ++ i)
        for(j = 1 ; j <= n ; ++ j)
        {
            f >> v [i][j];
            if(! v [i][j] && (i != j))
                v [i][j] = inf;
        }
    f . close ();
    for(k = 1 ; k <= n ; ++ k)
        for(i = 1 ; i <= n ; ++ i)
            for(j = 1 ; j <= n ; ++ j)
                if (v [k][j] + v [i][k] < v [i][j] && i != j)
                    v [i][j] = v [k][j] + v [i][k];
    g . open (out , ios :: out);
    for(i = 1 ; i <= n ; ++ i)
    {
        for(j = 1 ; j <= n ; ++ j)
            if (v [i][j] == inf)
                g << "0 ";
            else
                g << v [i][j] << ' ';
        g << '\n';
    }
    return 0;
}