Description
New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci . Visitors of the labyrinth are dropped from the helicopter to the room number 1 and their goal is to get to the labyrinth exit located in the room number n.
Labyrinth owners are planning to run a contest tomorrow. Several runners will be dropped to the room number 1. They will run to the room number 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 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 1 to the room number n that would allow him to win the contest.
Note:
A sequence is lexicographically smaller than a sequence if there exists i such that , and for all .
Input
The input file contains several test cases, each of them as described below.
The first line of the input file contains integers n and m — the number of rooms and passages, respectively (, ). The following m lines describe passages, each passage is described with three integer numbers: ai,bi , and ci — the numbers of rooms it connects and its color (,
Output
For each test case, the output must follow the description below.
The first line of the output file must contain k — the length of the shortest path from the room number 1 to the room number n. The second line must contain k numbers — the colors of passages in the order they must be passed in the ideal path.
Samples
Input Copy
Output
Source
UVA 1599
如果说从起点开始,一直向终点走,每次只是挑选边权(颜色)最小的走,可能无法到达,也可能找到的不是字典序最小的(考虑长度)
所以说可以先从终点向起点走,求出最短路(每条路径边权为),那么说到达起点的距离便是路径上经过的颜色的个数
倒着求完最短路径之后,就可以利用已经有的来正向寻找答案
细节的地方在于要先倒序求完最短路,然后正向获取答案