Cod sursa(job #1974617)

Utilizator delta_wolfAndrei Stoica delta_wolf Data 28 aprilie 2017 11:05:55
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <cstdio>

using namespace std;
int n,a[101][101];

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",&a[i][j]);
}


void rf()
{
    int i,j,k;
    for( k=1;k<=n;k++)
        for( i=1;i<=n;i++)
            for( j=1;j<=n;j++)
                if(i!=j&&a[i][k]&&a[k][j]&&(a[i][k]+a[k][j]<a[i][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++)
            printf("%d ",a[i][j]);
        printf("\n");
    }
}

int main()
{
    read();
    rf();
    print();
    return 0;
}