Problem D

Languages en ja

After last year’s edition of the BAPC, you are still stuck in Delft. In order to participate again this year, you are going to Amsterdam by bus. During the journey you look out of the window and look for traffic signs that point in the direction of Amsterdam. To your surprise, you notice that the bus is never taking the roads that are pointed out by the signs!

You think that the bus company might have chosen a route such that, at no intersection, the bus goes in the direction that is pointed to by the signs. Your friends, however, find this very unbelievable, and don’t think this is possible. Can you figure out whether there exists a bus route that satisfies this requirement? Note that a bus route never visits the same place twice.

A traffic sign pointing in the direction of the shortest route to Amsterdam is placed at every intersection. You may assume that the input graph is both simple and connected, and that there is a unique optimal route to Amsterdam from every intersection.


  • A single line containing two integers: $n$ ($2 \le n \le 10^5$), the number of intersections, and $m$ ($1 \le m \le 10^6$), the number of undirected roads that connect the intersections. The intersections are numbered from $0$ to $n-1$. Delft is denoted by intersection $i=0$ and Amsterdam is denoted by intersection $i=1$.

  • $m$ lines that specify the roads

    • A road is specified by three integers, $a_ i$, $b_ i$ ($0 \leq a_ i, b_ i < n$ and $a_ i \ne b_ i$) and $d_ i$ ($0 \le d_ i \leq 500000$), where $a_ i$ and $b_ i$ are ids of the two intersections that are connected by this road and $d_ i$ is the distance that the bus has to travel to get from $a_ i$ to $b_ i$ or vice versa.


As output, give one of the following:

  • A path from Delft to Amsterdam that satisfies the requirements, in case such a path exists.

    • A path is specified by a single line containing an integer $k$, the length of the path, followed by $k$ integers $p_ i$ that specify the intersections along the path in the order in which they are crossed, with $p_0 = 0$ and $p_{k-1}=1$.

  • The text “impossible”, if such a path does not exist.

Sample Input 1 Sample Output 1
4 5
0 2 5
2 1 5
0 3 10
3 1 20
3 2 5
3 0 3 1
Sample Input 2 Sample Output 2
4 3
0 1 10
1 2 20
2 3 30
Sample Input 3 Sample Output 3
7 9
0 1 800
6 2 300
2 3 75
3 4 80
4 5 50
4 1 100
1 6 35
0 2 120
0 3 100
2 0 1

Please log in to submit a solution to this problem

Log in