{"id":2900,"date":"2018-06-03T11:14:17","date_gmt":"2018-06-03T18:14:17","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2900"},"modified":"2018-07-30T20:17:07","modified_gmt":"2018-07-31T03:17:07","slug":"leetcode-847-shortest-path-visiting-all-nodes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-847-shortest-path-visiting-all-nodes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 847. Shortest Path Visiting All Nodes"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 847 Shortest Path Visiting All Nodes  - \u5237\u9898\u627e\u5de5\u4f5c EP195\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Vo3OEN2xgwk?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><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42\u9876\u70b9\u8986\u76d6\u7684\u6700\u77ed\u8def\u5f84\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/shortest-path-visiting-all-nodes\/description\/\">https:\/\/leetcode.com\/problems\/shortest-path-visiting-all-nodes\/description\/<\/a><\/p>\n<p>An undirected, connected graph of N nodes (labeled\u00a0<code>0, 1, 2, ..., N-1<\/code>) is given as\u00a0<code>graph<\/code>.<\/p>\n<p><code>graph.length = N<\/code>, and\u00a0<code>j != i<\/code>\u00a0is in the list\u00a0<code>graph[i]<\/code>\u00a0exactly once, if and only if nodes\u00a0<code>i<\/code>\u00a0and\u00a0<code>j<\/code>\u00a0are connected.<\/p>\n<p>Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[[1,2,3],[0],[0],[0]]\r\n<strong>Output: <\/strong>4\r\n<strong>Explanation<\/strong>: One possible path is [1,0,2,0,3]<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[[1],[0,2,4],[1,3,4],[2],[1,2]]\r\n<strong>Output: <\/strong>4\r\n<strong>Explanation<\/strong>: One possible path is [0,1,4,2,3]\r\n<\/pre>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2908\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/849-ep194.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/849-ep194.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/849-ep194-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/849-ep194-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><strong>Solution: BFS<\/strong><\/h1>\n<p>Time complexity: O(n*2^n)<\/p>\n<p>Space complexity: O(n*2^n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 34 ms\r\nclass Solution {\r\npublic:\r\n  int shortestPathLength(vector&lt;vector&lt;int&gt;&gt;&amp; graph) {\r\n    const int kAns = (1 &lt;&lt; (graph.size())) - 1;    \r\n    queue&lt;pair&lt;int, int&gt;&gt; q;\r\n    unordered_set&lt;int&gt; visited; \/\/ (cur_node &lt;&lt; 16) | state\r\n    for (int i = 0; i &lt; graph.size(); ++i)\r\n      q.push({i, 1 &lt;&lt; i});\r\n    int steps = 0;\r\n    while (!q.empty()) {\r\n      int s = q.size();      \r\n      while (s--) {\r\n        auto p = q.front(); \r\n        q.pop();\r\n        int n = p.first;\r\n        int state = p.second;\r\n        if (state == kAns) return steps;\r\n        int key = (n &lt;&lt; 16) | state;\r\n        if (visited.count(key)) continue;\r\n        visited.insert(key);\r\n        for (int next : graph[n])\r\n          q.push({next, state | (1 &lt;&lt; next)});\r\n      }\r\n      ++steps;\r\n    }\r\n    return -1;\r\n  }\r\n};<\/pre>\n<p>C++ \/ vector<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 10 ms\r\nclass Solution {\r\npublic:\r\n  int shortestPathLength(vector&lt;vector&lt;int&gt;&gt;&amp; graph) {\r\n    const int n = graph.size();\r\n    const int kAns = (1 &lt;&lt; n) - 1;\r\n    queue&lt;pair&lt;int, int&gt;&gt; q;\r\n    vector&lt;vector&lt;int&gt;&gt; visited(n, vector&lt;int&gt;(1 &lt;&lt; n));\r\n    for (int i = 0; i &lt; n; ++i)\r\n      q.push({i, 1 &lt;&lt; i});\r\n    int steps = 0;\r\n    while (!q.empty()) {\r\n      int s = q.size();\r\n      while (s--) {\r\n        auto p = q.front(); \r\n        q.pop();\r\n        int node = p.first;\r\n        int state = p.second;\r\n        if (state == kAns) return steps;   \r\n        if (visited[node][state]) continue;\r\n        visited[node][state] = 1;\r\n        for (int next : graph[node])\r\n          q.push({next, state | (1 &lt;&lt; next)});\r\n      }\r\n      ++steps;\r\n    }\r\n    return -1;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-864-shortest-path-to-get-all-keys\/\">\u82b1\u82b1\u9171 LeetCode 864. Shortest Path to Get All Keys<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u6c42\u9876\u70b9\u8986\u76d6\u7684\u6700\u77ed\u8def\u5f84\u3002 https:\/\/leetcode.com\/problems\/shortest-path-visiting-all-nodes\/description\/ An undirected, connected graph of N nodes (labeled\u00a00, 1, 2, &#8230;, N-1) is given as\u00a0graph. graph.length = N, and\u00a0j != i\u00a0is in&#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],"tags":[34,77,217,87,307],"class_list":["post-2900","post","type-post","status-publish","format-standard","hentry","category-graph","tag-bfs","tag-graph","tag-hard","tag-shortest-path","tag-vertex-cover","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2900","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=2900"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2900\/revisions"}],"predecessor-version":[{"id":3382,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2900\/revisions\/3382"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}