题目大意:题目给定了m条路,从a到b限制通过的高度为h,距离为l
现在要求从s到t,车能载的高度最高为h,求车能从s到t载的最高高度为多少,最短路距离是多少
分析:对车能载的高度进行二分,然后求最短路(描述能力好差,粘别人的叙述)
/****************************** * author :crazy_石头 * data structure: SPFA+二分 * created time:2013/11/6 11:11 * Pro:HDU 1142 * Judge Status:Accepted * Memory:596K * Time:171MS *******************************/ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define rep(i,h,n) for(int i=(h);i<=(n);i++) #define INF 0xfffffff const int maxn=1000+5; struct Edge { int to,next; int h,cost; }edge[maxn*maxn]; int n,m,cnt,mid; int s,e; int dis[maxn],vis[maxn],head[maxn]; inline void addedge(int u,int v,int h,int w) { edge[cnt].to=v; edge[cnt].next=head[u]; edge[cnt].h=h; edge[cnt].cost=w; head[u]=cnt++; } inline void makemap(int u,int v,int h,int w) { addedge(u,v,h,w); addedge(v,u,h,w); } inline int SPFA(int s,int mid) { queue<int> q; rep(i,1,n) dis[i]=INF; memset(vis,0,sizeof(vis)); dis[s]=0; vis[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];~i;i=edge[i].next) { int v=edge[i].to; if(dis[v]>dis[u]+edge[i].cost&&mid<=edge[i].h) { dis[v]=dis[u]+edge[i].cost; if(!vis[v]) { vis[v]=1; q.push(v); } } } } return dis[e]!=INF; } inline void init() { memset(head,-1,sizeof(head)); cnt=0; } int main() { int test=0; int cur; while(~scanf("%d%d",&n,&m),n,m) { int u,v,w,h; init(); while(m--) { scanf("%d%d%d%d",&u,&v,&h,&w); if(h==-1) h=INF; makemap(u,v,h,w); } int l=0,r,res=INF; scanf("%d%d%d",&s,&e,&r); while(l<=r) { mid=(l+r)>>1; if(SPFA(s,mid)) { l=mid+1; res=dis[e]; cur=mid; } else r=mid-1; } if(test) printf("\n"); printf("Case %d:\n",++test); if(res!=INF) { printf("maximum height = %d\n",cur); printf("length of shortest route = %d\n",res); } else printf("cannot reach destination\n"); } return 0; } |
* This source code was highlighted by YcdoiT. ( style: Desert )