#K93452. Minimum Corridor Length

    ID: 38422 Type: Default 1000ms 256MiB

Minimum Corridor Length

Minimum Corridor Length

You are given a network of outposts connected by potential corridors. Your task is to determine the minimum total length of corridors required to connect all the outposts. This problem is equivalent to finding the Minimum Spanning Tree (MST) of an undirected weighted graph.

Formally, if the graph consists of outposts represented as vertices and corridors as edges with weights, you need to compute $$\min \sum_{e \in T} w(e),$$ where \(T\) is a spanning tree connecting all vertices.

You may assume that the given corridors are sufficient to connect all outposts.

inputFormat

The first line of input contains two integers N and M, where N is the number of outposts and M is the number of potential corridors.

Each of the following M lines contains three integers u, v, and w, signifying a corridor between outposts u and v with a length (or weight) w. Outposts are numbered from 0 to N-1.

outputFormat

Output a single integer, the minimum total length of the corridors required to connect all outposts.

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