【最短路径】dijkstra算法 pascal

来源:互联网 发布:淘宝拒签快递怎么退款 编辑:程序博客网 时间:2024/06/05 03:32


dijkstra算法适用于无负边的图

var
  f:array[1..100] of boolean;
  cost:array[1..100,1..100] of longint;
  fa,dist:array[1..100] of longint;
  n,m,r,a,b,c,i,j,p,min:longint;
begin
  readln(n,m,r);
  for i:=1 to m do begin
    readln(a,b,c);
    cost[a,b]:=c;
  end;
  fillchar(f,sizeof(f),false);
  for i:=1 to n do dist[i]:=maxlongint;
  dist[r]:=0;  

 //不写f[r]=true;!!!!!!!!!吐舌头
  for i:=1 to n do begin
    if i<>r then begin
      min:=maxlongint;
      for j:=1 to n do
        if (not f[j]) and (dist[j]<min) then begin
          min:=dist[j];
          p:=j;
        end;
      f[p]:=true;
      for j:=1 to n do
        if (dist[j]>dist[p]+cost[p,j]) and (cost[p,j]<>0) then begin
          dist[j]:=dist[p]+cost[p,j];
          fa[j]:=p;
        end;
    end;
  end;
  for i:=1 to n do begin
    writeln(dist[i]);
    p:=i;
    write(i);
    while fa[p]<>0 do begin
      write('<--',fa[p]);
      p:=fa[p];
    end;
    writeln;
  end;
  readln;
end.

0 0
原创粉丝点击