Increase the Toll Fees
Time limit2sMemory limit1024 MB
Given a weighted graph with a unique minimum spanning tree, find the least total increase in tree edge prices so that no original tree edge stays in the new MST, or report -1.
- Level
Medium7 of 10
- Topics
- Minimum spanning tree, Union-find, Tree
- Solved
- No attempts yet
Problem
The ICPC Kingdom is a big kingdom with cities, numbered from to . The charm of the ICPC Kingdom lies in its beautiful sceneries. To promote those sceneries, the King of the ICPC Kingdom decided to build toll roads, numbered from to , near the scenic views for tourists to enjoy. To use toll road , which connects city to bidirectionally, one must pay . It is possible to travel from one city to any other city using these toll roads.
The toll roads have been built and can be used, but they still do not attract tourists. The King made a promotion: one can pay in advance for the toll roads they want to use, and can use them multiple times as long as they do not leave the ICPC Kingdom.
This idea finally attracts tourists, but a strange behavior happens. Every tourist pays only for the set of toll roads with the minimum total pay that lets them travel from any city to any other city. Such a set is unique under the current toll prices. This strange behavior does not fully expose the kingdom's scenery to the tourists.
To expose more scenery, the King decided to increase the price of some toll roads. If toll road is used by the tourists' strange behavior before the increase, then after the increase the King must ensure that toll road is not used by that behavior. The King also wants the total price increase across all toll roads to be as small as possible.
The King asked you to calculate the minimum total price increase that fulfills the King's plan, or to report that the plan is impossible.
Input
Input begins with two integers and (; ), the number of cities and toll roads. Each of the next lines contains three integers , , (; ), meaning toll road connects city and with price . Before the price increase, there is a unique set of toll roads that allows travel between any two cities with the minimum total pay.
Output
If the King's plan can be achieved, output one integer: the minimum total increase. Otherwise, output -1 on a single line.