Water Connection Problem | C # program to find efficient solution for the network ; number of houses and number of pipes ; Array rd stores the ending vertex of pipe ; Array wd stores the value of diameters between two pipes ; Array cd stores the starting end of pipe ; arraylist a , b , c are used to store the final output ; Function to perform calculations . ; If a pipe has no ending vertex but has starting vertex i . e is an outgoing pipe then we need to start DFS with this vertex . ; We put the details of component in final output array ; set the value of the araray to zero
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int n , p ; static int [ ] rd = new int [ 1100 ] ; static int [ ] wt = new int [ 1100 ] ; static int [ ] cd = new int [ 1100 ] ; static List < int > a = new List < int > ( ) ; static List < int > b = new List < int > ( ) ; static List < int > c = new List < int > ( ) ; static int ans ; static int dfs ( int w ) { if ( cd [ w ] == 0 ) return w ; if ( wt [ w ] < ans ) ans = wt [ w ] ; return dfs ( cd [ w ] ) ; } static void solve ( int [ , ] arr ) { int i = 0 ; while ( i < p ) { int q = arr [ i , 0 ] ; int h = arr [ i , 1 ] ; int t = arr [ i , 2 ] ; cd [ q ] = h ; wt [ q ] = t ; rd [ h ] = q ; i ++ ; } a = new List < int > ( ) ; b = new List < int > ( ) ; c = new List < int > ( ) ; for ( int j = 1 ; j <= n ; ++ j ) if ( rd [ j ] == 0 && cd [ j ] > 0 ) { ans = 1000000000 ; int w = dfs ( j ) ; a . Add ( j ) ; b . Add ( w ) ; c . Add ( ans ) ; } Console . WriteLine ( a . Count ) ; for ( int j = 0 ; j < a . Count ; ++ j ) Console . WriteLine ( a [ j ] + " ▁ " + b [ j ] + " ▁ " + c [ j ] ) ; } for ( int i = 0 ; i < 1100 ; i ++ ) [ i ] = cd [ i ] = wt [ i ] = 0 ; int [ , ] arr = { { 7 , 4 , 98 } , { 5 , 9 , 72 } , { 4 , 6 , 10 } , { 2 , 8 , 22 } , { 9 , 7 , 17 } , { 3 , 1 , 66 } } ; solve ( arr ) ; } }