luogu#P4802. [CCO 2015] 路短最
[CCO 2015] 路短最
Problem Description
This problem is translated from CCO 2015 Day 1 T2 “Artskjid”.
You can use many algorithms to find the shortest path from one place to another. People install GPS devices in their cars, and then their phones tell them the fastest way to reach the destination. However, when on vacation, Troy likes to travel slowly. He wants to find the longest path to the destination so that he can see many new and interesting places along the way.
Therefore, a valid path consists of a sequence of distinct cities , and for each , there is a road from to .
He does not want to visit any city more than once. Please help him find the longest path.
Input Format
The first line contains two integers , representing the number of cities and the number of roads between cities. There is at most one road between any pair of cities. Cities are numbered from to . Troy starts at city , and city is his destination.
The next lines each contain three integers . Each triple indicates that there is a directed road of length from city to city . Each road is one-way: you can only travel from to , not in the reverse direction. It is guaranteed that there is a path from city to city .
Output Format
Output one integer: the length of the longest path that starts at city and ends at city , without visiting any city more than once. The path length is the sum of the lengths of the roads used.
3 3
0 2 5
0 1 4
1 2 3
7
Hint
The shortest path is to take the road directly from city to city , with length km. The longest path is , with length km.
For at least of the testdata, .
For of the testdata, , , , , .
Translated by ChatGPT 5