Detour
Time limit2sMemory limit512 MB
In a graph with unique shortest paths to node 1, a sign at each node points along its shortest path; find a simple 0-to-1 path never taking a signed road, minimizing length then lexicographic order.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, BFS, Greedy
- Solved
- No attempts yet
Problem
You travel from Delft to Amsterdam by bus. Every intersection along the way carries a traffic sign that points in the direction of the shortest route to Amsterdam. This bus never takes the road a sign points to.
Decide whether such a bus route exists. A bus route starts in Delft, ends in Amsterdam, and never visits the same intersection twice. At every intersection of the route other than Amsterdam, the road the bus takes next must differ from the road the sign at that intersection points to.
The road network is a simple connected undirected graph, and the shortest route to Amsterdam is unique from every intersection. The sign at an intersection points to the first road of that route.
Input
The first line has two integers, the number of intersections () and the number of undirected roads (). Intersections are numbered to . Delft is intersection and Amsterdam is intersection .
Each of the next lines describes one road with three integers , (, ) and (). The road connects intersection and intersection , and its length is in either direction.
Output
If no route satisfies the requirement, print impossible.
Otherwise print the route on one line. Print the number of intersections on the route, then the intersection numbers in the order the bus passes them, where and .
Several routes can satisfy the requirement. Print the one that passes through the fewest intersections. If several routes tie, print the one whose sequence is lexicographically smallest.