PivotOJ

Minimum Longest Trip

시간 제한: 2000ms메모리 제한: 1024MB출처: USACO 2023 December GoldBOJ 31058

문제

Bessie is going on a trip in Cowland, which has NN (2N21052\le N\le 2\cdot 10^5) towns numbered from 11 to NN and MM (1M41051\le M\le 4\cdot 10^5) one-way roads. The iith road runs from town aia_i to town bib_i and has label lil_i (1ai,biN1\le a_i,b_i\le N, 1li1091\le l_i\le 10^9).

A trip of length kk starting at town x0x_0 is a sequence of towns x0,x1,,xkx_0, x_1, \ldots, x_k, such that there is a road from town xix_i to town xi+1x_{i+1} for all 0i<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.

입력

The first line contains NN and MM.

The next MM lines each contain three integers aia_i, bib_i, and lil_i, denoting a road from aia_i to bib_i with label lil_i.

출력

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.

예제

예제 1

입력
4 5
4 3 10
4 2 10
3 1 10
2 1 10
4 1 10
출력
0 0
1 10
1 10
2 20

예제 2

입력
4 5
4 3 4
4 2 2
3 1 5
2 1 10
4 1 1
출력
0 0
1 10
1 5
2 12

예제 3

입력
4 5
4 3 2
4 2 2
3 1 5
2 1 10
4 1 1
출력
0 0
1 10
1 5
2 7

예제 4

입력
4 5
4 3 2
4 2 2
3 1 10
2 1 5
4 1 1
출력
0 0
1 5
1 10
2 7
코드를 제출하려면 로그인하세요.