Power Shortage
InterviewTime limit1sMemory limit256 MB
Given a connected weighted undirected graph, keep a subset of roads so every pair of houses stays connected while maximizing the total length of removed roads.
- Level
Medium5 of 10
- Topics
- Minimum spanning tree, Graph, Union-find, Greedy
- Solved
- No attempts yet
Problem
Seongjin is the mayor of a city, but the city is short on money and struggling with a power shortage. So he decides to turn off some of the streetlights that were originally lit on every road. Keeping a road's streetlights on costs money each day equal to the road's length in meters, so turning some off saves exactly that much money.
However, it is dangerous if traveling between two houses forces you to pass along a road whose lights are off. Therefore, for every pair of houses in the city, it must be possible to travel between them using only lit roads.
Find the maximum amount of money that can be saved while satisfying this condition.
Input
The input consists of several test cases.
The first line of each test case contains the number of houses and the number of roads . (, )
Each of the next lines describes a road with three integers , , , meaning there is a bidirectional road between house and house whose length is meters. (, )
The city is always a connected graph; that is, for any two houses there exists a path between them. The sum of the lengths of all roads in the city is less than meters.
The last line of the input contains two zeros in place of and ; this line is not processed.
Output
For each test case, print on one line the maximum cost that can be saved.