Acasa Tehnologie Algoritmul Roy-Floyd

Algoritmul Roy-Floyd

by Dragos Schiopu

>

//Roy-Floyd - determina costul drumului minim de la i la j
//are ca intrare matricea costurilor
#include <stdio.h>
#include <conio.h>

int min(int a, int b)
{
if(a<=b) return a;
else return b;
}

void main()
{
int i,j,k,n,a[20][20];
FILE *f=fopen("date2.txt","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);

for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if((i!=k)&&(j!=k)&&(i!=j)) a[i][j]=min(a[i][j],a[i][k]+a[k][j]);

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf(" %d ",a[i][j]);
printf("n");
}
getch();
clrscr();
}

s-ar putea sa-ti placa