A country consists of N islands, numbered from 0 to N-1.
The government plans to build bridges to connect all islands. There are E possible bridges, each of which can be constructed between two different islands at a specified cost.
The goal is to select a set of bridges such that every island can be reached from every other island while minimizing the total construction cost.
All bridges are undirected, meaning that traveling between two islands is possible in both directions once a bridge is constructed.
Your task is to write a C/C++ program to determine the minimum total construction cost required to connect all islands.
If it is impossible to connect all islands using the available bridges, output IMPOSSIBLE.
Your program MAY use C/C++ standard library headers.
1 ≤ M ≤ 102 ≤ N ≤ 100000 ≤ E ≤ 1000000 ≤ u, v < Nu ≠ v1 ≤ w ≤ 100000Sum of N over all test cases ≤ 30000Sum of E over all test cases ≤ 200000The first line contains an integer M, representing the number of test cases.
For each test case:
The first line contains two integers N and E, representing the number of islands and possible bridges, respectively.
The next E lines each contain three integers:
u v w
Where:
- u and v are the two islands that the proposed bridge would connect.
- w is the construction cost of the proposed bridge.
For each test case, output one line containing the minimum total construction cost required to connect all islands.
If it is impossible to connect all islands, output IMPOSSIBLE.