ford算法

[复制链接]
发表于 2023-12-30 09:51:23 | 显示全部楼层 |阅读模式
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.

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表