Smallest lexicographic path P76480


Statement
 

pdf   zip

Given a DAG GG with nn vertices and mm arcs with unique positive integer labels on the arcs, find the smallest lexicographic path (considering the labels on the arcs, not the numbers of the vertices) between 0 and n1n-1.

A DAG (directed acyclic graph) is a directed graph without cycles. Given two sequences of integers a1,,aka_1, \dots, a_k and b1,,blb_1, \dots, b_l, we say aa is lexicographically smaller than bb when, for the first ii such that aibia_i \ne b_i, we have that ai<bia_i < b_i, or when k<lk < l in case that no such ii exists.

Input

Input consists of several cases. Every case consists of nn and mm, followed by mm triples u,v,wu, v, w meaning that there is an arc from uu to vv with label ww. Assume 2n1052 \le n \le 10^5, 0m5n0 \le m \le 5n, 1w1091 \le w \le 10^9, that vertices are numbered between 0 and n1n-1, uvu \ne v, and that there is no more than one arc from uu to vv. All ww are distinct in every given case.

Output

For every case, print the smallest lexicographic path between 00 and n1n-1. Print the labels separated by spaces. If there is no path between 00 and n1n-1, print -1.

Public test cases
  • Input

    3 3
    0 1 100
    1 2 300
    0 2 200
    
    4 5
    2 3 50
    1 2 20
    0 1 10
    1 3 30
    0 2 40
    
    2 1
    1 0 1000000000
    

    Output

    100 300
    10 20 50
    -1
    
  • Information
    Author
    Miquel Ortega
    Language
    English
    Official solutions
    C++
    User solutions
    C++