Cod sursa(job #1492328)

Utilizator zertixMaradin Octavian zertix Data 27 septembrie 2015 16:26:21
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <cstdio>
using namespace std;

int n,dist[105][105];

void read()
{
    freopen("royfloyd.in","r",stdin);
    freopen("royfloyd.out","w",stdout);

    scanf("%d",&n);
    for (int i=1; i<=n; ++i)
        for (int j=1; j<=n; ++j)
            scanf("%d",&dist[i][j]);
}
void afisare()
{

    for (int i=1; i<=n; ++i)
            {
                for (int j=1; j<=n; ++j)
                    printf("%d ",dist[i][j]);
                printf("\n");
            }
}
void floyd()
{
    for (int k=1; k<=n; ++k)
        for (int i=1; i<=n; ++i)
            for (int j=1; j<=n; ++j)
                if (dist[i][k] && dist[k][j] && (dist[i][j]>dist[i][k]+dist[k][j] || !dist[i][j]) && i!=j)
                    dist[i][j]=dist[i][k]+dist[k][j];
    afisare();
}

int main()
{
    read();
    floyd();

    return 0;
}