Optimize Water Distribution in a Village Problem
Description
LeetCode Problem 1168.
There are n houses in a village. We want to supply water for all the houses by building wells and laying pipes.
For each house i, we can either build a well inside it directly with cost wells[i - 1] (note the -1 due to 0-indexing), or pipe in water from another well to it. The costs to lay pipes between houses are given by the array pipes where each pipes[j] = [house1_j, house2_j, cost_j] represents the cost to connect house1_j and house2_j together using a pipe. Connections are bidirectional, and there could be multiple valid connections between the same two houses with different costs.
Return the minimum total cost to supply water to all houses.
Example 1:

Input: n = 3, wells = [1,2,2], pipes = [[1,2,1],[2,3,1]]
Output: 3
Explanation: The image shows the costs of connecting houses using pipes.
The best strategy is to build a well in the first house with cost 1 and connect the other houses to it with cost 2 so the total cost is 3.
Example 2:
Input: n = 2, wells = [1,1], pipes = [[1,2,1],[1,2,2]]
Output: 2
Explanation: We can supply water with cost two using one of the three options:
Option 1:
- Build a well inside house 1 with cost 1.
- Build a well inside house 2 with cost 1.
The total cost will be 2.
Option 2:
- Build a well inside house 1 with cost 1.
- Connect house 2 with house 1 with cost 1.
The total cost will be 2.
Option 3:
- Build a well inside house 2 with cost 1.
- Connect house 1 with house 2 with cost 1.
The total cost will be 2.
Note that we can connect houses 1 and 2 with cost 1 or with cost 2 but we will always choose the cheapest option.
Constraints:
2 <= n <= 10^4wells.length == n0 <= wells[i] <= 10^51 <= pipes.length <= 10^4pipes[j].length == 31 <= house1_j, house2_j <= n0 <= cost_j <= 10^5house1_j != house2_j
Solution
Approach: Virtual Water Source + Kruskal's MST
Building a well is the odd one out among the choices. The trick is to turn it into a pipe too: add a virtual node 0, the water source, and treat a well in house i as a pipe between 0 and i with cost wells[i - 1].
Now a house has water exactly when it is connected to node 0 through the chosen pipes, so we need the cheapest set of edges that connects all n + 1 nodes: a minimum spanning tree.
Kruskal's algorithm finds it. Sort all edges (the real pipes and the n well edges) by cost, and go through them from the cheapest. Use union-find to check whether an edge joins two groups that are still separate; if so, take it and merge the groups, otherwise skip it because it would only form a cycle. After n merges all n + 1 nodes are in one group and we can stop.
Duplicate pipes between the same houses need no special handling: once the cheaper one is taken, the more expensive one is skipped as a cycle.
Complexity: O((n + m) log(n + m)) time for sorting, where m is the number of pipes, and O(n + m) space.
class Solution:
def minCostToSupplyWater(self, n: int, wells: List[int], pipes: List[List[int]]) -> int:
# A well in house i is a pipe from a virtual water source 0 to i.
edges = [[0, i + 1, w] for i, w in enumerate(wells)] + pipes
edges.sort(key=lambda e: e[2])
parent = list(range(n + 1))
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving
x = parent[x]
return x
# Kruskal: take the cheapest edge that joins two separate groups, until all n + 1 nodes are one group.
ans, joins = 0, 0
for a, b, cost in edges:
ra, rb = find(a), find(b)
if ra != rb:
parent[ra] = rb
ans += cost
joins += 1
if joins == n:
break
return ans