★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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 }