Under construction

Still being built. Things may change

Minimum Longest Trip

Time limit2sMemory limit1024 MB

Summary
For each town, find the longest trip on one-way roads with no cycles, breaking ties by the smallest label sequence, and report its length and label sum.
Level

Medium6 of 10

Topics
Dynamic programming, Topological sort, Graph, Sorting
Solved
No attempts yet

Problem

Bessie is going on a trip in Cowland, which has NN (2≤N≤2⋅1052\le N\le 2\cdot 10^5) towns numbered from 11 to NN and MM (1≤M≤4⋅1051\le M\le 4\cdot 10^5) one-way roads. The iith road runs from town a_ia\_i to town b_ib\_i and has label l_il\_i (1≤a_i,b_i≤N1\le a\_i,b\_i\le N, 1≤l_i≤1091\le l\_i\le 10^9).

A trip of length kk starting at town x_0x\_0 is a sequence of towns x_0,x_1,…,x_kx\_0, x\_1, \ldots, x\_k, such that there is a road from town x_ix\_i to town x_i+1x\_{i+1} for all 0≤i<k0\le i < k. It is guaranteed that there are no trips of infinite length in Cowland, and that no two roads connect the same pair of towns.

For each town, Bessie wants to know the longest possible trip starting at it. For some starting towns, there are multiple longest trips. Out of these, she prefers the trip with the lexicographically minimum sequence of road labels. A sequence is lexicographically smaller than another sequence of the same length if, at the first position in which they differ, the first sequence has a smaller element than the second sequence.

Output the length and sum of road labels of Bessie's preferred trip starting at each town.

Input

The first line contains NN and MM.

The next MM lines each contain three integers a_ia\_i, b_ib\_i, and l_il\_i, denoting a road from a_ia\_i to b_ib\_i with label l_il\_i.

Output

Output NN lines. The iith should contain two space-separated integers, the length and sum of road labels of Bessie's preferred trip starting at town ii.

Examples4

  1. Example 1

    Input
    4 5
    4 3 10
    4 2 10
    3 1 10
    2 1 10
    4 1 10
    
    Expected output
    0 0
    1 10
    1 10
    2 20
    
  2. Example 2

    Input
    4 5
    4 3 4
    4 2 2
    3 1 5
    2 1 10
    4 1 1
    
    Expected output
    0 0
    1 10
    1 5
    2 12
    
  3. Example 3

    Input
    4 5
    4 3 2
    4 2 2
    3 1 5
    2 1 10
    4 1 1
    
    Expected output
    0 0
    1 10
    1 5
    2 7
    
  4. Example 4

    Input
    4 5
    4 3 2
    4 2 2
    3 1 10
    2 1 5
    4 1 1
    
    Expected output
    0 0
    1 5
    1 10
    2 7