Loading...
Loading...
Loading...
A company has n offices and m possible two-way cables "a b w" with price w. All offices can be connected using the cables. The company wants exactly K separate regions: offices inside a region are connected, but there is no cable between regions. Print the smallest total price of the cables to buy.
Example
Input
5 5 1 2 1 2 3 2 3 4 9 4 5 1 1 5 20 2
Output
4
Connect everything for 13, then leave out the cable of price 9.
Try the problem first. Open one hint at a time.
Run Kruskal: it adds the cheapest cables first and every added cable lowers the number of regions by one.
Stop as soon as exactly K regions are left.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor