#P9981. [USACO23DEC] Minimum Longest Trip G
[USACO23DEC] Minimum Longest Trip G
Problem Description
Bessie is traveling on Cowland. Cowland consists of cities numbered from to () and one-way roads (). The -th road goes from city to city and has label ().
A trip of length starting from city is defined as a sequence of cities such that for all , there exists a road from city to . 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 and .
The next lines each contain three integers , representing a one-way road from to with label .
Output Format
Output lines. The -th line contains two integers separated by a space, representing the length of the trip Bessie chooses starting from city 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 to denote a one-way road from city to city with label .
Starting from city , there are multiple trips, including , , and . Among these trips, and are the longest. Their lengths are both , and their road-label sequences are and , respectively. is lexicographically smaller, and its sum is .
Test Point Properties
- Test points satisfy that all road labels are the same.
- Test points satisfy that all road labels are distinct.
- Test points satisfy .
- Test points have no additional constraints.
Translated by ChatGPT 5