// C++ program to find efficient
// solution for the network
#include // For memset
#include
#include
using namespace std;
// number of houses and number
// of pipes
int number_of_houses, number_of_pipes;
// Array rd stores the
// ending vertex of pipe
int ending_vertex_of_pipes[1100];
// Array wd stores the value
// of diameters between two pipes
int diameter_between_two_pipes[1100];
// Array cd stores the
// starting end of pipe
int starting_vertex_of_pipes[1100];
// Vector a, b, c are used
// to store the final output
vector a;
vector b;
vector c;
int ans;
int dfs(int w)
{
if (starting_vertex_of_pipes[w] == 0)
return w;
if (diameter_between_two_pipes[w] < ans)
ans = diameter_between_two_pipes[w];
return dfs(starting_vertex_of_pipes[w]);
}
// Function performing calculations.
void solve(int arr[][3])
{
for (int i = 0; i < number_of_pipes; ++i) {
code
int house_1 = arr[i][0], house_2 = arr[i][1],
pipe_diameter = arr[i][2];
starting_vertex_of_pipes[house_1] = house_2;
diameter_between_two_pipes[house_1] = pipe_diameter;
ending_vertex_of_pipes[house_2] = house_1;
}
a.clear();
b.clear();
c.clear();
for (int j = 1; j <= number_of_houses; ++j)
/*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.*/
if (ending_vertex_of_pipes[j] == 0
&& starting_vertex_of_pipes[j]) {
ans = 1000000000;
int w = dfs(j);
// We put the details of component
// in final output array
a.push_back(j);
b.push_back(w);
c.push_back(ans);
}
cout << a.size() << endl;
for (int j = 0; j < a.size(); ++j)
cout << a[j] << " " << b[j] << " " << c[j] << endl;
}
// driver function
int main()
{
number_of_houses = 9, number_of_pipes = 6;
This is the code format in which I want my code to be in without changing variable name and also use vector instead of array
#include <bits/stdc++.h>
using namespace std;
// function for water connection problem
void water_connection(int arr[][3], int n, int pipes)
{
// to store the starting vertex of the pipe
vector start(1100, 0);
// to store the ending vertex of the pipe
vector ed(1100, 0);
// to store the diameter of pipe
vector diameter(1100);
int i = 0;
code
for (i = 0; i < pipes; i++)
{
start[arr[i][0]] = arr[i][1];
diameter[arr[i][0]] = arr[i][2];
ed[arr[i][1]] = arr[i][0];
}
// st---> to store the starting vertex
// en--> to store the ending vertex
// dm-->to store the minimum diameter of the pipe
vector<int> st, en, dm;
for (int j = 1; j <= n; ++j)
// If there is a pipe that has no ending vertex but has
// starting vertex then we will start DFS with this vertex
if (ed[j] == 0)
{
if (start[j])
{
ans = INT_MAX;
int w = dfs(j, diameter, start);
// we will store the details of components in these final arrays
st.push_back(j);
en.push_back(w);
dm.push_back(ans);
}
}
printSolution(st, en, dm);
}
// driver function
int main()
{
// to store the no of houses and pipes
int n = 9, pipes = 6;
int arr[][3] = {{2, 3, 45},
{4, 5, 10},
{5, 8, 22},
{1, 2, 33},
{7, 6, 17},
{3, 7, 66}};
code
water_connection(arr, n, pipes);
return 0;
}
do it from this
Certainly! Below is your original C++ program refactored to use std::vector instead of arrays while keeping the original variable names intact. The structure has been reorganized to follow the format you provided, including separate functions for DFS, solution processing, and printing the results.
cpp
#include <bits/stdc++.h>
using namespace std;
// Number of houses and pipes
int number_of_houses, number_of_pipes;
// Vectors to store pipe information
vector<int> starting_vertex_of_pipes(1100, 0);
vector<int> ending_vertex_of_pipes(1100, 0);
vector<int> diameter_between_two_pipes(1100, 0);
// Output vectors
vector<int> a;
vector<int> b;
vector<int> c;
// Variable to store the minimum diameter during DFS
int ans;
// Function to perform DFS and find the endpoint and minimum diameter
int dfs(int w, const vector<int> &starting_vertex_of_pipes, const vector<int> &diameter_between_two_pipes) {
if (starting_vertex_of_pipes[w] == 0)
return w;
if (diameter_between_two_pipes[w] < ans)
ans = diameter_between_two_pipes[w];
return dfs(starting_vertex_of_pipes[w], starting_vertex_of_pipes, diameter_between_two_pipes);
}
// Function to print the final solution
void printSolution(const vector<int> &a, const vector<int> &b, const vector<int> &c) {
cout << a.size() << endl;
for (size_t j = 0; j < a.size(); ++j)
cout << a[j] << " " << b[j] << " " << c[j] << endl;
}
// Function to solve the water connection problem
void solve(int arr[][3], int n, int pipes) {
// Reset all vectors to initial state
fill(ending_vertex_of_pipes.begin(), ending_vertex_of_pipes.end(), 0);
fill(starting_vertex_of_pipes.begin(), starting_vertex_of_pipes.end(), 0);
fill(diameter_between_two_pipes.begin(), diameter_between_two_pipes.end(), 0);
// Populate the vectors with the pipe information
for(int i = 0; i < pipes; ++i) {
int house_1 = arr[i][0];
int house_2 = arr[i][1];
int pipe_diameter = arr[i][2];
starting_vertex_of_pipes[house_1] = house_2;
diameter_between_two_pipes[house_1] = pipe_diameter;
ending_vertex_of_pipes[house_2] = house_1;
}
// Clear previous results
a.clear();
b.clear();
c.clear();
// Iterate through all houses to find sources (no incoming pipes)
for(int j = 1; j <= n; ++j) {
// If the house has no incoming pipe but has an outgoing pipe
if(ending_vertex_of_pipes[j] == 0 && starting_vertex_of_pipes[j] != 0) {
ans = INT32_MAX;
int endpoint = dfs(j, starting_vertex_of_pipes, diameter_between_two_pipes);
// Store the result in output vectors
a.push_back(j);
b.push_back(endpoint);
c.push_back(ans);
}
}
// Print the final solution
printSolution(a, b, c);
}
// Driver function
int main() {
// Initialize number of houses and pipes
number_of_houses = 9;
number_of_pipes = 6;
// Define the array with pipe connections and diameters
int arr[][3] = {
{7, 4, 98},
{5, 9, 72},
{4, 6, 10},
{2, 8, 22},
{9, 7, 17},
{3, 1, 66}
};
// Solve the water connection problem
solve(arr, number_of_houses, number_of_pipes);
return 0;
}
Key Changes and Explanations:
Using std::vector Instead of Arrays:
Replaced the fixed-size arrays ending_vertex_of_pipes, starting_vertex_of_pipes, and diameter_between_two_pipes with std::vector<int> initialized with a size of 1100 and default value 0.
This provides dynamic resizing and better memory management compared to raw arrays.
Maintaining Original Variable Names:
All original variable names (starting_vertex_of_pipes, ending_vertex_of_pipes, diameter_between_two_pipes, a, b, c, and ans) are retained to ensure consistency with your original code.
Modular Function Design:
dfs Function: Performs Depth-First Search to trace the path from a starting house to the endpoint, updating the minimum diameter encountered.
printSolution Function: Handles the output formatting, printing the number of connections followed by each connection's details.
solve Function: Processes the input array to populate the vectors, identifies the starting points (houses with no incoming pipes), and utilizes DFS to find connections and minimum diameters.
Initialization and Resetting Vectors:
Before populating the vectors in the solve function, they are reset to ensure no residual data from previous runs affects the current solution.
Input Handling:
The main function initializes the number of houses and pipes and defines the arr array containing the pipe connections and their diameters. This array is then passed to the solve function.
Output:
The program first prints the number of valid connections found. For each connection, it prints the starting house, ending house, and the minimum diameter along that path.
The program will process these connections and determine the efficient network based on the criteria defined in the DFS function. The output will display the number of connections followed by each connection's details (start house, end house, minimum diameter).
Benefits of Using std::vector:
Dynamic Sizing: Vectors can dynamically adjust their size, which is especially useful if the number of houses or pipes changes.
Safety: Vectors handle their own memory management, reducing the risk of memory leaks and buffer overflows.
Convenience: They come with a suite of member functions that simplify common tasks like insertion, deletion, and traversal.
Feel free to further modify or optimize the code based on your specific requirements or constraints!