Statement▼
There are
Build a well directly at a house: The costs for building wells are given in the wells array, where
wells[i - 1]represents the cost to build a well at housei(houses are numbered from1 ton , but the array is 0-indexed).Lay water pipes to connect two houses: Allowing one to receive water from the other. The costs for building these connections are given in the
pipesarray. Each pipe entry is written as[house1, house2, cost], meaning it costs to construct a pipe connectinghouse1andhouse2. The connection is two-way (water can flow in both directions).
In the given data, multiple pipe entries may connect the same pair of houses, but with different costs. Each entry represents an available option; you may choose the most cost-effective one.
Your task is to determine the minimum total cost to ensure every house gets access to water, either by building a well directly at it or connecting it via pipes to another house with access to water.
Constraints:
2<=n<=104 wells.length==n 0<= wells[i]<=105 1<= pipes.length<=104 pipes[j].length==3 1<= house1j,house2j<=n 0<= costj<=105 house1j!= house2j
Solution
This problem can be viewed as a graph connectivity problem where each house represents a node, edges represent pipes between houses, or the option to build a well. We use the Union-Find (Disjoint Set Union) data structure to construct a minimal connection network.
We begin with an extra virtual node (node i, we add an edge from the virtual node to that house with a cost equal to wells[i - 1], representing the option of building a well. All the original pipe connections are included as regular edges between houses with their given costs.
Next, we sort all these edges (pipes and virtual edges) by cost in ascending order. This enables a greedy approach where we always consider the cheapest connection available at each step. We initialize each node (including the virtual node) in its own set using Union-Find. Then, we iterate through the sorted list of edges. For each edge, we check if the two endpoints belong to different sets (i.e., they are not yet connected). If they are in different sets, we perform a union operation to connect them and add the edge’s cost to the total cost.
We continue this process until all houses are connected either directly to the water source or indirectly via pipes to other connected houses. The total cost accumulated during these union operations gives us the minimum cost to supply water to all houses.
Here’s the step-by-step explanation of the solution:
Create a Union-Find (Disjoint Set Union) structure with
n + 1elements.For each house
ifrom1 ton, add an edge[0, i, wells[i - 1]]to theedgeslist.Add all entries from
pipesto theedgeslist.Sort the
edgeslist in ascending order by cost.Initialize
totalCost = 0.For each edge
[house1, house2, cost]in the sorted list:If
find(house1) != find(house2):Perform
union(house1, house2).Add
costtototalCost.
Return
totalCost.
Let’s look at the following illustration to get a better understanding of the solution: