Cod sursa(job #651966)

Utilizator gherghe94Andrei Gherghelau gherghe94 Data 22 decembrie 2011 16:20:46
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <cstdio>

using namespace std;
int a[260][260];
int n;
const int infinite = (1<<30)- 5;
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]);
            if(i!=j && a[i][j] == 0)
                a[i][j] = infinite;

        }

}
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][j] > a[i][k]+a[k][j])
                    a[i][j]= a[i][k]+a[k][j];
            }
        }
    }
}

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