
- 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
Bus Routes in C++
Suppose we have a list of bus routes. In each routes[i] there is a bus route that the i-th bus repeats forever. So, if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1, 5, 7, 1, 5, 7, 1, ... forever.
Now suppose we start at bus stop S, initially not on a bus, and we want to go to bus stop T. we have to find the least number of buses we must take to reach our destination? If this is not possible then return -1.
So if the input is like [[1,2,8],[3,6,8]], and S = 1, T = 6, then output will be 2. So, take the first bus to bus stop 7, then take second bus to bus stop 6.
To solve this, we will follow these steps −
- Define one map m
- for initialize i := 0, when i < size of r, update (increase i by 1), do −
- for initialize j := 0, when j < size of r[i], update (increase j by 1), do −
- insert i at the end of m[r[i, j]]
- for initialize j := 0, when j < size of r[i], update (increase j by 1), do −
- Define one queue q, insert S into q
- if S is same as T, then −
- return 0
- Define one set called visited
- for initialize lvl := 1, when not q is empty, update (increase lvl by 1), do −
- sz := size of q
- while sz is non-zero, do −
- node := first element of q, delete element from q
- for initialize i := 0, when i < size of m[node], update (increase i by 1), do −
- route := m[node, i]
- if route is in visited, then −
- Ignore following part, skip to the next iteration
- insert route into visited
- for initialize j := 0, when j < size of r[route], update (increase j by 1), do −
- stop := r[route, j]
- if stop is same as T, then −
- return lvl
- insert stop into q
- decrease sz by 1
- return -1
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int numBusesToDestination(vector<vector<int>>& r, int S, int T) { unordered_map <int, vector <int> > m; for(int i = 0; i < r.size(); i++){ for(int j = 0; j < r[i].size(); j++){ m[r[i][j]].push_back(i); } } queue <int> q; q.push(S); if(S == T) return 0; unordered_set <int> visited; for(int lvl = 1; !q.empty(); lvl++){ int sz = q.size(); while(sz--){ int node = q.front(); q.pop(); for(int i = 0; i < m[node].size(); i++){ int route = m[node][i]; if(visited.count(route)) continue; visited.insert(route); for(int j = 0; j < r[route].size(); j++){ int stop = r[route][j]; if(stop == T) return lvl; q.push(stop); } } } } return -1; } }; main(){ Solution ob; vector<vector<int>> v = {{1,2,8}, {3,6,8}}; cout << (ob.numBusesToDestination(v, 1, 6)); }
Input
{{1,2,8}, {3,6,8}} 1 6
Output
2
- Related Articles
- Handling different routes in express.js
- What are Named Routes in Laravel?
- Reorder Routes to Make All Paths Lead to the City Zero in C++
- How OSPF routes the packets from source to destination?
- If a bus travel 48 kilometre in 40 minutes, what will be speed of bus ?
- Bus X travels a distance of 360 km in 5 hours whereas bus Y travels a distance of 476 km in 7 hours. Which bus travels faster ?
- CAN Bus with Arduino
- Madam Rides The Bus
- What is bus arbitration in computer organization?
- What is Bus Transfer in Computer Architecture?
- A bus travels 54 km in 90 minutes. Find the speed of the bus in m/s & k/m.
- Token Bus (IEEE 802.4) Network
- Token Bus and Token Ring
- Bus topology vs Ring topology
- What is single shared bus in computer architecture?

Advertisements