This page is still under construction.

Parts of this page are still being built. What you see may change.

The Ideal Path

Time limit1sMemory limit128 MB

Summary
Find the shortest path from node 1 to node n in a colored undirected graph, breaking ties by lexicographically smallest edge-color sequence.
Level

Medium7 of 10

Topics
BFS, Shortest path, Greedy
Solved
No attempts yet

Problem

A new maze has opened at an amusement park. It has n rooms and m two-way corridors connecting the rooms. Each corridor is painted a color ci. The entrance of the maze is room 1 and the exit is room n.

A contestant in the maze-escape contest starts at room 1 and, until reaching room n, writes down the colors of the corridors they walk through, in order. The winner is decided as follows:

  • First, the number of written colors must be as small as possible; that is, you must take a path that uses the fewest corridors.
  • If several paths use the same number of corridors, the one who took the most ideal path wins. A path is called ideal if its sequence of colors is lexicographically smaller than that of every other such path.

Given the maze, write a program that finds the ideal path from room 1 to room n.

Input

The first line contains the number of rooms n and the number of corridors m. (2 ≤ n ≤ 100,000, 1 ≤ m ≤ 200,000)

Each of the next m lines describes one corridor with three integers ai, bi, ci: ai and bi are the two rooms the corridor connects, and ci is its color. (1 ≤ ai, bi ≤ n, 1 ≤ ci ≤ 10^9)

Every corridor is bidirectional. There may be more than one corridor between the same pair of rooms, and there may be a corridor that returns to the same room (ai = bi). It is guaranteed that room n is always reachable from room 1.

Output

On the first line, print the length of the shortest path from room 1 to room n (the number of corridors it uses).

On the second line, print the colors of the ideal path in order, separated by single spaces.

Hint

A sequence (a1, a2, ..., ak) is lexicographically smaller than a sequence (b1, b2, ..., bk) if there exists an index i such that ai < bi and aj = bj for every earlier position j < i.

Examples4

  1. Example 1

    Input
    4 6
    1 2 1
    1 3 2
    3 4 3
    2 3 1
    2 4 4
    3 1 1
    
    Expected output
    2
    1 3
    
  2. Example 2

    Input
    2 1
    1 2 5
    
    Expected output
    1
    5
    
  3. Example 3

    Input
    2 3
    1 2 7
    1 2 3
    1 2 9
    
    Expected output
    1
    3
    
  4. Example 4

    Input
    4 4
    1 2 2
    1 3 1
    2 4 1
    3 4 3
    
    Expected output
    2
    1 3