Cow Hurdles
Time limit1sMemory limit128 MB
For each of many queries, find the route between two stations that minimizes the tallest hurdle, or report -1 if unreachable.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Binary search, Sorting
- Solved
- No attempts yet
Problem
Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to use as little energy as possible to clear the hurdles.
Jumping over several short hurdles is not very hard for a cow, but a single tall hurdle can be very stressful. Therefore the cows only care about the height of the tallest hurdle they have to jump over.
The practice room has stations, labeled (). A set of one-way paths connects pairs of stations, and the paths are labeled (). Path runs from station to station and contains exactly one hurdle of height (). A cow must jump every hurdle on any path it traverses.
The cows have tasks to complete (). Task consists of two distinct numbers and (, ), meaning a cow must travel from station to station over one or more paths along some route. For each task the cows want a route that minimizes the height of the tallest hurdle they jump over while traveling from to . For every task, determine the route whose tallest hurdle is smallest and report that height.
Input
- Line 1: Three space-separated integers: , , and
- Lines : Line contains three space-separated integers: , , and
- Lines : Line contains two space-separated integers describing task : and
Output
- Lines : Line contains the result for task : the smallest possible maximum hurdle height needed to travel between the two stations. Output if it is impossible to travel between them.