Cod sursa(job #505094)

Utilizator pongraczlajosLajos Pongracz pongraczlajos Data 30 noiembrie 2010 18:26:54
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.7 kb
type meret = 1..100;

var a:array[1..100,1..100] of longint;
    i,j,k,n:meret;
    f,g:text;

Begin
assign(f,'royfloyd.in');
reset(f);
readln(f,n);

 for i := 1 to n do
  for j := 1 to n do begin
   read(f,a[i,j]);
   if (a[i,j] = 0) and (i<>j) then a[i,j] := MAXLONGINT;
  end;

close(f);

 {ROY-FLOYD}
 for k := 1 to n do
  for i := 1 to n do
   for j := 1 to n do
    if (a[i,k]<>MAXLONGINT) and (a[k,j]<>MAXLONGINT) and (a[i,j]>a[i,k]+a[k,j]) then a[i,j] := a[i,k]+a[k,j];

assign(g,'royfloyd.out');
rewrite(g);

  for i := 1 to n do begin
   for j := 1 to n do
    if a[i,j] = MAXLONGINT then write(g,'0 ') else write(g,a[i,j],' ');
   writeln(g);
  end;

close(g);
End.