Reachable Nodes In Subdivided Graph

Hard

Description

Edges are subdivided into nodes. Starting from node 0 with maxMoves, find how many nodes (including subdivided nodes) are reachable.

Examples

Input:edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
Output:13
Explanation:

13 nodes reachable.

Input:edges = [[1]], maxMoves = 6, n = 3
Output:1
Explanation:

Minimal case with a single-element matrix.

Input:edges = [[0,1,4],[1,2,3],[2,3,1]], maxMoves = 8, n = 4
Output:11
Explanation:

Starting from node 0 with 8 moves: node 1 is reached in 4 moves, node 2 in 7 moves (4+3), and node 3 in 8 moves (7+1). This covers all 4 original nodes plus 7 subdivided nodes along the edges, totaling 11 reachable nodes.

Constraints

  • 0 ≤ edges.length ≤ min(n*(n-1)/2, 10⁴)

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!