Cod sursa(job #2160335)

Utilizator andreisamoila74Samoila Andrei andreisamoila74 Data 11 martie 2018 12:32:52
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include    <iostream>
#include    <fstream>

using namespace std;

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

const int NMax=10001;
const int oo= ( 1 << 30);

int n;
int a[NMax][NMax];

void read()
{
    f >> n;
    for (int i= 1; i<= n; ++i)
        for (int j= 1; j<= n; ++j)
        {
            f >> a[i][j];
            if ( !a[i][j])
            a[i][j]=oo;
        }
}

void roy_floyd()
{
    for (int k= 1; k<= n; ++k)
    for (int i= 1; i<= n; ++i)
    for (int j= 1; j<= n; ++j)
        if ( i!=k && j!=k && i!=j )
        if ( a[i][k]+a[k][j] < a[i][j])
        a[i][j]= a[i][k] + a[k][j];
}

void print()
{
    for (int i= 1; i<= n; ++i)
        {
            for (int j= 1; j<= n; ++j)
            {
                if (i==j)
                    g << 0 << " ";
                else
                    if (a[i][j]==oo)
                        g << 0 << " ";
                else
                    g << a[i][j] << " ";
            }
        }
}

int main()
{
    read();
    roy_floyd();
    print();
}