标签:
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include<queue> using namespace std ; typedef long long ll; const int N = 2000000 + 10; const int inf = 1e9 + 7; int dis[N],head[N],vis[N],t,n,m,T; struct ss{ int to,h,v,next; }e[N]; void add(int u,int v,int h,int w) { e[t].to = v; e[t].next = head[u]; e[t].v = w; e[t].h = h; head[u] = t++; } int spfa(int x,int limt) { queue<int >q; for(int i = 0; i <= n; i++) dis[i] = inf, vis[i] = 0; dis[x] = 0; q.push(x); vis[x] = 1; while(!q.empty()) { int k = q.front(); q.pop();vis[k] = 0; for(int i = head[k]; i; i = e[i].next) { if(e[i].h < limt) continue; if(dis[e[i].to] > dis[k] + e[i].v) { dis[e[i].to] = dis[k] + e[i].v; if(!vis[e[i].to]) { vis[e[i].to] = 1; q.push(e[i].to); } } } } return dis[T]; } int main() { int a,b,h,v,S,cas = 1; while(~scanf("%d%d",&n,&m)) { if(!n || !m) break; if (cas > 1) printf ("\n"); t = 1; memset(head,0,sizeof(head)); for(int i = 1; i <= m; i++) { scanf("%d%d%d%d",&a,&b,&h,&v); if(h == -1) h = inf; add(a,b,h,v); add(b,a,h,v); } scanf("%d%d%d",&S,&T,&h); int l = 0, r = h, ans = inf; while(l < r) { int mid = (l + r + 1) >> 1; if(spfa(S,mid) != inf) l = mid, ans = dis[T]; else r = mid - 1; } printf ("Case %d:\n", cas++); if(ans != inf) printf ("maximum height = %d\nlength of shortest route = %d\n", l, ans); else { printf("cannot reach destination\n"); } } return 0; }
UVALive 4223 / HDU 2962 spfa + 二分
标签:
原文地址:http://www.cnblogs.com/zxhl/p/5152613.html