Minions having fun
Time limit2sMemory limit1024 MB
Given a weighted multigraph, find a closed route that uses each edge at most once and maximizes the sum of its minimum and maximum edge weights.
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Graph
- Solved
- No attempts yet
Problem
Gru went shopping with the girls, and the minions are bored with nothing to do. They decided to keep themselves busy with a competition that every minion agreed to join.
The lab map has checkpoints. There are two-way paths between them, and each path has several bananas on it. A minion starts at any checkpoint, runs along the paths, uses each path at most once, and returns to the starting checkpoint. Suppose a minion ran such a cyclic route and passed through paths with bananas. Then the score for that route is .
The minion Dave wants to win badly. Find the cyclic route with the highest score and help him.
Input
The first line contains two integers , (), the number of checkpoints and the number of paths. Each of the next lines contains three integers , , (; ; ): the two checkpoints connected by the path and the number of bananas on it. Several paths may connect the same pair of checkpoints.
Output
Print one line with the maximum value of the minimum plus the maximum banana count over all cyclic routes. If no cyclic route exists, print 0.
Hints
In the first sample there is only one cycle, so the answer is the minimum plus the maximum banana count on it: . In the second sample there is again only one cycle, and the answer is . In the third sample there are three cycles: 1-2-3-1, 1-2-4-1, and 1-4-2-3-1. The first gives points, the second gives points, and the third gives points. So running the second cycle is best, and the answer is 4. In the fourth sample there is no cycle at all, so the answer is 0.