Canadian Computing Olympiad: 2015 Day 1, Problem 2
There are many well-known algorithms for finding the shortest route from one location to another. People have GPS devices in their cars and in their phones to show them the fastest way to get where they want to go. While on vacation, however, Troy likes to travel slowly. He would like to take the longest route to his destination so that he can visit many new and interesting places along the way.
As such, a valid route consists of a sequence of distinct cities, , such that there is a road from to for each .
He does not want to visit any city more than once. Can you help him find the longest route?
The first line of input contains two integers , the total number of cities and the number of roads connecting the cities (; ). There is at most one road from any given city to any other given city. Cities are numbered from to , with being Troy's starting city, and being his destination.
The next lines of input each contain three integers . Each triple indicates that there is a one way road from city to city of length km (; ; ; ). Each road is one-way: it can only be taken from to , not vice versa. There is always at least one route from city to city .
For at least 30% of the marks for this problem, .
Output a single integer, the length of the longest route that starts in city , ends in city , and does not visit any city more than once. The length is the sum of the lengths of the roads taken along the route.
3 3 0 2 5 0 1 4 1 2 3
Output for Sample Input
Explanation of Output for Sample Input
The shortest route would be to take the road directly from to , of length km. The route going from to to is km, which is longer.