POJ 3255 dijkstra次短路
不说什么了,看代码吧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | program p3255; Type rec= record e,w,next: longint ; end ; Var n,m,i,j,k,nowmin,minj,mink,u,top,st,ed,ww: longint ; y:rec; a: array [ 0..200002 ] of rec; b: array [ 0..10002 ] of longint ; v: array [ 1..2 , 0..10002 ] of boolean ; d: array [ 1..2 , 0..10002 ] of longint ; Procedure add(st,ed,ww: longint );inline; begin inc(top); with a[top] do begin e:=ed; w:=ww; next:=b[st]; end ; b[st]:=top; end ; begin readln(n,m); top:= 0 ; for i:= 1 to m do begin readln(st,ed,ww); add(st,ed,ww); add(ed,st,ww); end ; fillchar(v,sizeof(v), false ); fillchar(d,sizeof(d), $7f ); d[ 1 , 1 ]:= 0 ; for i:= 1 to 2 *n do begin nowmin:=maxlongint; for j:= 1 to n do for k:= 1 to 2 do if not v[k,j] then if d[k,j]<nowmin then begin nowmin:=d[k,j]; minj:=j;mink:=k; end ; if nowmin=maxlongint then break; v[mink,minj]:= true ; u:=b[minj]; while u<> 0 do begin y:=a[u]; u:=y . next; if nowmin+y . w<d[ 1 ,y . e] then begin d[ 2 ,y . e]:=d[ 1 ,y . e]; d[ 1 ,y . e]:=nowmin+y . w; end else if nowmin+y . w<d[ 2 ,y . e] then d[ 2 ,y . e]:=nowmin+y . w; end ; end ; writeln (d[ 2 ,n]); readln; end . |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步