This page is still under construction.

Parts of this page are still being built. What you see may change.

Runaway Time Machines

Time limit1sMemory limit128 MB

Summary
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 KK. Then the KK data sets follow, each in the format below.

The first line of a data set contains three integers MM, NN, and WW (1≤M≤201 \le M \le 20, 2≤N≤1002 \le N \le 100, N−1≤W≤500N - 1 \le W \le 500), where MM is the number of missing machines, NN is the number of space-time points, and WW is the number of wormholes. Machines are numbered 11 to MM and points are numbered 11 to NN.

Each of the next WW lines contains three integers aia_i, bib_i, and cic_i: wormhole ii connects points aia_i and bib_i and takes cic_i seconds to cross in either direction. No wormhole takes more than 1,000 seconds.

Each of the final MM lines contains two integers sis_i and tit_i: the starting point and the total travel time in seconds of machine ii. 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 xx is the number of the data set (starting from 11).

If the destination of every machine is uniquely determined, output the destinations on a single line, from machine 11 to machine MM, 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.

Examples1

  1. Example 1

    Input
    2
    2 4 4
    1 2 5
    1 4 5
    2 3 4
    3 4 3
    1 5
    2 7
    1 3 2
    1 2 4
    1 3 4
    1 4
    
    Expected output
    Data Set 1:
    2 4
    
    Data Set 2:
    impossible