★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-gcmhhzjr-me.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Starting with an undirected graph (the "original graph") with nodes from 0
to N-1
, subdivisions are made to some of the edges.node
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,git
and n
is the total number of new nodes on that edge. github
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.ide
Now, you start at node 0
from the original graph, and in each move, you travel along one edge. spa
Return how many nodes you can reach in at most M
moves. code
Example 1:htm
Input: = [[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.
edges
Example 2:blog
Input: = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4 Output: 23 edges
Note:
0 <= edges.length <= 10000
0 <= edges[i][0] < edges[i][1] < N
i != j
for which edges[i][0] == edges[j][0]
and edges[i][1] == edges[j][1]
.0 <= edges[i][2] <= 10000
0 <= M <= 10^9
1 <= N <= 3000
从具备 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:
输入:= [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3 输出:13 解释: 在 M = 6 次移动以后在最终图中可到达的结点以下所示。 edges
示例 2:
输入:= [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4 输出:23 edges
提示:
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 }