码迷,mamicode.com
首页 > 其他好文 > 详细

邻接表Spfa(Pascal版)

时间:2016-06-17 09:45:48      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:

 1 const maxn=100;
 2 type
 3         point=^node;
 4         node=record
 5                 g,w:longint;
 6                 next:point;
 7         end;
 8 var
 9         i,j,k,l,m,n,s,t:longint;
10         a:array[0..maxn] of point;
11         b,c:array[0..maxn] of longint;
12         d:array[0..maxn] of boolean;
13 procedure add(x,y,z:longint);
14         var p:point;
15         begin
16                 new(p);p^.g:=y;p^.w:=z;
17                 p^.next:=a[x];a[x]:=p;
18         end;
19 procedure spfa(x:longint);
20         var f,r:longint;p:point;
21         begin
22                 fillchar(d,sizeof(d),false);d[x]:=true;
23                 fillchar(c,sizeof(c),0);
24                 f:=1;r:=2;b[f]:=x;
25                 while f<r do
26                         begin
27                                 p:=a[b[f]];
28                                 while p<>nil do
29                                         begin
30                                                 if (c[p^.g]=0) or (c[p^.g]>c[b[f]]+p^.w) then
31                                                         begin
32                                                                 c[p^.g]:=c[b[f]]+p^.w;
33                                                                 if d[p^.g]=false then
34                                                                         begin
35                                                                                 d[p^.g]:=true;b[r]:=p^.g;
36                                                                                 if r=maxn then r:=1 else inc(r);
37                                                                         end;
38                                                         end;
39                                                 p:=p^.next;
40                                         end;
41                                 d[b[f]]:=false;
42                                 if f=maxn then f:=1 else inc(f);
43                         end;
44         end;
45 begin
46         readln(n,m,s,t);
47         for i:=1 to n do a[i]:=nil;
48         for i:=1 to m do
49                 begin
50                         readln(j,k,l);
51                         add(j,k,l);add(k,j,l);
52                 end;
53         spfa(s);writeln(c[t]);
54 end.

 

邻接表Spfa(Pascal版)

标签:

原文地址:http://www.cnblogs.com/Fefnir/p/5592878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!