#C3014. Minimum Total Travel Time

    ID: 46395 Type: Default 1000ms 256MiB

Minimum Total Travel Time

Minimum Total Travel Time

You are given several test cases. In each test case, there are n cities and m bidirectional roads. Each road connects two distinct cities u and v and has an associated travel time w.

Your task is to determine the minimum total travel time needed to connect all the cities so that every city is reachable from every other city. In other words, you have to compute the cost of a Minimum Spanning Tree (MST) of the graph formed by the cities and roads. If it is impossible to connect all cities, output IMPOSSIBLE.

The MST cost is defined as \(\sum_{e \in \text{MST}} w_e\), where \(w_e\) is the travel time of edge \(e\). If the graph is not connected, then an MST does not exist.

inputFormat

The input is read from stdin with the following format:

T
n1 m1
u v w
u v w
... (m1 lines)

n2 m2 ... (m2 lines)

... (T test cases in total)

</p>

Here, the first line contains an integer T specifying the number of test cases. For each test case, the first line contains two integers n and m representing the number of cities and roads respectively. Then, there follow m lines each containing three integers u, v, and w — where u and v are the city indices (starting from 0) and w is the travel time of the road connecting them.

outputFormat

For each test case, print a single line to stdout containing either the minimum total travel time (i.e. cost of the MST) or IMPOSSIBLE if it is not possible to connect all cities.

## sample
2
4 5
0 1 1
0 2 2
0 3 3
1 2 1
2 3 1
3 3
0 1 4
1 2 5
2 0 3
3

7

</p>