eolymp
bolt
Try our new interface for solving problems
Problems

Ideal Path

Ideal Path

New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of \textbf{n} rooms connected by \textbf{m}passages. Each passage is colored into some color \textbf{c_i}. Visitors of the labyrinth are dropped from the helicopter to the room number \textbf{1} and their goal is to get to the labyrinth exit located in the room number \textbf{n}. Labyrinth owners are planning to run a contest tomorrow. Several runners will be dropped to the room number \textbf{1}. They will run to the room number \textbf{n} writing down colors of passages as they run through them. The contestant with the shortest sequence of colors is the winner of the contest. If there are several contestants with the same sequence length, the one with the ideal path is the winner. The path is the \textit{ideal path} if its color sequence is the lexicographically smallest among shortest paths. Andrew is preparing for the contest. He took a helicopter tour above New Lostland and made a picture of the labyrinth. Your task is to help him find the ideal path from the room number \textbf{1} to the room number \textbf{n} that would allow him to win the contest. \InputFile The first line of the input file contains integers \textbf{n} and \textbf{m} --- the number of rooms and passages, respectively (\textbf{2} ≤\textbf{n} ≤ \textbf{100000}, \textbf{1} ≤ \textbf{m} ≤ \textbf{200000}). The following \textbf{m} lines describe passages, each passage is described with three integer numbers: \textbf{a_i}, \textbf{b_i}, and \textbf{c_i} --- the numbers of rooms it connects and its color (\textbf{1} ≤ \textbf{a_i}, \textbf{b_i} ≤ \textbf{n}, \textbf{1} ≤ \textbf{c_i} ≤ \textbf{10^9}). Each passage can be passed in either direction. Two rooms can be connected with more than one passage, there can be a passage from a room to itself. It is guaranteed that it is possible to reach the room number \textbf{n} from the room number \textbf{1}. \OutputFile The first line of the output file must contain \textbf{k} --- the length of the shortest path from the room number \textbf{1} to the room number \textbf{n}. The second line must contain \textbf{k} numbers --- the colors of passages in the order they must be passed in the ideal path.
Time limit 2 seconds
Memory limit 256 MiB
Input example #1
4 6
1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1
Output example #1
2
1 3