<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vertex cover Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/vertex-cover/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/vertex-cover/</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jul 2018 03:17:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>vertex cover Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/vertex-cover/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 847. Shortest Path Visiting All Nodes</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-847-shortest-path-visiting-all-nodes/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-847-shortest-path-visiting-all-nodes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Jun 2018 18:14:17 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[shortest path]]></category>
		<category><![CDATA[vertex cover]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2900</guid>

					<description><![CDATA[<p>Problem 题目大意：求顶点覆盖的最短路径。 https://leetcode.com/problems/shortest-path-visiting-all-nodes/description/ An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.length = N, and j != i is in&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-847-shortest-path-visiting-all-nodes/">花花酱 LeetCode 847. Shortest Path Visiting All Nodes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/Vo3OEN2xgwk?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>题目大意：求顶点覆盖的最短路径。</p>
<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>
<p>An undirected, connected graph of N nodes (labeled <code>0, 1, 2, ..., N-1</code>) is given as <code>graph</code>.</p>
<p><code>graph.length = N</code>, and <code>j != i</code> is in the list <code>graph[i]</code> exactly once, if and only if nodes <code>i</code> and <code>j</code> are connected.</p>
<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>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>[[1,2,3],[0],[0],[0]]
<strong>Output: </strong>4
<strong>Explanation</strong>: One possible path is [1,0,2,0,3]</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>[[1],[0,2,4],[1,3,4],[2],[1,2]]
<strong>Output: </strong>4
<strong>Explanation</strong>: One possible path is [0,1,4,2,3]
</pre>
<h1><img 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="(max-width: 960px) 100vw, 960px" /></h1>
<h1><strong>Solution: BFS</strong></h1>
<p>Time complexity: O(n*2^n)</p>
<p>Space complexity: O(n*2^n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 34 ms
class Solution {
public:
  int shortestPathLength(vector&lt;vector&lt;int&gt;&gt;&amp; graph) {
    const int kAns = (1 &lt;&lt; (graph.size())) - 1;    
    queue&lt;pair&lt;int, int&gt;&gt; q;
    unordered_set&lt;int&gt; visited; // (cur_node &lt;&lt; 16) | state
    for (int i = 0; i &lt; graph.size(); ++i)
      q.push({i, 1 &lt;&lt; i});
    int steps = 0;
    while (!q.empty()) {
      int s = q.size();      
      while (s--) {
        auto p = q.front(); 
        q.pop();
        int n = p.first;
        int state = p.second;
        if (state == kAns) return steps;
        int key = (n &lt;&lt; 16) | state;
        if (visited.count(key)) continue;
        visited.insert(key);
        for (int next : graph[n])
          q.push({next, state | (1 &lt;&lt; next)});
      }
      ++steps;
    }
    return -1;
  }
};</pre><p>C++ / vector</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 10 ms
class Solution {
public:
  int shortestPathLength(vector&lt;vector&lt;int&gt;&gt;&amp; graph) {
    const int n = graph.size();
    const int kAns = (1 &lt;&lt; n) - 1;
    queue&lt;pair&lt;int, int&gt;&gt; q;
    vector&lt;vector&lt;int&gt;&gt; visited(n, vector&lt;int&gt;(1 &lt;&lt; n));
    for (int i = 0; i &lt; n; ++i)
      q.push({i, 1 &lt;&lt; i});
    int steps = 0;
    while (!q.empty()) {
      int s = q.size();
      while (s--) {
        auto p = q.front(); 
        q.pop();
        int node = p.first;
        int state = p.second;
        if (state == kAns) return steps;   
        if (visited[node][state]) continue;
        visited[node][state] = 1;
        for (int next : graph[node])
          q.push({next, state | (1 &lt;&lt; next)});
      }
      ++steps;
    }
    return -1;
  }
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-864-shortest-path-to-get-all-keys/">花花酱 LeetCode 864. Shortest Path to Get All Keys</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-847-shortest-path-visiting-all-nodes/">花花酱 LeetCode 847. Shortest Path Visiting All Nodes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-847-shortest-path-visiting-all-nodes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
