#P9981. [USACO23DEC] Minimum Longest Trip G

    ID: 11322 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP倍增USACO2023O2优化拓扑排序哈希 hashing

[USACO23DEC] Minimum Longest Trip G

Problem Description

Bessie is traveling on Cowland. Cowland consists of NN cities numbered from 11 to NN (2N21052 \le N \le 2\cdot 10^5) and MM one-way roads (1M41051 \le M \le 4\cdot 10^5). The ii-th road goes from city aia_i to city bib_i and has label lil_i (1li1091 \le l_i \le 10^9).

A trip of length kk starting from city x0x_0 is defined as a sequence of cities x0,x1,,xkx_0, x_1, \ldots, x_k such that for all 0i<k0 \le i < k, there exists a road from city xix_i to xi+1x_{i+1}. It is guaranteed that there is no trip of infinite length in Cowland, and there are no two roads connecting the same ordered pair of cities.

For each city, Bessie wants to know the longest trip starting from it. For some cities, the longest trip starting from them is not unique, so Bessie will choose the one whose road-label sequence is lexicographically smaller. A sequence is lexicographically smaller than another sequence of the same length if and only if, at the first position where they differ, the element in the former sequence is smaller.

Output the length of the trip Bessie chooses for each city and the sum of the road labels along that trip.

Input Format

The first line contains NN and MM.

The next MM lines each contain three integers ai,bi,lia_i, b_i, l_i, representing a one-way road from aia_i to bib_i with label lil_i.

Output Format

Output NN lines. The ii-th line contains two integers separated by a space, representing the length of the trip Bessie chooses starting from city ii and the sum of the road labels along that trip.

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
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
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 5
4 3 2
4 2 2
3 1 10
2 1 5
4 1 1
0 0
1 5
1 10
2 7

Hint

Sample Explanation 2

In the explanation below, we use ailibia_i\xrightarrow{l_i} b_i to denote a one-way road from city aia_i to city bib_i with label lil_i.

Starting from city 44, there are multiple trips, including 443514\xrightarrow{4} 3\xrightarrow 5 1, 4114\xrightarrow 1 1, and 4221014\xrightarrow 2 2\xrightarrow{10} 1. Among these trips, 443514\xrightarrow{4} 3\xrightarrow 5 1 and 4221014\xrightarrow 2 2\xrightarrow{10} 1 are the longest. Their lengths are both 22, and their road-label sequences are [4,5][4,5] and [2,10][2,10], respectively. [2,10][2,10] is lexicographically smaller, and its sum is 1212.

Test Point Properties

  • Test points 565-6 satisfy that all road labels are the same.
  • Test points 787-8 satisfy that all road labels are distinct.
  • Test points 9109-10 satisfy N,M5000N, M \le 5000.
  • Test points 112011-20 have no additional constraints.

Translated by ChatGPT 5