<?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>all pair shortest paths Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/all-pair-shortest-paths/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/all-pair-shortest-paths/</link>
	<description></description>
	<lastBuildDate>Sun, 31 May 2020 01:09:59 +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>all pair shortest paths Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/all-pair-shortest-paths/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1462. Course Schedule IV</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1462-course-schedule-iv/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1462-course-schedule-iv/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 May 2020 01:07:46 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[all pair shortest paths]]></category>
		<category><![CDATA[graph]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6841</guid>

					<description><![CDATA[<p>There are a total of&#160;n&#160;courses you have to take, labeled from&#160;0&#160;to&#160;n-1. Some courses may have direct prerequisites, for example, to take course 0 you have&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1462-course-schedule-iv/">花花酱 LeetCode 1462. Course Schedule IV</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>There are a total of&nbsp;<code>n</code>&nbsp;courses you have to take, labeled from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n-1</code>.</p>



<p>Some courses may have direct prerequisites, for example, to take course 0 you have first to take course 1, which is expressed as a pair:&nbsp;<code>[1,0]</code></p>



<p>Given the total number of courses&nbsp;<code>n</code>,&nbsp;a list of direct&nbsp;<code>prerequisite</code>&nbsp;<strong>pairs</strong>&nbsp;and a list of&nbsp;<code>queries</code>&nbsp;<strong>pairs</strong>.</p>



<p>You should answer for each&nbsp;<code>queries[i]</code>&nbsp;whether the course&nbsp;<code>queries[i][0]</code>&nbsp;is a&nbsp;prerequisite of the course&nbsp;<code>queries[i][1]</code>&nbsp;or not.</p>



<p>Return&nbsp;<em>a list of boolean</em>, the answers to the given&nbsp;<code>queries</code>.</p>



<p>Please note that if course&nbsp;<strong>a</strong>&nbsp;is a prerequisite of course&nbsp;<strong>b</strong>&nbsp;and course&nbsp;<strong>b</strong>&nbsp;is a prerequisite&nbsp;of course&nbsp;<strong>c</strong>, then, course&nbsp;<strong>a</strong>&nbsp;is a&nbsp;prerequisite of course&nbsp;<strong>c</strong>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/17/graph.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]]
<strong>Output:</strong> [false,true]
<strong>Explanation:</strong> course 0 is not a prerequisite of course 1 but the opposite is true.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, prerequisites = [], queries = [[1,0],[0,1]]
<strong>Output:</strong> [false,false]
<strong>Explanation:</strong> There are no prerequisites and each course is independent.
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/17/graph-1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]]
<strong>Output:</strong> [true,true]
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, prerequisites = [[1,0],[2,0]], queries = [[0,1],[2,0]]
<strong>Output:</strong> [false,true]
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, prerequisites = [[0,1],[1,2],[2,3],[3,4]], queries = [[0,4],[4,0],[1,3],[3,0]]
<strong>Output:</strong> [true,false,true,false]
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>2 &lt;= n &lt;= 100</code></li><li><code>0 &lt;= prerequisite.length &lt;= (n * (n - 1) / 2)</code></li><li><code>0 &lt;= prerequisite[i][0], prerequisite[i][1] &lt; n</code></li><li><code>prerequisite[i][0] != prerequisite[i][1]</code></li><li>The prerequisites graph has no cycles.</li><li>The prerequisites graph has no repeated edges.</li><li><code>1 &lt;= queries.length &lt;= 10^4</code></li><li><code>queries[i][0] != queries[i][1]</code></li></ul>



<h2><strong>Solution: Floyd-Warshall Algorithm (All pairs shortest paths</strong>)</h2>



<p>Time complexity: O(n^3 + q)<br>Space complexity: O(n^2)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;bool&gt; checkIfPrerequisite(int n, 
      vector&lt;vector&lt;int&gt;&gt;&amp; prerequisites, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    vector&lt;vector&lt;int&gt;&gt; g(n, vector&lt;int&gt;(n));
    for (const auto&amp; p : prerequisites)
      g[p[0]][p[1]] = 1;
    for (int k = 0; k &lt; n; ++k)
      for (int i = 0; i &lt; n; ++i)
        for (int j = 0; j &lt; n; ++j)          
          g[i][j] |= g[i][k] &amp; g[k][j];
    vector&lt;bool&gt; ans;
    for (const auto&amp; q : queries)
      ans.push_back(g[q[0]][q[1]]);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1462-course-schedule-iv/">花花酱 LeetCode 1462. Course Schedule IV</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-1462-course-schedule-iv/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
