Galaxy Quest
Time limit5sMemory limit1024 MB
For each query, find the least fuel needed to fly from planet 1 to a target planet within a time limit, where each highway's fuel cost depends on how long the trip takes.
- Level
Hard8 of 10
- Topics
- Shortest path, Dynamic programming, Geometry
- Solved
- No attempts yet
Statement
You are travelling through the galaxy in your spaceship. There are planets, numbered from to , and each planet is modelled as a point in 3-dimensional space.
You can travel between these planets along space highways. Each highway follows the straight line that connects its two planets. Your engine can accelerate (or decelerate) at , and it uses 1 litre of fuel per second. There is no limit to how fast you can go, but you must come to a complete standstill each time you arrive at the planet at the end of a highway.
A highway may pass through planets other than the two it connects. Your ship has hyperspace technology, so it phases through those planets without stopping. You cannot switch from one highway to another midway, so every highway must be travelled in full.

Figure G.1: Illustration of the first sample input. Highways are drawn in blue, and the route from planet 1 to planet 3 is shown. The green start of a highway marks acceleration, and the red end marks deceleration. The figure shows the optimal route for the second mission.
You need to fly several missions. In each mission you start at your home planet (planet 1) and must reach a given target planet within a given time limit. For each mission, determine whether it can be completed, and if so, find the least amount of fuel required.
Input
The first line contains three integers , , and (, ), where is the number of planets, is the number of highways, and is the number of missions.
The next lines each contain the coordinates , , and of planet , in metres (, ).
The next lines each contain two integers and (, ), describing a highway that connects planets and . A highway can be traversed in either direction.
The last lines each contain a target planet and a time limit in seconds (, ).
All planets are at distinct locations. No two highways connect the same pair of planets. For each mission, both the absolute and the relative difference between the time limit and the shortest possible completion time are at least .
Output
For each mission, print the least amount of fuel in litres needed to reach the target planet within the time limit. If the target cannot be reached in time, print impossible. Answers are accepted with an absolute or relative error of at most .