POJ 3255 dijkstra次短路
不说什么了,看代码吧。
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.