bzoj#P2644. Pku3967 Ideal Path
Pku3967 Ideal Path
题目描述
New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of rooms connected by passages. Each passage is colored into some color . Visitors of the labyrinth are dropped from the helicopter to the room number and their goal is to get to the labyrinth exit located in the room number .
Labyrinth owners are planning to run a contest tomorrow. Several runners will be dropped to the room number . They will run to the room number 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 to the room number that would allow him to win the contest.
Note:
- A sequence () is lexicographically smaller than a sequence () if there exists such that , and for all .
给定无向图(有重边)每条边有一种颜色,边权为 ,让你找到从 到 的最短路,并且经过的边的颜色组成的序列的字典序最小。
输入格式
The first line of the input file contains integers and —the number of rooms and passages, respectively. The following m lines describe passages, each passage is described with three integer numbers: ,and — the numbers of rooms it connects and its color. 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 n from the room number .
输出格式
The first line of the output file must contain — the length of the shortest path from the room number to the room number . The second line must contain numbers — the colors of passages in the order they must be passed in the ideal path.
样例
4 6
1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1
2
1 3
数据规模与约定
对于 的数据,, ,,。
题目来源
Pku3967