Cod sursa(job #1621507)

Utilizator cristinamateiCristina Matei cristinamatei Data 29 februarie 2016 19:32:42
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int d[102][102], n;
const int N = 1000000;

/*void drum( int x, int y )
{
    if ( x != y )
        drum(x, pred[x][y] );
    out << y <<' ';
}*/

int main()
{
    int i, j, k;
    in >> n;
    for ( i = 1; i <= n; i++ )
        for ( j = 1; j <= n; j++ )
        {
            in >> d[i][j];
            if ( i != j && d[i][j] == 0 )
                d[i][j] = N;
        }
    for ( k = 1; k <= n; k++ )
        for ( i = 1; i <= n; i++ )
            for ( j = 1; j <= n; j++ )
                if ( d[i][k] + d[k][j] < d[i][j] )
                {
                    d[i][j] = d[i][k] + d[k][j];
                    //pred[i][k] = pred[k][j];
                    ///pred[i][j] = penultimul nod de cost minim pe drumul de la i la j
                }
    for ( i = 1; i <= n; i++ )
    {
        for ( j = 1; j <= n; j++ )
        {
            if ( d[i][j] == N )
                out <<0<<' ';
            else out << d[i][j]<<' ';
        }
        out <<'\n';
    }
    return 0;
}