还是水题,接近于裸的spfa(个人比较喜欢用spfa,dijkstra不太喜欢用),代码附上
1 const maxn=6200001; 2 type 3 link=^node; 4 node=record 5 t,d:longint; 6 f:link; 7 end; 8 var n,m,s,i,j,u,v,w,max:longint; 9 adj:array[0..3000] of link; 10 f:array[0..1000000] of longint; 11 d,val:array[0..3000] of longint; 12 went:array[0..3000] of boolean; 13 procedure insert(f,t,d:longint); 14 var p:link; 15 begin 16 new(p); 17 p^.f:=adj[f]; 18 p^.t:=t; 19 p^.d:=d; 20 adj[f]:=p; 21 end; 22 procedure spfa(s:longint); 23 var l,r,now,i:longint; 24 p:link; 25 begin 26 for i:=1 to n do 27 d[i]:=maxn; 28 fillchar(went,sizeof(went),true); 29 l:=1; r:=1; f[1]:=s; d[s]:=0; went[s]:=false; 30 while l<=r do 31 begin 32 now:=f[l]; 33 p:=adj[now]; 34 while p<>nil do 35 begin 36 if d[p^.t]>d[now]+p^.d then 37 begin 38 d[p^.t]:=d[now]+p^.d; 39 if went[p^.t] then 40 begin 41 went[p^.t]:=false; 42 inc(r); 43 f[r]:=p^.t; 44 end; 45 end; 46 p:=p^.f; 47 end; 48 went[now]:=true; 49 inc(l); 50 end; 51 end; 52 begin 53 readln(n,m,s); 54 for i:=1 to m do 55 begin 56 readln(u,v,w); 57 insert(u,v,w); 58 //insert(v,u,w); 59 end; 60 for i:=1 to n do 61 begin 62 spfa(i); 63 val[i]:=d[s]; 64 //writeln(sb); 65 //writeln(d[s]); 66 end; 67 spfa(s); 68 for i:=1 to n do 69 //if i<>s then 70 begin 71 inc(val[i],d[i]); 72 if val[i]>max then max:=val[i]; 73 end; 74 writeln(max); 75 end.
时间: 2024-11-09 00:10:18