Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. static constexpr int pow(int x, int p) {
  2.     int res = 1;
  3.     int base = x;
  4.  
  5.     while (p) {
  6.         if (p & 1) res *= base;
  7.         base *= base;
  8.         p /= 2;
  9.     }
  10.  
  11.     return res;
  12. }
  13.  
  14.  
  15. class Solution {
  16. public:
  17.     int numBusesToDestination(vector<vector<int>>& routes, int S, int T) {
  18.         stop_id_to_rout_id_ = vector<vector<int>>(Solution::kMaxStopId);
  19.         auto graph = BuildGraph(routes);
  20.         return SearchTargetStop(graph, S, T);
  21.     }
  22.  
  23. private:
  24.     static constexpr int kMaxStopId{ pow(10, 6) };
  25.     static constexpr int kMaxRouts{ 500 };
  26.  
  27.     vector<vector<int>> stop_id_to_rout_id_;
  28.  
  29.     vector<vector<int>> BuildGraph(const vector<vector<int>>& routes) {
  30.  
  31.         for (int rout_id = 0; rout_id < routes.size(); ++rout_id) {
  32.             for (auto stop_id : routes[rout_id])
  33.                 stop_id_to_rout_id_[stop_id].push_back(rout_id);
  34.         }
  35.  
  36.         vector<vector<int>> graph(kMaxRouts);
  37.  
  38.         for (auto& rout_ids : stop_id_to_rout_id_) {
  39.             for (int i = 1; i < rout_ids.size(); ++i) {
  40.                 for (int j = 0; j < i; ++j) {
  41.                     graph[rout_ids[i]].push_back(rout_ids[j]);
  42.                     graph[rout_ids[j]].push_back(rout_ids[i]);
  43.                 }
  44.             }
  45.         }
  46.  
  47.         return graph;
  48.     }
  49.  
  50.     int SearchTargetStop(const vector<vector<int>>& graph, int S, int T) {
  51.         queue<pair<int, int>> to_process;
  52.         vector<int> visited(Solution::kMaxRouts);
  53.  
  54.         for (auto route_id : stop_id_to_rout_id_[S]) { // add all routs containing start stop
  55.             if (find(stop_id_to_rout_id_[T].begin(), stop_id_to_rout_id_[T].end(), route_id) !=
  56.                     stop_id_to_rout_id_[T].end())
  57.                 return (S == T) ? 0 : 1;
  58.             to_process.push({ route_id, 1 });
  59.         }
  60.  
  61.         while (!to_process.empty()) {
  62.             auto[curr_rout_id, dist] = to_process.front();
  63.             to_process.pop();
  64.             visited[curr_rout_id] = true;
  65.  
  66.             for (auto adj_rout_id : graph[curr_rout_id]) {
  67.                 if (visited[adj_rout_id]) continue;
  68.                 if (find(stop_id_to_rout_id_[T].begin(), stop_id_to_rout_id_[T].end(), adj_rout_id) !=
  69.                         stop_id_to_rout_id_[T].end())
  70.                     return dist + 1;
  71.  
  72.                 to_process.push({ adj_rout_id, dist + 1 });
  73.             }
  74.         }
  75.  
  76.         return -1;
  77.     }
  78. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement