Runaway Time Machines
Time limit1sMemory limit128 MB
Given a weighted undirected graph and, for each of several machines, a start and a shortest-path distance, decide whether the distinct destinations are uniquely forced.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Brute force, Backtracking
- Solved
- No attempts yet
Problem
Tim the time-traveling salesman owns several time machines that carry him to different points in space-time. The machines navigate a network of wormholes; each wormhole connects two space-time points and takes a fixed amount of time to cross. Tim's machines are fully automated and always follow a shortest path to their destination.
A recent, violent wormhole storm sent some of Tim's machines haywire. Each affected machine loaded the single destination stored in its memory bank and took off. When a machine finished its trip it broadcast a diagnostics signal reporting its starting point and the total time the trip took. Tim no longer remembers which destination each machine was programmed for, but he does know that no two machines were programmed for the same destination.
For each machine you are given its starting point and its total travel time; the machine's destination is a point whose shortest-path distance from the start equals that travel time. Decide whether the destinations of all machines can be uniquely determined.
Input
The first line contains the number of data sets . Then the data sets follow, each in the format below.
The first line of a data set contains three integers , , and (, , ), where is the number of missing machines, is the number of space-time points, and is the number of wormholes. Machines are numbered to and points are numbered to .
Each of the next lines contains three integers , , and : wormhole connects points and and takes seconds to cross in either direction. No wormhole takes more than 1,000 seconds.
Each of the final lines contains two integers and : the starting point and the total travel time in seconds of machine . Every machine is guaranteed to have followed a valid shortest path to some destination, and no two machines share a destination.
Output
For each data set, first output a line containing "Data Set x:", where is the number of the data set (starting from ).
If the destination of every machine is uniquely determined, output the destinations on a single line, from machine to machine , separated by single spaces, with no leading or trailing whitespace. If the destination of one or more machines cannot be uniquely determined, output "impossible" instead.
Separate consecutive data sets with a blank line.