#K10661. Minimum Cost to Connect Buildings
Minimum Cost to Connect Buildings
Minimum Cost to Connect Buildings
You are given n buildings and m potential connections between them. Each connection is represented by three integers u, v, and w, where u and v are the building indices and w is the cost to connect them. Your task is to compute the minimum cost required to connect all the buildings so that every building is reachable from any other building.
This problem can be modeled as finding a Minimum Spanning Tree (MST) of a connected graph. In an MST, the total cost is given by \(\sum_{e \in \text{MST}} w_e\), where \(w_e\) represents the weight of edge \(e\). You can assume that the input graph is connected, so an MST always exists.
inputFormat
The first line contains two space-separated integers n
and m
, representing the number of buildings and the number of connections, respectively.
The next m
lines each contain three space-separated integers u
, v
, and w
, indicating there is a connection between building u
and building v
with cost w
.
outputFormat
Output a single integer representing the minimum cost to connect all the buildings.
## sample5 4
1 2 4
1 3 3
2 4 2
3 5 6
15