[Swift]LeetCode882. 细分图中的可到达结点 | Reachable Nodes In Subdivided Graph
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10602841.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Starting with an undirected graph (the "original graph") with nodes from 0
to N-1
, subdivisions are made to some of the edges.
The graph is given as follows: edges[k]
is a list of integer pairs (i, j, n)
such that (i, j)
is an edge of the original graph,
and n
is the total number of new nodes on that edge.
Then, the edge (i, j)
is deleted from the original graph, n
new nodes (x_1, x_2, ..., x_n)
are added to the original graph,
and n+1
new edges (i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j)
are added to the original graph.
Now, you start at node 0
from the original graph, and in each move, you travel along one edge.
Return how many nodes you can reach in at most M
moves.
Example 1:
Input: edges
= [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3
Output: 13
Explanation:
The nodes that are reachable in the final graph after M = 6 moves are indicated below.
Example 2:
Input: edges
= [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4
Output: 23
Note:
0 <= edges.length <= 10000
0 <= edges[i][0] < edges[i][1] < N
- There does not exist any
i != j
for whichedges[i][0] == edges[j][0]
andedges[i][1] == edges[j][1]
. - The original graph has no parallel edges.
0 <= edges[i][2] <= 10000
0 <= M <= 10^9
1 <= N <= 3000
- A reachable node is a node that can be travelled to using at most M moves starting from node 0.
从具有 0
到 N-1
的结点的无向图(“原始图”)开始,对一些边进行细分。
该图给出如下:edges[k]
是整数对 (i, j, n)
组成的列表,使 (i, j)
是原始图的边。
n
是该边上新结点的总数
然后,将边 (i, j)
从原始图中删除,将 n
个新结点 (x_1, x_2, ..., x_n)
添加到原始图中,
将 n+1
条新边 (i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j)
添加到原始图中。
现在,你将从原始图中的结点 0
处出发,并且每次移动,你都将沿着一条边行进。
返回最多 M
次移动可以达到的结点数。
示例 1:
输入:edges
= [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3
输出:13
解释:
在 M = 6 次移动之后在最终图中可到达的结点如下所示。
示例 2:
输入:edges
= [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4
输出:23
提示:
0 <= edges.length <= 10000
0 <= edges[i][0] < edges[i][1] < N
- 不存在任何
i != j
情况下edges[i][0] == edges[j][0]
且edges[i][1] == edges[j][1]
. - 原始图没有平行的边。
0 <= edges[i][2] <= 10000
0 <= M <= 10^9
1 <= N <= 3000
1 class Solution { 2 func reachableNodes(_ edges: [[Int]], _ M: Int, _ N: Int) -> Int { 3 var steps:[Int] = [Int](repeating:-1,count:N) 4 steps[0] = M 5 for _ in 0...N 6 { 7 var stable:Bool = true 8 for edge in edges 9 { 10 if 0 < steps[edge[0]] 11 { 12 let diff:Int = steps[edge[0]] - edge[2] - 1 13 if steps[edge[1]] < diff 14 { 15 steps[edge[1]] = diff 16 stable = false 17 } 18 } 19 if 0 < steps[edge[1]] 20 { 21 let diff:Int = steps[edge[1]] - edge[2] - 1 22 if steps[edge[0]] < diff 23 { 24 steps[edge[0]] = diff 25 stable = false 26 } 27 } 28 } 29 if stable {break} 30 } 31 var res:Int = 0 32 for i in steps 33 { 34 if 0 <= i 35 { 36 res += 1 37 } 38 } 39 for edge in edges 40 { 41 var cnt:Int = 0 42 if 0 < steps[edge[0]] 43 { 44 cnt += steps[edge[0]] 45 } 46 if 0 < steps[edge[1]] 47 { 48 cnt += steps[edge[1]] 49 } 50 res += edge[2] >= cnt ? cnt : edge[2] 51 } 52 return res 53 } 54 }