Cod sursa(job #1185370)

Utilizator andreeaghetuUNIBUC andreeaghetu andreeaghetu Data 15 mai 2014 16:32:25
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("royfloyd.in");
ofstream out ("royfloyd.out");
#define MX 101
#define INF 1001

int N;
int dist[MX][MX];
int main()
{
    in>>N;
    for (int i=1;i<=N;++i)
        for (int j=1;j<=N;++j)
        {
            in>>dist[i][j];
            if (!dist[i][j] && i!=j)
                dist[i][j]=INF;
        }

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

    for (int i=1;i<=N;++i)
    {
        for (int j=1;j<=N;++j)
            out<<(dist[i][j]==INF ? 0 : dist[i][j])<<" ";
        out<<'\n';
    }


    return 0;
}