Cod sursa(job #651956)

Utilizator gherghe94Andrei Gherghelau gherghe94 Data 22 decembrie 2011 15:56:29
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <cstdio>

using namespace std;
int a[260][260];
int n;
void citire()
{
    freopen("royfloyd.in","r",stdin);
    scanf("%d\n",&n);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            scanf("%d",&a[i][j]);
}
void afisare()
{
    freopen("royfloyd.out","w",stdout);
   for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j)
            printf("%d ",a[i][j]);
        printf("\n");
   }
}
void roy_floyd()
{
    for(int k=1;k<=n;++k)
    {
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=n;++j)
            {
                if(a[i][k] && a[k][j] && a[i][j] > a[i][k]+a[k][j])
                    a[i][j]= a[i][k]+a[k][j];
            }
        }
    }
}

int main()
{
    citire();
    roy_floyd();
    afisare();
    return 0;
}