Difficult Routes
Time limit1sMemory limit128 MB
Given a 3D road map where each directed edge has a difficulty, find the shortest route from s to t whose maximum edge difficulty equals exactly d.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Binary search, Greedy
- Solved
- No attempts yet
Problem
In preparation for the coming Olympics, you have been asked to propose bicycle training routes for your national cycling team. The training committee wants to identify routes between pairs of locations at several sites around the country. Each route must have a desired level of difficulty based on the steepness of its hills.
You are given a road map with elevation data superimposed on it. Each intersection, where two or more roads meet, is identified by its -, -, and -coordinates. Each road starts and ends at an intersection, is straight, and does not contain bridges over or tunnels under other roads. A road may be cycled in either direction, and its difficulty depends on the direction of travel.
The difficulty level of cycling a road is if the road is level or is travelled in the downhill direction. The difficulty of a non-level road travelled in the uphill direction is , where rise is the absolute change in elevation and run is the distance between its two endpoints in their horizontal projection onto the plane at elevation zero. (Cycling a descending road always has difficulty .)
The length of a road is the straight-line (3D Euclidean) distance between its two endpoints. A route is a sequence of roads in which each road continues from the intersection where the previous road ended; the length of a route is the sum of the lengths of its roads. A route has difficulty if the maximum difficulty among all of its roads equals . For a chosen pair of locations, the committee wants the route with the required difficulty that has the shortest possible total length.
Reminder: the floor function means truncated to an integer.
The figure below shows a road map with three intersections, corresponding to the sample input.

The edge labels on the darker shaded surface give the uphill difficulty levels. The lighter shaded surface is the horizontal projection onto the plane at elevation zero.
Input
The input consists of several road maps. Each map begins with two non-negative integers and on a line by themselves, separated by a space, giving the number of intersections and the number of roads, respectively (). A line containing and marks the end of the input.
Each of the next lines contains three integers, separated by single spaces, giving the -, -, and -coordinates of an intersection. Each coordinate is between and , inclusive. Intersections are numbered starting from in the order they appear. Each of the following lines contains two integers, the two endpoint intersections of a road (a road may be cycled in either direction).
Finally, a line with three integers , , and gives the desired start intersection , finish intersection , and required difficulty level for the training route (). A valid training route must contain at least one road of difficulty exactly and no road of difficulty greater than . If the route is a closed circuit, then and are the same intersection.
Output
For each road map, print a single line containing either:
- the shortest length of a valid training route, rounded to exactly three decimal places (always printing all three decimal digits); or
- the single word
Noneif no feasible route exists.
Hint
Reminder — how to round a positive number of the form R.xxxy to three decimal places:
- If the fourth decimal digit
yis less than 5, the result isR.xxx. - Otherwise, the result is
R.xxx+ 0.001.
For example, 10.3463 is printed as 10.346, and 10.3695 is printed as 10.370.