Cod sursa(job #363389)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 12 noiembrie 2009 23:13:18
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include<fstream>
#include<iostream>
#define inf "royfloyd.in"
#define outf "royfloyd.out"
#define MaxN 101
#define PINF 1.e20
using namespace std;

fstream f(inf,ios::in),g(outf,ios::out);

int N;
float MP[MaxN][MaxN];

void Citire()
{
f>>N;
int i,j;
for(i=1;i<=N;i++)
    {
    for(j=1;j<=N;j++)
        {
        f>>MP[i][j];
        if(i!=j && MP[i][j]==0)MP[i][j]=PINF;
        }
    }
}

void RoyFloyd()
{
for(int k=1;k<=N;k++)
    {
    for(int i=1;i<=N;i++)
        {
        for(int j=1;j<=N;j++)
            if(MP[i][j]>MP[i][k]+MP[k][j])MP[i][j]=MP[i][k]+MP[k][j];
        }
    }
}

int main()
{
Citire();
RoyFloyd();
for(int i=1;i<=N;i++)
    {
    for(int j=1;j<=N;j++)
        {
        if(MP[i][j]==PINF)g<<"0"<<" ";
        else g<<MP[i][j]<<" ";
        }
    g<<endl;
    }
f.close();
g.close();
return 0;
}