Reachable Nodes In Subdivided Graph - Problem
Reachable Nodes In Subdivided Graph

Imagine you have a network of cities connected by roads, and you want to add rest stops along each road to make travel more convenient. Each road will be divided into segments with a specific number of rest stops added between the original cities.

You are given an undirected graph with n nodes labeled from 0 to n-1. Each edge in the original graph will be subdivided by adding new nodes along it. The graph is represented as a 2D array edges where edges[i] = [ui, vi, cnti] means:
  • There's an edge between nodes ui and vi
  • cnti new nodes will be added along this edge
  • If cnti = 0, no subdivision occurs

Subdivision Process: An edge [u, v] with cnt subdivisions becomes a chain: u → x1 → x2 → ... → x_cnt → v, creating cnt + 1 new edges.

Goal: Starting from node 0, determine how many nodes (both original and newly added) you can reach within maxMoves steps in the subdivided graph.

Input & Output

example_1.py — Basic Graph
$ Input: edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
Output: 13
💡 Note: From node 0, we can reach node 2 in 2 moves (via edge [0,2,1]), then backtrack and explore the [0,1,10] edge. We can reach node 0 (1 node) + node 2 (1 node) + 4 subdivision nodes on edge [0,1] + 1 subdivision node on edge [0,2] + 2 subdivision nodes on edge [1,2] + remaining moves allow reaching more subdivision nodes.
example_2.py — Simple Chain
$ Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], maxMoves = 10, n = 4
Output: 23
💡 Note: With 10 moves from node 0, we can reach multiple original nodes and many subdivision nodes along various edges. The optimal paths allow reaching nodes 0,1,2,3 plus numerous intermediate subdivision nodes.
example_3.py — Disconnected Component
$ Input: edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5
Output: 1
💡 Note: Since node 0 is disconnected from the rest of the graph, we can only reach node 0 itself, regardless of the number of maxMoves available.

Constraints

  • 0 ≤ edges.length ≤ 104
  • edges[i].length == 3
  • 0 ≤ ui < vi < n
  • There are no multiple edges and no self loops in the given graph
  • 0 ≤ cnti ≤ 104
  • 0 ≤ maxMoves ≤ 109
  • 1 ≤ n ≤ 3000

Visualization

Tap to expand
Graph Subdivision VisualizationStep 1: Original Network012cnt=2cnt=1Step 2: Dijkstra's Algorithm012weight=3weight=2d=0d=3d=2Step 3: Reachable CitiesmaxMoves = 6✓ Node 0: distance 0 ≤ 6✓ Node 1: distance 3 ≤ 6✓ Node 2: distance 2 ≤ 6Reachable cities: 3Step 4: Subdivision Nodes CalculationEdge [0,1] with 2 subdivisions:0s1s21• From node 0 (d=0): can reach 6-0 = 6 steps → both subdivisions• From node 1 (d=3): can reach 6-3 = 3 steps → both subdivisions• Total reachable subdivisions: min(2, 6+3) = 2Edge [0,2] with 1 subdivision:• From node 0: 6-0 = 6 steps• From node 2: 6-2 = 4 steps• Reachable: min(1, 6+4) = 1Final ResultOriginal nodes: 3 + Subdivision nodes: 3 = Total: 6
Understanding the Visualization
1
Original Network
Start with cities connected by roads, each road marked with number of toll stations
2
Calculate Distances
Use Dijkstra's algorithm to find shortest paths between cities, treating toll count + 1 as road weight
3
Count Reachable Cities
Identify all cities reachable within the fuel budget
4
Count Toll Stations
For each road, calculate how many toll stations can be reached from either end city
Key Takeaway
🎯 Key Insight: Instead of building the massive subdivided graph, we use shortest path distances to calculate exactly how many subdivision nodes are reachable along each edge, making the solution efficient even for large subdivision counts.
Asked in
Google 25 Facebook 18 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen