ford算法 programzudlouj;
const n=6;max=10000 ;
cost:array[1..6,1..6] ofreal=((0,50,10,max,45,max),(max,0
,15,max,10,max),(20,max,0,15,max,max),(max,20,max,0,35,max),(
max,max,max,30,0,max),(max,max,max,3,max,0));
var dist:array[1..n] of real;
path,p:array[1..n] of 1..n;
first,tail,u:1..n;
s:set of 1..n;
i,j,y,m:integer;
min:real;
quit:boolean;
begin
read(first);
for i:=1 to n do dist:=max;
dist[first]:=0;
repeat
quit:=true;
for i:=1 to n do
if dist<max then
for j:=1 to n do
if dist+cost[i,j]<dist[j] then
begin
dist[j]:=dist+cost[i,j];
path[j]:=i;
quit:=false;
end;
until quit;
for i:=1 to n do
if i<>first then
if dist<>max then
begin
writeln('mindist(',first,',',i,')=',dist:8:2);
y:=i;m:=0;
while (y<>first) do
begin inc(m);p[m]:=y;y:=path[y] end;
write('path:',first);
for j:=m downto 1 do
write('->',p[j]);
writeln;
end
else
writeln('mindist(',first,',',i,'):','No answer');
end.
|