Optimize Water Distribution System

Optimize Water Distribution in a Village

There are n houses in a village. You want to supply water to all the houses by building wells and laying pipes. For each house i, you can either build a well inside it directly with cost wells[i-1], or pipe water from another house to it. The cost to lay a pipe between house u and house v is given by the array pipes where each pipes[j] = [house1, house2, cost] represents a bidirectional connection. Return the minimum total cost to supply water to all houses. Constraints: 1 \le n \le 10^4 wells.length == n 0 \le wells[i] \le 10^5 1 \le pipes.length \le 10^4 1 \le house1, house2 \le n 0 \le cost \le 10^5 house1 \neq house2
C++Kruskal's AlgorithmMSTDSU
00
Read

Optimize Water Distribution System

There are n houses in a neighborhood. You want to provide water to every house at the minimum possible cost. For each house i, you have two options: Build a well directly at the house with cost wells[i-1]. Lay a pipe connecting house i to another house j with a specific cost given in an array pipes, where pipes[k] = [house1, house2, cost]. Connections are bidirectional. Multiple pipes can exist between the same pair of houses. Find the minimum total cost to ensure every house has access to water (either via its own well or via a connection to a house that has access to water).
JavaKruskal's AlgorithmUnion-FindMSTGreedy
00
Read
1
InterviewGPT

AI-powered tools to help you succeed in tech interviews — from resume to offer.

Products

  • Interview Solver
  • Question Bank
  • Golden Blogs
  • Intervipedia
  • Application Tools

Company

  • Pricing
  • FAQ
  • About

Legal

  • Privacy Policy
  • Terms of Service

© 2026 InterviewGPT Inc. All rights reserved.

All systems operationalUS-East

Made with ♥ for developers