http://www.lydsy.com/JudgeOnline/problem.php?id=1614
二分,练手。注意处理无解和答案为0.
Code/************************************************************** Problem: 1614 User: lijianlin1995 Language: Pascal Result: Accepted Time:68 ms Memory:656 kb ****************************************************************/ program Telephone; uses math; const Filename='Telephone'; maxn=1010; maxp=10010; var v,w,next :array[0..maxp*2] of longint; head,dis,q:array[0..maxn] of longint; vis :array[0..maxn] of boolean; ans,x,y,z,i,n,p,k,l,r,tot,mid:longint; procedure build(x,y,z:longint); begin inc(tot); v[tot]:=y;w[tot]:=z; next[tot]:=head[x]; head[x]:=tot; end; procedure SPFA(x:longint); var h,t,i,cur,cost:longint; begin fillchar (vis,sizeof(vis), false); filldword(dis,sizeof(dis)>>2,maxlongint); h:=0;t:=1;Q[1]:=1; dis[1]:=0;vis[1]:=true; while h<>t do begin inc(h);if h>maxn then h:=1; cur:=Q[h];i:=head[cur]; while i<>0 do begin cost:=longint(w[i]>x); if(dis[v[i]]>dis[cur]+cost) then begin dis[v[i]]:=dis[cur]+cost; if not vis[v[i]] then begin inc(t);if t>maxn then t:=1; Q[t]:=v[i];vis[v[i]]:=true; end; end; i:=next[i];end; vis[cur]:=false;end; end; begin readln(N,P,K); for i:=1 to P do begin readln(x,y,z); build(x,y,z); build(y,x,z); r:=max(z,r); end; SPFA(r);l:=-1; if dis[n]>k then ans:=-1 else begin while l<r-1 do begin mid:=(l+r)>>1;SPFA(mid); if dis[n]<=k then r:=mid else l:=mid; end; ans:=r; end; writeln(ans); end.