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]]
  • 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

 Live Demo

#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

Updated on: 02-Jun-2020

921 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements