15110 - Island Connection   

Description

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.

Constraints

  • 1 ≤ M ≤ 10
  • 2 ≤ N ≤ 10000
  • 0 ≤ E ≤ 100000
  • 0 ≤ u, v < N
  • u ≠ v
  • 1 ≤ w ≤ 100000
  • Sum of N over all test cases ≤ 30000
  • Sum of E over all test cases ≤ 200000
 

Input

The 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.

Output

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.

Sample Input  Download

Sample Output  Download

Tags

yan_ds



Discuss