Short roads X50299


Statement
 

pdf   zip

Consider the map of a country, with nn cities (numbered between 00 and n1n-1) and mm unidirectional roads that connect them. Each road has a certain length. We want to go from city 00 to city 11. As we travel with people prone to get carsick, and we do not want to stop halfway to stretch our legs, we want to follow the route such that the longest road we take is as short as possible. That is, if the route uses kk roads, with lengths 1\ell_1, \cdots, k\ell_k, and =max(1,,k)\ell = \max(\ell_1, \cdots, \ell_k), we want \ell to be as small as possible.

Input

The input consists in several cases. Each case begins with nn and mm, followed by mm triplets xx yy \ell, with xyx \neq y, indicating a road that goes from xx to yy of length \ell. Assume 2n1042 \leq n \leq 10^4, 1m10n1 \leq m \leq 10n, that there is at most one road from xx to yy in this order, that the lengths are between 1 and 10510^5, and that there is always a route between 0 and 1.

Output

For each case, write the maximum length of the roads of the best possible route.

The second line of the example of output corresponds to the route 04210 \to 4 \to 2 \to 1, which has a road (the 040 \to 4) of maximum length 80.

Hint

Consider a variant of Dijkstra’s algorithm.

Public test cases
  • Input

    2 2
    0 1 100000
    1 0 42
    
    5 6
    0 1 90
    0 4 80
    4 1 60
    0 2 100
    2 1 60
    4 2 70
    
    

    Output

    100000
    80
    
  • Information
    Author
    Language
    English
    Translator
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++