
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find out if a round trip is possible from a particular city
Suppose, there are n cities and m roads are connecting them. Each road is unidirectional, and it takes a particular amount of time to reach from the source city to the destination city. The information of the roads is given in the array roads where each element is of the format (source, destination, time). Now, a person is traveling from one city to another city and the trip has to be a round-trip. A trip can be called round-trip when the person starts from a particular city, goes through one or more roads, and finishes the trip in the same city. So for each city, we have to determine if a round-trip is possible from that particular city. If it is possible, print the time required to perform the round-trip or else print -1.
So, if the input is like n = 4, m = 4, roads = {{1, 2, 5}, {2, 3, 8}, {3, 4, 7}, {4, 1, 6}}, then the output will be: 26 26 26 26. From each city, it takes time 26 to perform a round-trip.
To solve this, we will follow these steps −
Define one 2D array graph(n) of pairs for initialize i := 0, when i < m, update (increase i by 1), do: x := first value of roads[i] y := second value of roads[i] z := third value of roads[i] decrease x and y by 1 insert pair (y, z) at the end of graph[x] for initialize i := 0, when i < n, update (increase i by 1), do: q := a new priority queue Define an array dst insert pair (0, i) at the top of q while size of q is non-zero, do: pair p := top value of q delete the top element from q dt := first value of p curr := second value of p if dst[curr] is same as 0, then: dst[curr] := dt Come out from the loop if dst[curr] is not equal to -1, then: Ignore following part, skip to the next iteration dst[curr] := dt for element next in graph[curr], do: tp := first value of next cst := second value of next insert pair(dt + cst, tp) at the top of q if dst[i] is same as 0, then: dst[i] := -1 print(dst[i])
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int modval = (int) 1e9 + 7; #define N 100 void solve(int n, int m, vector<tuple<int, int, int>> roads ) { vector<vector<pair<int, int>>> graph(n); for(int i = 0; i < m; i++) { int x, y, z; tie(x, y, z) = roads[i]; x--; y--; graph[x].emplace_back(y, z); } for(int i = 0; i < n; i++) { priority_queue<pair<int, int>> q; vector<int> dst(n, -1); q.emplace(0, i); while(q.size()){ pair<int, int> p = q.top(); q.pop(); int curr, dt; tie(dt, curr) = p; if(dst[curr] == 0) { dst[curr] = dt; break; } if(dst[curr] != -1) continue; dst[curr] = dt; for(auto next : graph[curr]){ int tp, cst; tie(tp, cst) = next; q.emplace(dt + cst, tp); } } if(dst[i] == 0) dst[i] = -1; cout<< dst[i]<< endl; } } int main() { int n = 4, m = 4; vector<tuple<int, int, int>> roads = {{1, 2, 5}, {2, 3, 8}, {3, 4, 7}, {4, 1, 6}}; solve(n, m, roads); return 0; }
Input
4, 4, {{1, 2, 5}, {2, 3, 8}, {3, 4, 7}, {4, 1, 6}}
Output
26 26 26 26
- Related Articles
- C Program to calculate the Round Trip Time (RTT)
- C++ Program to find out the total cost required for a robot to make a trip in a grid
- C++ program to find out the maximum possible tally from given integers
- C# Round-trip ("R") Format Specifier
- Thomas covers a total distance of 1056 km from city A to city C through city B. If the distance from city A to city B is 543.7 km, find the distance between city B and city C.Express the answer as a decimal.
- Python Program to calculate the Round Trip Time (RTT)
- C++ Program to find out if there is a pattern in a grid
- C++ Program to find out if a palindromic matrix can be made from a given matrix
- Program to find out the minimum number of intercountry travels in a road trip in Python
- C++ Program to find out if a person has won lottery
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid
- C++ Program to Generate All Possible Combinations Out of a,b,c,d,e
- Program to find out if k monitoring stations are enough to monitor particular points in Python
- Program to find out the letter at a particular index in a synthesized string in python
