{"id":2462,"date":"2018-04-09T21:13:18","date_gmt":"2018-04-10T04:13:18","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2462"},"modified":"2018-04-17T23:34:04","modified_gmt":"2018-04-18T06:34:04","slug":"leetcode-815-bus-routes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-815-bus-routes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 815. Bus Routes"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 815. Bus Routes - \u5237\u9898\u627e\u5de5\u4f5c EP180\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/vEcm5farBls?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1>Problem<\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u6bcf\u8f86\u516c\u4ea4\u8f66\u7684\u73af\u5f62\u8def\u7ebf\uff0c\u95ee\u6700\u5c11\u9700\u8981\u5750\u591a\u5c11\u8f86\u516c\u4ea4\u8f66\u624d\u80fd\u9001S\u5230\u8fbeT\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/bus-routes\/description\/\">https:\/\/leetcode.com\/problems\/bus-routes\/description\/<\/a><\/p>\n<div class=\"question-description\">\n<div>\n<p>We have a list of bus routes. Each\u00a0<code>routes[i]<\/code>\u00a0is a bus route that the i-th bus\u00a0repeats forever. For example if\u00a0<code>routes[0] = [1, 5, 7]<\/code>, this means that the first\u00a0bus (0-th indexed) travels in the sequence 1-&gt;5-&gt;7-&gt;1-&gt;5-&gt;7-&gt;1-&gt;&#8230; forever.<\/p>\n<p>We start at bus stop\u00a0<code>S<\/code>\u00a0(initially not on a bus), and we want to go to bus stop\u00a0<code>T<\/code>. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.<\/p>\n<pre class=\"crayon:false\"><strong>Example:<\/strong>\r\n<strong>Input:<\/strong> \r\nroutes = [[1, 2, 7], [3, 6, 7]]\r\nS = 1\r\nT = 6\r\n<strong>Output:<\/strong> 2\r\n<strong>Explanation:<\/strong> \r\nThe best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= routes.length &lt;= 500<\/code>.<\/li>\n<li><code>1 &lt;= routes[i].length &lt;= 500<\/code>.<\/li>\n<li><code>0 &lt;= routes[i][j] &lt; 10 ^ 6<\/code>.<\/li>\n<\/ul>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2465\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/04\/815-ep180.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/04\/815-ep180.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/04\/815-ep180-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/04\/815-ep180-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<\/div>\n<\/div>\n<h1><strong>Solution: BFS<\/strong><\/h1>\n<p>Time Complexity: O(m*n) m: # of buses, n: # of routes<\/p>\n<p>Space complexity: O(m*n + m)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 89 ms\r\nclass Solution {\r\npublic:\r\n  int numBusesToDestination(vector&lt;vector&lt;int&gt;&gt;&amp; routes, int S, int T) {\r\n    if (S == T) return 0;\r\n    \r\n    unordered_map&lt;int, vector&lt;int&gt;&gt; m;\r\n    for (int i = 0; i &lt; routes.size(); ++i)\r\n      for (const int stop : routes[i])\r\n        m[stop].push_back(i);\r\n    \r\n    vector&lt;int&gt; visited(routes.size(), 0);\r\n    queue&lt;int&gt; q;\r\n    q.push(S);\r\n    int buses = 0;\r\n    \r\n    while (!q.empty()) {\r\n      int size = q.size();      \r\n      ++buses;\r\n      while (size--) {\r\n        int curr = q.front(); q.pop();        \r\n        for (const int bus : m[curr]) {\r\n          if (visited[bus]) continue;          \r\n          visited[bus] = 1;\r\n          for (int stop : routes[bus]) {\r\n            if (stop == T) return buses;            \r\n            q.push(stop);\r\n          }\r\n        }        \r\n      }      \r\n    }\r\n    return -1;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u6bcf\u8f86\u516c\u4ea4\u8f66\u7684\u73af\u5f62\u8def\u7ebf\uff0c\u95ee\u6700\u5c11\u9700\u8981\u5750\u591a\u5c11\u8f86\u516c\u4ea4\u8f66\u624d\u80fd\u9001S\u5230\u8fbeT\u3002 https:\/\/leetcode.com\/problems\/bus-routes\/description\/ We have a list of bus routes. Each\u00a0routes[i]\u00a0is a bus route that the i-th bus\u00a0repeats forever. For example if\u00a0routes[0] = [1, 5,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76,44],"tags":[34,77,42,87],"class_list":["post-2462","post","type-post","status-publish","format-standard","hentry","category-graph","category-searching","tag-bfs","tag-graph","tag-search","tag-shortest-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2462"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2462\/revisions"}],"predecessor-version":[{"id":2528,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2462\/revisions\/2528"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}