#K53977. Minimum Total Magical Value for Island Bridges
Minimum Total Magical Value for Island Bridges
Minimum Total Magical Value for Island Bridges
You are given n islands and m bridges. Each bridge connects two islands u and v and has a magical value w. Your task is to choose a subset of bridges such that all islands are connected and the total magical value is minimized.
This is essentially the Minimum Spanning Tree (MST) problem. A well-known algorithm to solve this problem is Kruskal's algorithm. In Kruskal's algorithm, the bridges are sorted in non-decreasing order of their magical values, and added one by one, ensuring that no cycle is formed, until all islands are connected.
The total magical value of the chosen bridges is given by the sum of the magical values of the bridges selected in the MST.
The mathematical formulation is given by: $$\min \sum_{e\in T} w_e$$ where \(T\) is the set of bridges (edges) chosen such that the graph is connected, and \(w_e\) is the magical value of edge \(e\).
inputFormat
The input is given via standard input (stdin). The first line contains two integers n and m, denoting the number of islands and the number of bridges respectively. Each of the following m lines contains three integers u, v and w, indicating that a bridge connects island u and island v with a magical value of w. Islands are numbered from 1 to n.
outputFormat
Output a single integer to standard output (stdout) representing the minimum total magical value required to connect all islands.
## sample4 5
1 2 1
1 3 4
1 4 3
2 3 2
3 4 5
6
</p>