Minimum cost to supply water to every house using wells and pipes

Quick Overview

Find the minimum total cost to supply water to every house in a village, where each house can get its own well or be connected by pipes to a house that has water. It tests graph modeling, minimum spanning trees and union-find, plus careful handling of parallel pipes and 1-based house numbers.

Minimum cost to supply water to every house using wells and pipes

Company: Yahoo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

There are `n` houses in a village, numbered 1 to `n`. We want to supply water to 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]` (the `- 1` is there because the array is 0-indexed), 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]` is the cost to connect `house1_j` and `house2_j` with a pipe. Connections are bidirectional, and there can be several pipes between the same two houses with different costs. Return the minimum total cost to supply water to all houses. ### Function Signature ```python def min_cost_to_supply_water(n: int, wells: list[int], pipes: list[list[int]]) -> int: ``` ### Rules - Houses are numbered from 1 to `n`, and `wells[i - 1]` is the cost of a well at house `i`. - A house has water when it has its own well, or when a chain of built pipes joins it to a house that has a well. Water flows both ways along a pipe. - You may build any subset of the wells and any subset of the pipes. When several pipes join the same two houses, each one is a separate option with its own cost. - Return only the minimum total cost. It is unique even when several plans reach it. ### Constraints - `2 <= n <= 10⁴` - `wells.length == n` - `0 <= wells[i] <= 10⁵` - `1 <= pipes.length <= 10⁴` - `pipes[j].length == 3` - `1 <= house1_j, house2_j <= n` - `0 <= cost_j <= 10⁵` - `house1_j != house2_j` ### Examples ```text Input: n = 3, wells = [1, 2, 2], pipes = [[1, 2, 1], [2, 3, 1]] Output: 3 ``` Build a well at house 1 (cost 1), then pipe house 2 to house 1 (cost 1) and house 3 to house 2 (cost 1), for a total of 3. Any plan with a second well pays at least 2 for it and saves at most one pipe of cost 1. ```text Input: n = 2, wells = [1, 1], pipes = [[1, 2, 1]] Output: 2 ``` Two wells cost 1 + 1 = 2, and one well plus the pipe also costs 1 + 1 = 2, so the plans tie. Nothing cheaper exists: at least one well is needed, and the other house needs either its own well or the pipe. ```text Input: n = 4, wells = [1, 9, 9, 2], pipes = [[1, 2, 3], [1, 2, 1], [2, 3, 2], [3, 4, 8]] Output: 6 ``` Build wells at houses 1 and 4 (1 + 2), use the cheaper of the two pipes between houses 1 and 2 (1), and pipe house 3 to house 2 (2), for 6 in total. Piping house 4 from house 3 would cost 8, far more than its own well at 2.

Overview: Find the minimum total cost to supply water to every house in a village, where each house can get its own well or be connected by pipes to a house that has water. It tests graph modeling, minimum spanning trees and union-find, plus careful handling of parallel pipes and 1-based house numbers.

Yahoo logo
Yahoo
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

There are n houses in a village, numbered 1 to n. We want to supply water to 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] (the - 1 is there because the array is 0-indexed), 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] is the cost to connect house1_j and house2_j with a pipe. Connections are bidirectional, and there can be several pipes between the same two houses with different costs.

Return the minimum total cost to supply water to all houses.

Function Signature

def min_cost_to_supply_water(n: int, wells: list[int], pipes: list[list[int]]) -> int:

Rules

  • Houses are numbered from 1 to n , and wells[i - 1] is the cost of a well at house i .
  • A house has water when it has its own well, or when a chain of built pipes joins it to a house that has a well. Water flows both ways along a pipe.
  • You may build any subset of the wells and any subset of the pipes. When several pipes join the same two houses, each one is a separate option with its own cost.
  • Return only the minimum total cost. It is unique even when several plans reach it.

Constraints

  • 2 <= n <= 10⁴
  • wells.length == n
  • 0 <= wells[i] <= 10⁵
  • 1 <= pipes.length <= 10⁴
  • pipes[j].length == 3
  • 1 <= house1_j, house2_j <= n
  • 0 <= cost_j <= 10⁵
  • house1_j != house2_j

Examples

Input: n = 3, wells = [1, 2, 2], pipes = [[1, 2, 1], [2, 3, 1]]
Output: 3

Build a well at house 1 (cost 1), then pipe house 2 to house 1 (cost 1) and house 3 to house 2 (cost 1), for a total of 3. Any plan with a second well pays at least 2 for it and saves at most one pipe of cost 1.

Input: n = 2, wells = [1, 1], pipes = [[1, 2, 1]]
Output: 2

Two wells cost 1 + 1 = 2, and one well plus the pipe also costs 1 + 1 = 2, so the plans tie. Nothing cheaper exists: at least one well is needed, and the other house needs either its own well or the pipe.

Input: n = 4, wells = [1, 9, 9, 2], pipes = [[1, 2, 3], [1, 2, 1], [2, 3, 2], [3, 4, 8]]
Output: 6

Build wells at houses 1 and 4 (1 + 2), use the cheaper of the two pipes between houses 1 and 2 (1), and pipe house 3 to house 2 (2), for 6 in total. Piping house 4 from house 3 would cost 8, far more than its own well at 2.