Roads and Libraries


Problem Statement :


Determine the minimum cost to provide library access to all citizens of HackerLand. There are n cities numbered from 1 to n. Currently there are no libraries and the cities are not connected. Bidirectional roads may be built between any city pair listed in cities. A citizen has access to a library if:

1. Their city contains a library.
2. They can travel by road from their city to a city containing a library.


unction Description

Complete the function roadsAndLibraries in the editor below.
roadsAndLibraries has the following parameters:

int n: integer, the number of cities
int c_lib: integer, the cost to build a library
int c_road: integer, the cost to repair a road
int cities[m][2]: each cities[ i ] contains two integers that represent cities that can be connected by a new road
Returns
- int: the minimal cost

Input Format

The first line contains a single integer q, that denotes the number of queries.

The subsequent lines describe each query in the following format:
- The first line contains four space-separated integers that describe the respective values of n , m , c_lib and c_road, the number of cities, number of roads, cost of a library and cost of a road.
- Each of the next m lines contains two space-separated integers, u[ i ] and v[ i ] , that describe a bidirectional road that can be built to connect cities c[ i ] and v[ i ].


Sample Input

STDIN       Function
-----       --------
2           q = 2
3 3 2 1     n = 3, cities[] size m = 3, c_lib = 2, c_road = 1
1 2         cities = [[1, 2], [3, 1], [2, 3]]
3 1
2 3
6 6 2 5     n = 6, cities[] size m = 6, c_lib = 2, c_road = 5
1 3         cities = [[1, 3], [3, 4],...]
3 4
2 4
1 2
2 3
5 6


Sample Output
4
12



Solution :



title-img


                            Solution in C :

In   C :






#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#define swap_(x, y) { int z = x; x = y; y = z; }

typedef long long ll;

typedef struct L
{
  int *xs;
  int n;
  int size;
} L;

void add(L *l, int x)
{
  if (l->n == l->size)
  {
    l->size *= 2;
    l->xs = realloc(l->xs, sizeof(int) * l->size);
    assert(l->xs);
  }
  l->xs[l->n++] = x;
}

void ini(L *l)
{
  l->n = 0;
  l->size = 4;
  l->xs = malloc(sizeof(int) * l->size);
  assert(l->xs);
}

L *create()
{
  L *l = malloc(sizeof(L));
  assert(l);
  ini(l);
  return l;
}
  
L *ls[100000];

ll solve()
{
  int n, m;
  ll rc, lc;
  scanf("%d%d%lld%lld", &n, &m, &lc, &rc);
  for (int i = 0; i < n; ++i)
  {
    ls[i] = NULL;
  }
  int count = 0;
  for (int _i = 0; _i < m; ++_i)
  {
    int i, j;
    scanf("%d%d", &i, &j);
    i--; j--;
    if (lc <= rc)
    {
      continue;
    }
    if (ls[i] == ls[j])
    {
      if (ls[i] == NULL)
      {
        L *l = create();
        add(l, i);
        add(l, j);
        count++;
        ls[i] = l;
        ls[j] = l;
      }
    }
    else
    {
      count++;
      if (ls[i] == NULL)
      {
        swap_(i, j);
      }
      if (ls[j] == NULL)
      {
        add(ls[i], j);
        ls[j] = ls[i];
      }
      else
      {
        if (ls[i]->n < ls[j]->n)
        {
          swap_(i, j);
        }
        L *l = ls[j];
        for (int p = 0; p < l->n; ++p)
        {
          int k = l->xs[p];
          add(ls[i], k);
          ls[k] = ls[i];
        }
        free(l->xs);
        free(l);
      }
    }
  }
  return rc * count + lc * (n - count);
}

int main()
{
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i)
  {
    ll min_cost = solve();
    printf("%lld\n", min_cost);
  }
  return 0;
}
                        


                        Solution in C++ :

In   C++ :





#include <bits/stdc++.h>
using namespace std;
vector<int> p;
int f(int a){return p[a]==a?a:p[a]=f(p[a]);}
void u(int a, int b){p[f(a)] = f(b);}
signed main()
{
    int T;cin >> T;while(T--){
        int N, M, a, b;
        long long c, d;
        cin >> N >> M >> c >> d;
        p.clear();p.resize(N);
        iota(p.begin(), p.end(), 0);
        while(M--){
            cin >> a >> b;
            --a, --b;
            u(a, b);
        }
        int comp=0;
        for(int i=0;i<N;++i){
            if(p[i]==i)++comp;
        }
        cout << (comp*c+(N-comp)*min(c, d)) << "\n";
    }

    return 0;
}
                    


                        Solution in Java :

In  Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            int n = in.nextInt();
            int m = in.nextInt();
            int x = in.nextInt();
            int y = in.nextInt();
            List<List<Integer>> groups = new ArrayList<List<Integer>>();
            for (int i = 0; i < n; i++){
                List<Integer> group = new ArrayList<Integer>();
                group.add(i);
                groups.add(group);
            }
            boolean[] enabled = new boolean[n];
            for (int i = 0; i < n; i++)
                enabled[i] = true;
            int[] pointers = new int[n];
            for (int i = 0; i < n; i++)
                pointers[i] = i;
            for(int a1 = 0; a1 < m; a1++){
                int city_1 = in.nextInt() - 1;
                int city_2 = in.nextInt() - 1;
                int p1 = pointers[city_1];
                int p2 = pointers[city_2];
                if (p1 != p2){
                    for (int i : groups.get(p2)){
                        pointers[i] = p1;
                        groups.get(p1).add(i);
                    }
                    enabled[p2] = false;
                }
            }
            long total = 0;
            for (int i = 0; i < n; i++){
                if (enabled[i]){
                    int size = groups.get(i).size();
                    total += Math.min(size * x, x + (size - 1) * y);
                }
            }
            System.out.println(total);
        }
    }
}
                    


                        Solution in Python : 
                            
In   Python3 :





#!/bin/python3

import sys

def find_root(roots, city):
    i = roots[city]
    while(i != roots[i]):
        i = roots[i]
    return roots[i]

q = int(input().strip())
for a0 in range(q):
    n,m,x,y = input().strip().split(' ')
    n,m,x,y = [int(n),int(m),int(x),int(y)]
    
    root = [x for x in range(n+1)]
    cost = 0
    
    if(x <= y):
        print(n*x)
        for _ in range(m):
            x = input()
        continue
    for a1 in range(m):
        city_1,city_2 = input().strip().split(' ')
        city_1,city_2 = [int(city_1),int(city_2)]
        temp1 = find_root(root, city_1)
        temp2 = find_root(root, city_2)
        if(temp1 != temp2):
            root[temp1] = temp2
            cost += y
    for i in range(1,n+1):
        if(i == root[i]):
            cost +=x
    print(cost)
                    


View More Similar Problems

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →