#C9876. Minimum Cost to Connect Water Tanks

    ID: 54017 Type: Default 1000ms 256MiB

Minimum Cost to Connect Water Tanks

Minimum Cost to Connect Water Tanks

In this problem, you are given a city with (n) water tanks and (m) available pipes. Each pipe connects two different tanks and has an associated maintenance cost. Your task is to determine the minimum total maintenance cost required to connect all the water tanks, such that there is a path between any two tanks. This is essentially a Minimum Spanning Tree problem. Formally, given a graph (G = (V, E)) where (V) is the set of water tanks and (E) is the set of pipes (with each edge ((u, v)) having weight (w)), find a subset (E' \subseteq E) that connects all vertices and minimizes the sum of weights (\sum_{e \in E'} w_e).

inputFormat

The input is given via standard input (stdin). The first line contains two integers (n) and (m), where (n) represents the number of water tanks and (m) represents the number of pipes. Each of the following (m) lines contains three integers (u), (v), and (w), indicating that there is a pipe connecting water tank (u) and water tank (v) with a maintenance cost of (w). If (m = 0), then there are no pipes.

outputFormat

Output a single integer representing the minimum total maintenance cost to connect all water tanks. The answer should be printed to standard output (stdout).## sample

4 5
1 2 1
1 3 2
1 4 3
2 3 4
3 4 5
6