Loading...
Loading...
Loading...
A company wants all n offices connected, directly or through other offices. There are m possible cables "a b w", each with price w. The offices can always be connected using the given cables. Print the smallest total price of the cables that connect all offices.
Example
Input
4 5 1 2 1 2 3 2 3 4 3 1 4 10 1 3 5
Output
6
Use the cables of price 1, 2 and 3.
Try the problem first. Open one hint at a time.
Sort the cables by price.
Take a cable when its two offices are in different groups; use Union-Find to check.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor