#C10941. Minimum Cost to Connect Locations (MST)
Minimum Cost to Connect Locations (MST)
Minimum Cost to Connect Locations (MST)
You are given n locations and a list of m potential roads connecting these locations. Each road is bidirectional and has an associated construction cost. Your task is to connect all the locations with the minimum total cost, which is equivalent to finding the Minimum Spanning Tree (MST) of the graph.
The MST cost is defined as \( \sum_{e \in T} cost_e \), where \(T\) is the set of edges selected. You are encouraged to use Kruskal's algorithm (or any other suitable method) to solve this problem.
If there is only one location, the cost is zero.
inputFormat
The input is read from stdin:
- The first line contains an integer n representing the number of locations.
- The second line contains an integer m representing the number of roads.
- The next m lines each contain three integers u, v, and c, indicating there is a road between location u and location v with a construction cost of c.
outputFormat
Output a single integer to stdout representing the minimum total cost required to connect all locations.
## sample4
4
1 2 3
2 3 1
3 4 2
4 1 4
6
</p>