Minimize cost to connect the graph by connecting any pairs of vertices having cost at least 0 | C ++ program for the above approach ; Function to perform the find operation to find the parent of a disjoint set ; FUnction to perform union operation of disjoint set union ; Update the minimum value such than it should be greater than 0 ; Update the minimum value such than it should be greater than 0 ; Function to minimize the cost to make the graph connected as per the given condition ; Stores the parent elements of sets ; Stores the rank of the sets ; Stores the minValue of the sets ; Update parent [ i ] to i ; Update minValue [ i ] to cost [ i - 1 ] ; Add i . first and i . second elements to the same set ; Stores the parent elements of the different components ; Insert parent of i to s ; Stores the minimum value from s ; Flag to mark if any minimum value is negative ; If minVal [ i ] is negative ; Mark flag as true ; Update the minimum value ; Stores minimum cost to add the edges ; If the given graph is connected or components minimum values not having any negative edge ; Update the minCost ; Print the value of minCost ; Print - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Find ( vector < int > & parent , int a ) { return parent [ a ] = ( parent [ a ] == a ? a : Find ( parent , parent [ a ] ) ) ; } void Union ( vector < int > & parent , vector < int > & rank , vector < int > & minVal , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( rank [ a ] == rank [ b ] ) rank [ a ] ++ ; if ( rank [ a ] > rank [ b ] ) { parent [ b ] = a ; if ( minVal [ a ] >= 0 && minVal [ b ] >= 0 ) { minVal [ a ] = min ( minVal [ a ] , minVal [ b ] ) ; } else if ( minVal [ a ] >= 0 && minVal [ b ] < 0 ) { minVal [ a ] = minVal [ a ] ; } else if ( minVal [ a ] < 0 && minVal [ b ] >= 0 ) { minVal [ a ] = minVal [ b ] ; } else { minVal [ a ] = max ( minVal [ a ] , minVal [ b ] ) ; } } else { parent [ a ] = b ; if ( minVal [ a ] >= 0 && minVal [ b ] >= 0 ) { minVal [ b ] = min ( minVal [ a ] , minVal [ b ] ) ; } else if ( minVal [ a ] >= 0 && minVal [ b ] < 0 ) { minVal [ b ] = minVal [ a ] ; } else if ( minVal [ a ] < 0 && minVal [ b ] >= 0 ) { minVal [ b ] = minVal [ b ] ; } else { minVal [ b ] = max ( minVal [ a ] , minVal [ b ] ) ; } } } void findMinCost ( vector < pair < int , int > > & G , vector < int > & cost , int N , int M ) { vector < int > parent ( N + 1 ) ; vector < int > rank ( N + 1 , 0 ) ; vector < int > minVal ( N + 1 ) ; for ( int i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = i ; minVal [ i ] = cost [ i - 1 ] ; } for ( auto i : G ) { Union ( parent , rank , minVal , i . first , i . second ) ; } set < int > s ; for ( int i = 1 ; i < N + 1 ; i ++ ) { s . insert ( Find ( parent , i ) ) ; } pair < int , int > min = { 0 , INT_MAX } ; bool flag = false ; for ( auto i : s ) { if ( minVal [ i ] < 0 ) { flag = true ; } if ( min . second > minVal [ i ] ) { min = { i , minVal [ i ] } ; } } int minCost = 0 ; if ( ! flag || ( flag && s . size ( ) == 1 ) ) { for ( auto i : s ) { if ( i != min . first ) { minCost += ( minVal [ i ] + min . second ) ; } } cout << minCost << endl ; } else { cout << -1 << endl ; } } int main ( ) { int N = 6 ; vector < pair < int , int > > G = { { 3 , 1 } , { 2 , 3 } , { 2 , 1 } , { 4 , 5 } , { 5 , 6 } , { 6 , 4 } } ; vector < int > cost { 2 , 1 , 5 , 3 , 2 , 9 } ; int M = G . size ( ) ; findMinCost ( G , cost , N , M ) ; return 0 ; }