Spfa算法模版
来源:互联网 发布:mac系统怎么看内存 编辑:程序博客网 时间:2024/05/08 15:46
{<spfa><O(KM)>}const maxn=5000;type link=^node; node=record x,dis:longint; next:link; end;var g:array[1..maxn] of link; dist,q:array[1..maxn] of longint; v:array[1..maxn] of boolean; n:longint;procedure spfa(s:longint);var top,i,nowi:longint; p:link;begin for i:=1 to n do begin dist[i]:=maxlongint; v[i]:=false; end; dist[s]:=0; v[s]:=true; top:=1; q[1]:=s; while top>0 do begin nowi:=q[top]; v[nowi]:=false; dec(top); p:=g[nowi]; while p<>nil do begin if dist[nowi]+p^.dis<dist[p^.x] then begin dist[p^.x]:=dist[nowi]+p^.dis; if not v[p^.x] then begin inc(top); q[top]:=p^.x; v[p^.x]:=true; end; end; p:=p^.next; end; end;end;