VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-cost-to-provide-water/

⇱ Minimum cost to provide water - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum cost to provide water

Last Updated : 23 Jul, 2025

Given an integer N, where N denotes the number of villages numbered 1 to N, an array wells[] where wells[i] denotes the cost to build a water well in the i'th city, a 2D array pipes in form of [X Y C] which denotes that the cost to connect village X and Y with water pipes is C. Your task is to provide water to each and every village either by building a well in the village or connecting it to some other village having water. Find the minimum cost to do so.

Examples:

Input: N=3, wells=[1, 2, 2], pipes=[[1 2 1], [2 3 1]]
Output: 3
Explanation: Build well in village1 to provide water with cost=1
connect village2 and village1 with cost =1 , now village1 and village2 have water.
Finally connect village3 with village2 with cost=1 , now all the villages have water with total cost=3.

Input: N=4, wells[1, 1, 1, 1], pipes=[[1 2 100], [2 3 100], [2 4 50]]
Output: 4
Explanation: Clearly its better to construct well at each village rather than building costly roads. Hence total cost=1+1+1+1=4.

We can create a graph with villages as vertices and use pipes array to form edges between these villages, also we need to construct a pseudo vertex that is connected to each and every village 'i' with an edges weight of wells[i] , in this way we can take care of cost of building the pipes and wells simultaneously. The MST cost of this graph will give us our answer i.e. the minimum cost to provide water to each village.

Suppose, N=4, wells[1, 2, 1, 2], pipes=[[1 2 1], [1 3 3],[2 3 3], [3 4 1]]
Step 1: We construct the graph with villages as the vertex and use pipes array to form edges as shown in fig-1

👁 Initial-graph-to-find-minimum-water-supply-cost
fig-1

Step 2: Create a pseudo node (0) and add edges from 0 to all other vertices from 1 to N , assign edge weight as wells[i] , for each 'i' 1 to N.

👁 file
fig-2

Step 3: Find the MST of the above graph and return the MST cost= 1+1+1+1= 4 as the answer.

👁 Finding-The-MST-For-Minimum-Water-Supply-Cost
fig-3

Step-by-step algorithm:

  • Firstly initialize the DSU data structure i.e. make(), find(), Union(), and parent array.
  • Create a set 'st' to store the edges sorted in ascending order on the basis of edge weight.
  • Insert the edges in 'st' using the pipes array.
  • Edges between villages(1 to N) and pseudo vertex(0) have to be inserted with edge weight=wells[i] for each village 'i'.
  • Apply Kruskal's MST algorithm on the set 'st'.
  • return the MST cost as the answer.

Below is the implementation of the above algorithm:


Output
3

Time Complexity: O(E*log(N)), where E is the number of edges and N is the number of vertices in the formed graph.
Auxiliary Space: O(N)

Comment