The Neptune Adventure
Time limit1sMemory limit128 MB
Given a directed graph with travel times and a flood time per location, find the earliest safe arrival time from S to R, or report failure.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Greedy
- Solved
- No attempts yet
Problem
You are aboard the luxury liner Neptune, which is flooding fast. Find out whether you can escape the ship and reach the rescue team before you drown.
The ship's layout is described by a set of locations (cabins, lounges, and other rooms) and directed paths (hallways, elevators, Christmas trees, and stairs) between them, each with a travel time. You must find the shortest time it takes to get from your starting location to the rescue team.
A series of explosions floods the ship. Each location floods at a fixed time, and when a location floods, every path leading into or out of it floods as well. If you are inside a location, or on a path, at the very moment it floods, you drown. You may not travel through a location once it has flooded.
You win ties: if you reach a location that is not yet flooded at the exact moment the path you were traveling on floods, you survive; and if you reach the rescue team's location at the exact moment that location floods, you also survive.
Input
The first line contains a single integer (), the number of data sets. Each data set is given as follows.
- The first line contains three integers , , and : () is the number of locations, () is your starting location, and () is the rescue team's location.
- The next lines describe the locations in order, starting with location . Line contains the integers :
- () is the time (in minutes, starting from time ) at which location floods. means location never floods.
- () is the time in minutes to travel from location to location . means there is no path from location to location . In particular, the self entry is .
Paths are directed: the time from location to location need not equal the time from location to location , and some paths exist in only one direction.
Output
For each data set, print a single line with the shortest time in minutes it takes to reach the rescue team from your starting location without drowning. If it is impossible to reach the rescue team, print GENE HACKMAN instead.