http://acm.hdu.edu.cn/showproblem.php?pid=2962
二分+spfa
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 const int N=1010,M=200010; 7 struct edge 8 { 9 int v,w,h,next; 10 }e[M]; 11 int head[N],n,m; 12 bool inq[N]; 13 int dis[N]; 14 queue<int> q; 15 void addedge(int u,int v,int w,int h) 16 { 17 edge et={v,w,h,head[u]}; 18 e[m]=et; 19 head[u]=m++; 20 } 21 void graphinit() 22 { 23 m=0; 24 memset(head,-1,sizeof(head)); 25 } 26 bool read_graph() 27 { 28 graphinit(); 29 int mm; 30 scanf("%d%d",&n,&mm); 31 if(n==0 && mm==0) return false; 32 while(mm--) 33 { 34 int u,v,w,h; 35 scanf("%d%d%d%d",&u,&v,&h,&w); 36 addedge(u,v,w,h); 37 addedge(v,u,w,h); 38 } 39 return true; 40 } 41 void spfa(int p,int h) 42 { 43 while(!q.empty()) q.pop(); 44 memset(inq,0,sizeof(inq)); 45 memset(dis,-1,sizeof(dis)); 46 dis[p]=0; 47 q.push(p); 48 while(!q.empty()) 49 { 50 int u=q.front(); q.pop(); 51 inq[u]=false; 52 for(int i=head[u];i!=-1;i=e[i].next) 53 { 54 if(e[i].h!=-1 && e[i].h<h) continue; 55 int v=e[i].v, w=e[i].w; 56 if(dis[v]==-1 || dis[v]>dis[u]+w) 57 { 58 dis[v]=dis[u]+w; 59 if(!inq[v]) {inq[v]=true; q.push(v);} 60 } 61 } 62 } 63 } 64 int main() 65 { 66 int C=0; 67 while(read_graph()) 68 { 69 int u,v,h; 70 scanf("%d%d%d",&u,&v,&h); 71 int l=1, r=h+1; 72 while(l<r) 73 { 74 int m=(l+r)/2; 75 spfa(u,m); 76 if(dis[v]!=-1) l=m+1; 77 else r=m; 78 } 79 int ansh=l-1; 80 spfa(u,ansh); 81 int ansd=dis[v]; 82 if(C) printf("\n"); 83 printf("Case %d:\n",++C); 84 if(ansh>0) 85 { 86 printf("maximum height = %d\n",ansh); 87 printf("length of shortest route = %d\n",ansd); 88 } 89 else printf("cannot reach destination\n"); 90 } 91 return 0; 92 }