Bicycle Race
Time limit8sMemory limit256 MB
Find two triangles that share one host city and have five distinct cities in total, maximizing the sum of their six road values, or report -1 if none exists.
- Level
Medium7 of 10
- Topics
- Graph, Brute force, Greedy
- Solved
- No attempts yet
Problem
The Great Bicycle Race is approaching, and the organizers of the annual sports competition need to choose a track for the racers. The country has cities and bidirectional roads. The organizers pick one city as the host. The race lasts two days. Each day, riders start at the host city, pass through two intermediate cities, and return to the host city. The intermediate cities must not repeat across the two days, so the race uses five distinct cities and six distinct roads in total. Each road has an integer attractiveness , and the attractiveness of a route is the sum of the six road values. Determine whether a valid race exists, and if so, the maximum possible attractiveness of a route.
Input
The first line contains two integers and (, ), the number of cities and roads. Each of the next lines contains three integers , , and (, , ): the two cities connected by road and its attractiveness. No two roads connect the same pair of cities.
Output
If no valid route exists, print . Otherwise, print the maximum total attractiveness of a valid route.
Hint
In the first sample, one of the best choices is city 5 as the host, with the route 5 → 1 → 2 → 5 → 3 → 4 → 5.