题目链接
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.
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 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
Sample Output
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
n个城市,m条路径,路径有长度和载货最高高度,-1是无限高度。卡车载货高度高于路径的载货最高高度就不能通过。求卡车最高的载货高度和最短路径。
用二分列出一系列的高度,假设卡车的高度就是二分得到的数,然后用dijkstra计算最小路径(如果是inf就是不能到达)。
这道题目是前几天西安邀请赛的类似的题目。当时用的DFS暴力搜索出全部从起点到终点的路径,然后计算结果ans,求个最优的ans。但是超时了.....听说队长用BFS就过了........后来找了HDU这道类似题目用DFS和BFS写,提交,果然DFS还是超时,可能是递归调用太费时间??BFS内存超了。不过ACM好像内存有512MB,不太会爆内存,然后那道M题用BFS就过了= = ,DFS就是超时。我......
上面是BFS,下面是DFS,使用二分+搜索写的,不用二分也差不多....HDU对内存限制了,二分+BFS内存超了一点。所以得用dijkstra。
对DFS、BFS和dijkstra的理解:DFS和BFS类似,但是DFS超时严重,以后用BFS!!DFS和BFS虽然慢,但是他们可以实现的功能比dijkstra强太多,dijkstra只能求最短路,DFS和BFS不止是图的搜索了。
这种题目是图的搜索,但是有一些限制条件,然后求个最优的解。方法就是二分+dijkstra。二分列出可能的最优解,用dijkstra去判断是否可行。而且这个二分的范围是递增的,比如说1-10,1-8用dijkstra判断都是可行的,9和10是不可行的,最大的就是8。即大于某个数都是不可行,小于某个数都是可行。这是二分的基础。
另外Presentation Error的错误,需要注意最后一个测试的后面没有\n,所以需要对测试的个数T进行判断,T!=1时在输出之前输出一个\n 。
代码参考:https://blog.csdn.net/Lionel_D/article/details/44874443