#5060. Problem 2. Minimum Longest Trip
Problem 2. Minimum Longest Trip
Problem 2. Minimum Longest Trip
USACO 2023 December Contest, Gold
Bessie is going on a trip in Cowland, which has () towns numbered from to and () one-way roads. The th road runs from town to town and has label (, ).
A
trip
of length starting at town is a sequence of towns , such that there is a road from town to town for all . 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 FORMAT (input arrives from the terminal / stdin):
The first line contains and .
The next lines each contain three integers , , and , denoting a road from to with label .
OUTPUT FORMAT (print output to the terminal / stdout):
Output lines. The th should contain two space-separated integers, the length and sum of road labels of Bessie's preferred trip starting at town .
SAMPLE INPUT:
4 5 4 3 10 4 2 10 3 1 10 2 1 10 4 1 10
SAMPLE OUTPUT:
0 0 1 10 1 10 2 20
SAMPLE INPUT:
4 5 4 3 4 4 2 2 3 1 5 2 1 10 4 1 1
SAMPLE OUTPUT:
0 0 1 10 1 5 2 12
In the following explanation, we let represent the road from to with label .
There are several trips starting from vertex , including , , and . Of these trips, and are the longest. These trips each have length 2, and their road label sequences are and , respectively. is the lexicographically smaller sequence, and its sum is .
SAMPLE INPUT:
4 5 4 3 2 4 2 2 3 1 5 2 1 10 4 1 1
SAMPLE OUTPUT:
0 0 1 10 1 5 2 7
SAMPLE INPUT:
4 5 4 3 2 4 2 2 3 1 10 2 1 5 4 1 1
SAMPLE OUTPUT:
0 0 1 5 1 10 2 7
SCORING: Inputs 5-6: All labels are the same.Inputs 7-8: All labels are distinct.Inputs 9-10: Inputs 11-20: No additional constraints.
Problem credits: Claire Zhang and Spencer Compton