#K70152. Minimum Network Latency

    ID: 33245 Type: Default 1000ms 256MiB

Minimum Network Latency

Minimum Network Latency

You are given a network of n computers and m cables. Each cable connects two computers and is associated with a latency value. Your task is to determine the minimum total latency required to connect all computers together, i.e., to form a spanning tree.

If it is impossible to connect all computers (i.e. the network is disconnected), output IMPOSSIBLE.

The problem can be formulated as finding the Minimum Spanning Tree (MST) of a graph. Mathematically, the MST of a connected graph \(G=(V,E)\) is a subset \(T\subseteq E\) such that:

\[ \sum_{e\in T}w(e)\ \] is minimized and \(|T|=|V|-1\). If there exists no such \(T\) that spans all vertices, then the answer is \(IMPOSSIBLE\).

inputFormat

The first line of input contains two integers n and m, where n is the number of computers and m is the number of network cables.

Each of the next m lines contains three integers u, v, and w representing a cable connecting computer u and computer v with a latency (or weight) of w.

\(1 \leq u,v \leq n\). It is guaranteed that the latency w is a positive integer.

outputFormat

Output a single line containing the minimum total latency required to connect all computers. If it is impossible to connect all computers, output IMPOSSIBLE.

## sample
4 5
1 2 5
1 3 10
1 4 20
2 3 2
3 4 3
10