【图论】【单源最短路】【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.
相关文章推荐
- HDOJ 2544 最短路(最短路径 dijkstra算法,SPFA邻接表实现,floyd算法)
- HDU 2544 最短路 SPFA 邻接表 模板
- 图论基础之Floyd(弗洛伊德算法&&Spfa算法&&邻接表):畅通工程续 HDU1874&&最短路 HDU2544
- 0 or 1 图论最短路spfa
- HDU2544_最短路(Dijkstra)(Bellman-Ford)(SPFA+邻接表/邻接矩阵)
- 【图论】【单源最短路】【dijikstra】pascal+邻接矩阵/边集数组
- 【算法系列学习】SPFA邻接表最短路 [kuangbin带你飞]专题四 最短路练习 F - Wormholes
- HDU 2544 最短路 floyd djkstra(邻接表,邻接矩阵) spfa bellman-ford 模板题
- POJ1511---Invitation Cards (最短路:邻接表+2次spfa)
- 【图论】【强连通分量】【Tarjan】pascal+邻接表
- POJ 1511 双向单源最短路 SPFA+邻接表
- 2017.9.9 图论 — 最短路(修邻接表bug---10.20)
- 最短路问题(4种方法)(邻接矩阵,邻接表,bellman-ford,spfa)
- hdu 1874 畅通工程续(最短路spfa邻接表)
- HDU 1535 Invitation Cards 有向图的来回最短路(邻接表反向建图)+spfa
- BZOJ2118 由数论推导至图论!最短路SPFA
- hdu 3416 Marriage Match IV 【图论-网络流-最短路+最大流(spfa + Dinic)】
- 图论浅析--最短路之SPFA
- 最短路模板——dijkstra,SPFA(邻接表实现)
- HDU 2544 最短路 SPFA 邻接表 模板