eolymp
bolt
Try our new interface for solving problems
Problems

Locks and keys

Locks and keys

A wizard is in a labyrinth where there are \textbf{V} rooms and \textbf{V} - \textbf{1} doors connecting some pairs of rooms in both directions, in such a way that there is always a sequence of doors one can traverse to go from a room to any other room. Additionally, there are \textbf{C} locks and \textbf{C} keys of \textbf{C} different colours (one of each) in some of the doors and rooms of the maze, respectively; each door has at most one lock, and there is at most one key placed in each room. It should be an easy matter for the wizard to bypass the lock system, were it not for the fact that he forgot his spell book, without which his wizardry is utterly useless. The wizard is currently in room \textbf{X}, and he wants to get his spell book, located in room \textbf{Y}, without taking too long. At every step he may go to an adjacent room through one of the doors. Of course, if the door is locked, he needs to be carrying the key of the same colour as the lock (unless, of course, that door has already been unlocked). The wizard can carry only one key at a time and after picking up a key it is not possible for him to drop it somewhere in the maze in order to take it again afterwards. Once a door has been unlocked, the key is thrown away since it is no longer any use. Given the maze and the positions of the \textbf{C} keys and \textbf{C} locks, determine how to reach \textbf{Y} from \textbf{X}, if possible. Any path whose length does not exceed \textbf{4}^\{ . \}(\textbf{C} + 1)^\{ . \}\textbf{V} is acceptable. \InputFile The first line of each case contains four integers: the number \textbf{V} of rooms in the maze (\textbf{1} ≤ \textbf{V} ≤ \textbf{1500}), the number \textbf{C} of locks (\textbf{0} ≤ \textbf{C }< \textbf{V}), and rooms \textbf{X} and \textbf{Y} numbered \textbf{0}, \textbf{1},..., \textbf{V} - \textbf{1}. Then comes a (possibly empty) line with \textbf{C} integers indicating the location of each of the keys, in order of increasing colour. The next \textbf{V} - \textbf{1} lines describe the maze: each contains three integers \textbf{A B L}, meaning that there is a door between rooms \textbf{A} and \textbf{B} which can be unlocked with the key of colour \textbf{L}, if \textbf{0} ≤ \textbf{L} < \textbf{C}; a value of \textbf{-1} for \textbf{L} indicates that no lock is needed. The last line has \textbf{V}, \textbf{C}, \textbf{X}, \textbf{Y} = \textbf{0}, \textbf{0}, \textbf{0}, \textbf{0}. \OutputFile Impossible There is one line of output per test case. If there is no solution, output ''. Otherwise print the full path corresponding to your solution in the format \textbf{L}: \textbf{V_0},...,\textbf{V_L}, where \textbf{L} ≤ \textbf{4}(\textbf{C} + \textbf{1})\textbf{V} is the length of a path from \textbf{X} to \textbf{Y}, and \textbf{X} = \textbf{V_0}, \textbf{V_1},..., \textbf{V_L}_\{-1\}, \textbf{V_L} = \textbf{Y} is the sequence of \textbf{L} + \textbf{1} vertices visited in the right order. A single space must precede each vertex in the path; see sample output for clarification.
Time limit 10 seconds
Memory limit 256 MiB
Input example #1
1 0 0 0


3 1 0 2
1
0 1 -1
0 2 0

3 2 0 2
1 2
0 1 1
0 2 0

5 3 0 4
2 0 3
0 1 0
0 2 -1
1 3 1
2 4 2

0 0 0 0
Output example #1
0: 0
3: 0 1 0 2
Impossible
10: 0 2 0 1 0 1 3 1 0 2 4