您的位置:首页 > 其它

【图论】【单源最短路】【SPFA】pascal+邻接表

2016-11-14 20:47 399 查看
var list,dis:array[0..10000] of longint;
next,toit,cost,q:array[0..100000] of longint;
flag:array[0..100000] of boolean;
n,m,i,a,b,c,s,e,tot:longint;

procedure add(a,b,c:longint);
begin
inc(tot);
toit[tot]:=b; //当前边的出点
cost[tot]:=c; //边权
next[tot]:=list[a]; //当前边指向的前一条边
list[a]:=tot; //当前边的入点a引出的边的标号
end;

procedure SPFA;
var i,head,tail,v,k:longint;
begin
fillchar(flag,sizeof(flag),true);
for i:=1 to n do dis[i]:=maxlongint;
head:=1;
tail:=1;
q[1]:=s;
dis[s]:=0;
flag[s]:=false;
repeat
v:=q[head];
k:=list[v];
while k<>0 do begin
if dis[v]+cost[k]<dis[toit[k]] then begin
dis[toit[k]]:=dis[v]+cost[k];
if flag[toit[k]] then begin
inc(tail);
q[tail]:=toit[k];
flag[toit[k]]:=false;
end;
end;
k:=next[k];
end;
flag[v]:=true;
inc(head);
until head>tail;
end;

begin
fillchar(list,sizeof(list),0);
fillchar(next,sizeof(next),0);
fillchar(toit,sizeof(toit),0);
fillchar(cost,sizeof(cost),0);
tot:=0;
readln(n,m);
for i:=1 to m do begin
readln(a,b,c);
add(a,b,c);
end;
readln(s,e);
SPFA;
if dis[e]<maxlongint then writeln(dis[e]) else writeln('No Solution');
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pascal 图论 SPFA 邻接表