- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find out the minimal cost so that the citizens have access to a market in Python
Suppose, there is n number of cities and m roads connecting the cities. The citizens of the people need markets where they can buy their commodities. Now, there are no markets in the cities, and the roads between the cities are under construction.A two-way road can be built between two cities if (i) The city contains a market; (ii) The cities can be visited by the road where there is a market. The cost of building a road is x, and building a market is y and they are given. We have to find out the minimal cost to provide access to markets to citizens of each city. The array 'cities' contains information about the cities about which cities can be connected by road.
So, if the input is like n = 4, m = 3, x = 1, y = 2, cities = [[1, 2], [2, 3], [3, 4]], then the output will be 4.
Here, we can see four cities 1, 2, 3 and 4. If a market is built a t city 1, and two more roads are built between (1, 4) and (1,3) the total cost will be 2 + 1 + 1 = 4. This is the minimum cost.
To solve this, we will follow these steps −
- if x <= y, then
- return n * x
- otherwise,
- adj_list := a map containing lists as elements
- for each city in cities, do
- city1 := city[0]
- city2 := city[1]
- insert city2 at the end of adj_list[city1]
- insert city1 at the end of adj_list[city2]
- temp := a new list of size (n + 1) initialized with value True
- value := 0
- dq := a double ended queue
- for cur in range 1 to n + 1, do
- if temp[cur] is non-zero, then
- value := value + x
- insert cur at the rightmost end of dq
- temp[cur] := False
- while dq is not empty, do
- for each i adj_list[extracted leftmost element of dq], do
- if temp[i] is non-zero, then
- insert i at the rightmost end of dq
- temp[i] := False
- value := value + y
- if temp[i] is non-zero, then
- if temp[cur] is non-zero, then
- return value
Example
Let us see the following implementation to get better understanding −
from collections import defaultdict, deque def solve(n, m, x, y, cities): if x <= y: return n * x else: adj_list = defaultdict(list) for city in cities: city1 = city[0] city2 = city[1] adj_list[city1].append(city2) adj_list[city2].append(city1) temp = [True] * (n + 1) value = 0 dq = deque() for cur in range(1, n + 1): if temp[cur]: value += x dq.append(cur) temp[cur] = False while dq: for i in adj_list[dq.popleft()]: if temp[i]: dq.append(i) temp[i] = False value += y return value print(solve(4, 3, 1, 2, [[1, 2], [2, 3], [3, 4]]))
Input
4, 3, 2, 1, [[1, 2], [2, 3], [3, 4]]
Output
4
- Related Articles
- Program to Find Out the Minimal Submatrices in Python
- Program to find out the buildings that have a better view in Python
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to Find Out the Edges that Disconnect the Graph in Python
- Program to find out the sum of minimum cost within a graph among all vertices in Python
- C++ Program to find out the cost to travel all the given coordinates
- Program to find out the minimum cost path between the lowest value vertex to the highest value vertex (Python)
- Program to find out if a vertex in an undirected graph has a lesser cost path in Python
- C++ program to find out the shortest cost path in a given graph for q queries
- C++ Program to find out the total cost required for a robot to make a trip in a grid
- Program to find out the path between two vertices in a graph that has the minimum penalty (Python)
- Program to find out the cost to merge an array of integers into a single value in C++
- Program to Find Out the Special Nodes in a Tree in Python
- Program to Find Out the Points Achievable in a Contest in Python
