#K80402. Minimum Spanning Tree Cost
Minimum Spanning Tree Cost
Minimum Spanning Tree Cost
You are given an undirected, connected weighted graph with n nodes and m edges. Each edge connects two nodes and has an associated weight. Your task is to compute the minimum total cost to connect all nodes in the graph, i.e. the cost of the Minimum Spanning Tree (MST).
You can assume that the graph is connected. Use an efficient algorithm such as Kruskal's algorithm to solve this problem.
The formula for the MST cost is given by:
\( MST_{cost} = \sum_{e \in T} w(e) \)
where \( T \) is the set of edges in the MST and \( w(e) \) is the weight of edge \( e \).
inputFormat
The input is read from standard input (stdin) and has the following format:
n m u1 v1 w1 u2 v2 w2 ... um v m w m
Where n
is the number of nodes, m
is the number of edges, and each of the next m
lines contains three integers u
, v
and w
representing an edge between node u
and node v
with weight w
.
outputFormat
Output a single integer representing the total cost of the minimum spanning tree. The result should be printed to the standard output (stdout).
## sample4 5
1 2 1
1 3 4
2 3 2
2 4 6
3 4 3
6