Cod sursa(job #1791627)

Utilizator PaulStighiStiegelbauer Paul-Alexandru PaulStighi Data 29 octombrie 2016 16:01:17
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include<fstream>
#define NMax 105
#define oo 1<<14
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

int N;
int D[NMax][NMax];

void Read()
{
    fin>>N;
    for(int i = 1 ; i <= N ; ++i)
        for(int j = 1 ; j <= N ; ++j)
        {
            int x;  fin>>x;

            if(!x && i != j)
                D[i][j] = oo;
            else
                D[i][j] = x;
        }
}

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

void Print()
{
    for(int i = 1 ; i <= N ; ++i)
    {
        for(int j = 1 ; j <= N ; ++j)
        {
            if(D[i][j] == oo)   fout<<"0 ";
            else    fout<<D[i][j]<<" ";
        }
        fout<<"\n";
    }
}

int main()
{
    Read();
    Solve();
    Print();

    fin.close();
    fout.close();
    return 0;
}