#K48557. Minimum Travel Distance

    ID: 28446 Type: Default 1000ms 256MiB

Minimum Travel Distance

Minimum Travel Distance

You are given a connected undirected graph with \( n \) nodes and \( m \) edges. Each edge has an associated travel distance. The goal of this problem is to compute the minimum total travel distance required to connect all nodes – that is, to find the Minimum Spanning Tree (MST) of the graph.

The MST is defined as a subset of the edges that connects all the nodes together without any cycles and with the minimum possible total edge weight. In mathematical form, if \( MST \) is the set of chosen edges, then we seek to minimize:

[ \min \sum_{e \in MST} w(e) ]

You can solve this problem efficiently using algorithms such as Kruskal's algorithm combined with a Disjoint Set Union (Union-Find) data structure.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains two space-separated integers \( n \) and \( m \) indicating the number of nodes and the number of edges respectively.
  • Each of the next \( m \) lines contains three space-separated integers \( u \), \( v \), and \( d \) representing an undirected edge between node \( u \) and node \( v \) with a travel distance of \( d \). The nodes are numbered from 0 to \( n-1 \).

outputFormat

Output a single integer to stdout representing the minimum total travel distance (i.e. the weight of the MST).

## sample
4 4
0 1 1
0 2 2
0 3 3
1 3 4
6

</p>