Coder Social home page Coder Social logo

dsa-essentials-java's Introduction

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class GraphRoadsAndLibraries {

    static ArrayList<ArrayList<Integer>> graph;
    static boolean[] visited;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int q = scanner.nextInt(); // Number of queries

        for (int i = 0; i < q; i++) {
            int n = scanner.nextInt(); // Number of cities (nodes)
            int m = scanner.nextInt(); // Number of roads (edges)
            long c_lib = scanner.nextLong(); // Cost to build a library
            long c_road = scanner.nextLong(); // Cost to build a road

            graph = new ArrayList<>();
            visited = new boolean[n + 1];

            for (int j = 0; j <= n; j++) {
                graph.add(new ArrayList<>());
            }

            // Read and build the graph
            for (int j = 0; j < m; j++) {
                int city1 = scanner.nextInt();
                int city2 = scanner.nextInt();
                graph.get(city1).add(city2);
                graph.get(city2).add(city1);
            }

            // Calculate and print the minimum cost
            long result = calculateMinimumCost(n, c_lib, c_road);
            System.out.println(result);
        }

        scanner.close();
    }

    static long calculateMinimumCost(int n, long c_lib, long c_road) {
        if (c_road >= c_lib || m == 0) {
            // It's cheaper to build a library in each city
            return n * c_lib;
        } else {
            // Use DFS or BFS to find connected components and calculate the cost
            long totalCost = 0;
            for (int i = 1; i <= n; i++) {
                if (!visited[i]) {
                    totalCost += c_lib + (dfs(i) - 1) * c_road;
                }
            }
            return totalCost;
        }
    }

    static int dfs(int node) {
        int count = 1;
        visited[node] = true;

        for (int neighbor : graph.get(node)) {
            if (!visited[neighbor]) {
                count += dfs(neighbor);
            }
        }

        return count;
    }
}

dsa-essentials-java's People

Contributors

deepakrai990 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.