Cod sursa(job #1238544)

Utilizator ThomasFMI Suditu Thomas Thomas Data 7 octombrie 2014 09:55:16
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include <fstream>
using namespace std;

#define NMax 105

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

int n;
int M[NMax][NMax];

int main()
{
    int i,j,k;

    f>>n;
    for(i=1;i<=n;++i) for(j=1;j<=n;++j) f>>M[i][j];

    for(k=1;k<=n;++k) for(i=1;i<=n;++i) for(j=1;j<=n;++j)
        if(i!=j && M[i][k] && M[k][j] && (M[i][k]+M[k][j]<M[i][j] || !M[i][j]))
            M[i][j]=M[i][k]+M[k][j];

    for(i=1;i<=n;++i) {for(j=1;j<=n;++j) g<<M[i][j]<<" "; g<<"\n";}

    f.close();
    g.close();
    return 0;
}