Weeping Fig
Time limit3sMemory limit512 MB
Split the vertices of a connected weighted undirected graph into two nonempty parts so that the total weight of edges crossing the cut is minimized.
- Level
Hard8 of 10
- Topics
- Graph, Minimum spanning tree, Greedy, Divide and conquer
- Solved
- No attempts yet
Problem
A weeping fig grows in your backyard. Its roots never push under the house, they only spread into the empty space around it, so your friends call it the nice weeping fig. A friend who is building a new house wants the same tree, and yours grows fast enough that you agreed to cut off part of it.
The tree has roots, numbered to . There are connections, each one joining two different roots, and cutting the connection between roots and takes effort .
You want to split the roots into two groups, neither of them empty. No connection may be left between the two groups, and the effort you spend is the sum of the efforts of the connections you cut. Find the smallest total effort.
Input
The first line has the number of test cases ().
The first line of each test case has two integers and , the number of roots and the number of connections (, ).
Each of the next lines has three integers , , (, , ), meaning roots and are connected and cutting that connection takes effort .
At most one connection joins any pair of roots, and every root reaches every other root along the connections.
Output
For each test case, print the smallest total effort on its own line.
Hint
In the first example, pulling root away from the rest takes . Splitting from instead takes .