HDU 2962 Trucking(二分+带限制最短路)
来源:互联网 发布:软件开发专业技能 编辑:程序博客网 时间:2024/05/16 09:02
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2962
题目:
Trucking
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1211 Accepted Submission(s): 428
Problem Description
A certain local trucking company would like to transport some goods on a cargo truck from one place to another. It is desirable to transport as much goods as possible each trip. Unfortunately, one cannot always use the roads in the shortest route: some roads may have obstacles (e.g. bridge overpass, tunnels) which limit heights of the goods transported. Therefore, the company would like to transport as much as possible each trip, and then choose the shortest route that can be used to transport that amount.
For the given cargo truck, maximizing the height of the goods transported is equivalent to maximizing the amount of goods transported. For safety reasons, there is a certain height limit for the cargo truck which cannot be exceeded.
For the given cargo truck, maximizing the height of the goods transported is equivalent to maximizing the amount of goods transported. For safety reasons, there is a certain height limit for the cargo truck which cannot be exceeded.
Input
The input consists of a number of cases. Each case starts with two integers, separated by a space, on a line. These two integers are the number of cities (C) and the number of roads (R). There are at most 1000 cities, numbered from 1. This is followed by R lines each containing the city numbers of the cities connected by that road, the maximum height allowed on that road, and the length of that road. The maximum height for each road is a positive integer, except that a height of -1 indicates that there is no height limit on that road. The length of each road is a positive integer at most 1000. Every road can be travelled in both directions, and there is at most one road connecting each distinct pair of cities. Finally, the last line of each case consists of the start and end city numbers, as well as the height limit (a positive integer) of the cargo truck. The input terminates when C = R = 0.
Output
For each case, print the case number followed by the maximum height of the cargo truck allowed and the length of the shortest route. Use the format as shown in the sample output. If it is not possible to reach the end city from the start city, print "cannot reach destination" after the case number. Print a blank line between the output of the cases.
Sample Input
5 61 2 7 51 3 4 22 4 -1 102 5 2 43 4 10 14 5 8 51 5 105 61 2 7 51 3 4 22 4 -1 102 5 2 43 4 10 14 5 8 51 5 43 11 2 -1 1001 3 100 0
Sample Output
Case 1:maximum height = 7length of shortest route = 20Case 2:maximum height = 4length of shortest route = 8Case 3:cannot reach destination
题目大意:
在一个地图上,每条路径有长度,以及它的限制高度。 一辆卡车要从A地到B地, 卡车的最大能装h高度的货物。 求这辆卡车从A到B所能装下的最高货物的时候, 最短路径是多少。
分析与总结:
和 HDU 1839 相类似。 卡车载的货物高度h从小到大依次枚举时,满足条件的A到B的路径数量是依次递减的,直到没有路径为止。因此满足单调性,可以用二分法对卡车的载货量进行二分,在求最短路。 如果能够求出最短路,那么left=mid+1, 否则right=mid.
注意,能够求出最短路的同时,还要保存下答案。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int INF = 0x7fffffff;const int VN = 1005;const int EN = VN*VN/2;struct Edge{ int v,next; int h, len;}E[EN];int n;int m;int size;int head[VN];int d[VN];int limit;bool inq[VN];void init(){ size=0; memset(head, -1, sizeof(head));}void addEdge(int u,int v,int h,int l){ E[size].v=v; if(h!=-1)E[size].h=h; else E[size].h=INF; E[size].len=l; E[size].next=head[u]; head[u]=size++;}int SPFA(int src, int end){ for(int i=1; i<=n; ++i)d[i]=INF; memset(inq, 0, sizeof(inq)); queue<int>q; d[src] = 0; q.push(src); while(!q.empty()){ int u=q.front(); q.pop(); inq[u]=false; for(int e=head[u]; e!=-1; e=E[e].next)if(E[e].h>=limit){ int tmp = d[u]+E[e].len; if(d[E[e].v] > tmp){ d[E[e].v] = tmp; if(!inq[E[e].v]){ inq[E[e].v]=true; q.push(E[e].v); } } } } return d[end];}int main(){ int cas=1,u,v,h,l,start,end,hh; while(~scanf("%d%d",&n,&m) && n+m){ if(cas!=1)puts(""); init(); for(int i=0; i<m; ++i){ scanf("%d%d%d%d",&u,&v,&h,&l); addEdge(u,v,h,l); addEdge(v,u,h,l); } scanf("%d%d%d",&start,&end,&hh); int ans,ans_h=0,ans_len=INF,left=0, right=hh+1; while(left<right){ limit = (left+right)>>1; ans=SPFA(start, end); if(ans!=INF){ left=limit+1; if(limit>ans_h){ ans_h=limit; ans_len=ans; } else if(limit==ans_h&&ans<ans_len){ ans_len=ans; } } else{ right=limit; } } printf("Case %d:\n",cas++); if(ans_len==INF){ puts("cannot reach destination"); } else{ printf("maximum height = %d\n", ans_h); printf("length of shortest route = %d\n", ans_len); } } return 0;}
—— 生命的意义,在于赋予它意义。
原创 http://blog.csdn.net/shuangde800 , By D_Double (转载请标明)
- HDU 2962 Trucking(二分+带限制最短路)
- HDU 2962 Trucking 二分+最短路(带限制最短路)\\不满足递推关系,不能直接是spfa
- HDU 2962 Trucking 最短路+二分
- 【HDU】2962 Trucking 二分+最短路
- hdu 2962 Trucking (二分+最短路Spfa)
- hdu 2962 Trucking(二分+最短路)
- HDU 2962 Trucking 二分+最短路
- HDU 2962 Trucking 最短路
- HDU-2962-Trucking(最短路)
- HDU 2962 Trucking 最短路。。
- HDU 2962 Trucking 最短路
- hdu 2962 Trucking (最短路之SPFA算法 + 二分)
- hdu 2962 Trucking 最短路+二分。。Dijkstra+SPFA两种算法实现。
- hdu 2962 Trucking(二分最大可行的高度+最短路dijkstra)
- HDU 1839 Delay Constrained 带限制最短路:二分下限
- 【最短路+dijkstra+spfa】杭电 hdu 2962 Trucking
- HDU 2363 Cycling 二分+ 限制最短路
- hdu 2962 Trucking 二分 + Dijsktra
- 【Web缓存机制概述】2 – Web浏览器的缓存机制
- 搬家
- 与RTX相关的进程与线程基础知识
- 【Web缓存机制概述】3 – 如何构建可缓存站点
- 【Web缓存机制概述】4 – HTML5时代的Web缓存机制
- HDU 2962 Trucking(二分+带限制最短路)
- 诺基亚CEO遭前高管炮轰:应辞职 战略大溃败
- 中国物联网国家标准草案年内完成
- C#中string字符串处理
- 【Web缓存机制概述】5 – Web App时代的缓存机制新思路 & 全文总结
- 查询Oracle正在执行和执行过的SQL语句
- WinCC flexible 2008 SP4 奇怪现象1
- 给数据减肥 让MySQL数据库跑的更快
- 有用的命令