Binary search + shortest circuit
Two points limit the height and then the shortest
#include <cstdio>#include<cstring>#include<cmath>#include<vector>#include<queue>#include<algorithm>using namespacestd;Const intinf=0x7FFFFFFF;Const intmaxn= ++Ten;structedge{int from, To,lim,len;} E[MAXN*MAXN];intN,m,tot;intSt,en,lim;vector<int>G[MAXN];intDISLEN[MAXN];intFLAG[MAXN];intMin,max,mid;voidinit () { for(intI=0; i<=n; i++) g[i].clear (); Tot=0;}voidSPFA () {Queue<int>p; for(intI=0; i<=n; i++) dislen[i]=INF; memset (Flag,0,sizeofflag); DISLEN[ST]=0; FLAG[ST]=1; Q.push (ST); while(!Q.empty ()) { intH=Q.front (); Q.pop (); FLAG[H]=0; for(intI=0; I<g[h].size (); i++) { intId=G[h][i]; if(E[id].lim<mid)Continue; if(dislen[h]+e[id].len<Dislen[e[id].to]) {Dislen[e[id].to]=dislen[h]+E[id].len; if(flag[e[id].to]==0) {flag[e[id].to]=1; Q.push (e[id].to); } } } }}intMain () {intt=1; while(~SCANF ("%d%d",&n,&M)) {if(n==0&&m==0) Break; Init (); for(intI=1; i<=m; i++) { intU,v,f,c; scanf ("%d%d%d%d",&u,&v,&f,&c); if(f==-1) f=INF; E[tot]. from=u; E[tot].to=W; E[tot].lim=F; E[tot].len=C; G[u].push_back (TOT); Tot++; E[tot]. from=v; E[tot].to=u; E[tot].lim=F; E[tot].len=C; G[v].push_back (TOT); Tot++; } scanf ("%d%d%d",&st,&en,&Lim); Min=0; max=Lim; Mid= (min+max+1)/2; intAns1,ans2=INF; while(Min <Max) {Mid= (min+max+1) >>1; SPFA (); if(Dislen[en]! =INF) Min= Mid, Ans2 =Dislen[en]; ElseMax = Mid-1; } if(t!=1) printf ("\ n"); printf ("Case %d:\n", t++); if(ans2==INF) printf ("cannot reach destination\n"); Elseprintf ("Maximum height =%d\nlength of Shortest route =%d\n", MIN,ANS2); } return 0;}
HDU 2962 Trucking