#C3842. Minimum Toll Cost
Minimum Toll Cost
Minimum Toll Cost
You are given a set of houses and a set of roads connecting them. Each road is associated with a toll cost. Your task is to compute the minimum total toll cost required to connect all the houses so that every house is reachable from any other. In other words, you need to form a spanning tree with the minimum sum of toll costs.
Using Kruskal's algorithm for finding a minimum spanning tree, the total cost is given by ( \sum_{e \in T} cost(e) ), where ( T ) is the set of edges in the spanning tree.
If there is only one house or no roads, the answer is 0.
inputFormat
The input is read from standard input (stdin) and consists of multiple lines. The first line contains two integers (N) and (M) where (N) is the number of houses and (M) is the number of roads. Each of the following (M) lines contains three integers (u), (v), and (c) representing a road between house (u) and house (v) with a toll cost of (c).
outputFormat
Output a single integer to standard output (stdout) which represents the minimum toll cost required to connect all houses.## sample
4 5
1 2 5
1 3 10
2 3 7
2 4 4
3 4 3
12