Wormholes
Time limit1sMemory limit128 MB
Given start, destination, and wormholes with entry-time constraints and time shifts, compute the earliest arrival time using a shortest-path style relaxation over travel distances and wormhole jumps.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Simulation
- Solved
- No attempts yet
Problem
A friend of yours has just built a spaceship and wants to explore space with it. On his first voyages he discovered that the universe is full of wormholes. A wormhole transports you instantly from its entry point to its exit point, and it can also shift you to a moment in the past or in the future.
Having mapped every wormhole together with its entry and exit points, you and your friend decide to fly to a distant destination and want to arrive as early as possible. Given your departure point, your destination, and all of the wormholes, determine the earliest time at which you can reach the destination.
Input
The first line contains an integer (), the number of test cases. Each test case is given as follows.
- The first line contains two coordinate triples and : the departure point and the destination.
- The next line contains an integer (), the number of wormholes.
- Each of the next lines describes one wormhole with two coordinate triples (entry) and (exit), followed by two integers and (): the creation time of the wormhole and the time shift applied when travelling through it.
All coordinates are integers with absolute value at most , and no two of the listed points coincide.
Time starts at . The spaceship travels at speed , and the distance between two points is their Euclidean distance rounded up to the nearest integer, that is . Passing through a wormhole is instantaneous: you may enter wormhole only at a time (you may wait before entering), and doing so places you at its exit point at time .
Output
For each test case, print a single line containing one integer: the earliest time at which you can arrive at your destination. This time may be negative.