Ant mansion collapse
Time limit2sMemory limit1024 MB
Find the minimum total risk of paths linking nests 1 to N to nest 0, then the least stamina among those, or -1 if some nest is unreachable.
- Level
Medium6 of 10
- Topics
- Minimum spanning tree, Graph, Tree
- Solved
- No attempts yet
Problem
An ant kingdom has ant nests, numbered from to . There are bidirectional paths, each connecting two different nests. After a heavy rain, the kingdom is at risk of collapse, so the collapse risk of every path was measured. The collapse risk of path is . By default, every path has a different risk. The exception is that paths directly connected to nest may have equal risks.
So, are the nests safe?
At the moment, ants numbered from to are gathered at nest . Ant sets out on a trip to check whether nest is safe. The ants want to minimize the total risk of the paths they use. In other words, if the set of path numbers used by the ants is , they want to minimize the following value .
Each time an ant uses a path, it spends unit of stamina. Among the cases where is minimal, help the ants minimize the total stamina they spend.
Input
The first line contains and , separated by a space.
Each of the next lines contains , , and , separated by spaces. This means the path connecting nest and nest has risk .
Output
When is minimal, print the minimum total stamina the ants spend. If it is impossible to check all nests, print .