desc.html 1.9 KB
Newer Older
ToTensor's avatar
ToTensor 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<p>在一个有向图中,节点分别标记为&nbsp;<code>0, 1, ..., n-1</code>。这个图中的每条边不是红色就是蓝色,且存在自环或平行边。</p>

<p><code>red_edges</code>&nbsp;中的每一个&nbsp;<code>[i, j]</code>&nbsp;对表示从节点 <code>i</code> 到节点 <code>j</code> 的红色有向边。类似地,<code>blue_edges</code>&nbsp;中的每一个&nbsp;<code>[i, j]</code>&nbsp;对表示从节点 <code>i</code> 到节点 <code>j</code> 的蓝色有向边。</p>

<p>返回长度为 <code>n</code> 的数组&nbsp;<code>answer</code>,其中&nbsp;<code>answer[X]</code>&nbsp;是从节点&nbsp;<code>0</code>&nbsp;到节点&nbsp;<code>X</code>&nbsp;的红色边和蓝色边交替出现的最短路径的长度。如果不存在这样的路径,那么 <code>answer[x] = -1</code></p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre><strong>输入:</strong>n = 3, red_edges = [[0,1],[1,2]], blue_edges = []
<strong>输出:</strong>[0,1,-1]
</pre>

<p><strong>示例 2:</strong></p>

<pre><strong>输入:</strong>n = 3, red_edges = [[0,1]], blue_edges = [[2,1]]
<strong>输出:</strong>[0,1,-1]
</pre>

<p><strong>示例 3:</strong></p>

<pre><strong>输入:</strong>n = 3, red_edges = [[1,0]], blue_edges = [[2,1]]
<strong>输出:</strong>[0,-1,-1]
</pre>

<p><strong>示例 4:</strong></p>

<pre><strong>输入:</strong>n = 3, red_edges = [[0,1]], blue_edges = [[1,2]]
<strong>输出:</strong>[0,1,2]
</pre>

<p><strong>示例 5:</strong></p>

<pre><strong>输入:</strong>n = 3, red_edges = [[0,1],[0,2]], blue_edges = [[1,0]]
<strong>输出:</strong>[0,1,1]
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>red_edges.length &lt;= 400</code></li>
	<li><code>blue_edges.length &lt;= 400</code></li>
	<li><code>red_edges[i].length == blue_edges[i].length == 2</code></li>
	<li><code>0 &lt;= red_edges[i][j], blue_edges[i][j] &lt; n</code></li>
</ul>