UVALive 4223 / HDU 2962 spfa + 二分
2016-01-22 23:20
363 查看
Trucking
[align=left][b]Problem Description[/b][/align]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.
[align=left][b]Input[/b][/align]
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.
[align=left][b]Output[/b][/align]
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.
[align=left][b]Sample Input[/b][/align]
5 6
1 2 7 5
1 3 4 2
2 4 -1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 10
5 6
1 2 7 5
1 3 4 2
2 4 -1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 4
3 1
1 2 -1 100
1 3 10
0 0
[align=left][b]Sample Output[/b][/align]
Case 1:
maximum height = 7
length of shortest route = 20
Case 2:
maximum height = 4
length of shortest route = 8
Case 3:
cannot reach destination
[align=left][b]题意:[/b][/align]
[align=left] 给出一无向图 每条路对卡车的高度都有限制 求从起点到终点 卡车最高的高度及行进的最短路 [/align]
[align=left] [/align]
[align=left][b]题解:[/b][/align]
[align=left] 我们二分高度, [/align]
[align=left] 在这个高度下进行一次最短路,解决是否能到达 终点,能的话记录 路径长度[/align]
[align=left] 更新答案[/align]
#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 ,head ,vis ,t,n,m,T; struct ss{ int to,h,v,next; }e ; 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; }
相关文章推荐
- 嵌入式系统学习入门篇之-----开门探头
- 小乔落花流水
- 如何在网页中加入title图标
- 【Web前端】Js利用正则表达式提取字符串
- Android keystore 的MD5、SHA1值获取方法
- QT事件
- repeatCount和repeatDuration
- 间断捕捉格式介绍
- codeforces 257C解题报告
- MySQL只有information_schema,test两个数据库
- 槽函数的好处
- 使用HTML 和CSS 开发商业站点
- bzoj3280: 小R的烦恼
- 批量执行语句之——禁用所有表的外键
- 2016.01.21-------html5第二天
- typedef和#define的用法与区别
- Fragment(碎片)实用案例
- 使用C#开发数据库应用系统
- 大一下学期的自我目标
- aidl的简单使用