#C3319. Minimum Highway Length

    ID: 46733 Type: Default 1000ms 256MiB

Minimum Highway Length

Minimum Highway Length

Problem Description:

You are given n towns and a list of potential highways. Each highway connects two towns and has an associated construction cost. The list of highways is given as triples \((u, v, w)\), where \(u\) and \(v\) denote the town numbers (numbered from 1 to \(n\)) and \(w\) is the length (or cost) of constructing that highway.

Your task is to determine the minimum additional highway length required to ensure that all towns become connected. If the towns are already connected through the available highways, output \(0\). Otherwise, if the towns are disconnected, you must output the total length of the highways that would connect the disconnected components using a minimum spanning tree approach.

In mathematical terms, if a spanning tree exists connecting all \(n\) towns then no additional highways are needed and the answer is \(0\). Otherwise, let \(T\) be the set of highways that connect the various components with minimum total cost, then output the sum:

\[ \text{Total Cost} = \sum_{(u,v,w) \in T} w \]

inputFormat

Input:

The first line contains two integers \(n\) and \(m\), representing the number of towns and the number of available highways respectively. Each of the following \(m\) lines contains three integers \(u\), \(v\) and \(w\), representing a highway connecting towns \(u\) and \(v\) with length \(w\).

outputFormat

Output:

Print a single integer – the minimum additional highway length required to connect all towns. The output should be sent to stdout.

## sample
4 0
0