You are given an undirected graph (the "original graph") with n nodes labeled from 0 to n - 1. You decide to subdivide each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge.
The graph is given as a 2D array of edges where edges[i] = [ui, vi, cnti] indicates that there is an edge between nodes ui and vi in the original graph, and cnti is the total number of new nodes that you will subdivide the edge into. Note that cnti == 0 means you will not subdivide the edge.
To subdivide the edge [ui, vi], replace it with (cnti + 1) new edges and cnti new nodes. The new nodes are x1, x2, ..., xcnti, and the new edges are [ui, x1], [x1, x2], [x2, x3], ..., [xcnti-1, xcnti], [xcnti, vi].
In this new graph, you want to know how many nodes are reachable from the node 0, where a node is reachable if the distance is maxMoves or less.
Given the original graph and maxMoves, return the number of nodes that are reachable from node 0 in the new graph.
Input & Output
Example 1 — Basic Case
$Input:edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
›Output:13
💡 Note:From node 0: can reach node 2 (distance 2), then reach some nodes on edge [0,1] and [1,2]. Total reachable includes all original nodes and several subdivided nodes.
Example 2 — Simple Path
$Input:edges = [[0,1,4],[1,2,6]], maxMoves = 10, n = 3
›Output:23
💡 Note:Can traverse the entire path 0→1→2 and reach most subdivided nodes along the way within 10 moves.
Example 3 — No Subdivisions
$Input:edges = [[1,2,0],[2,3,0]], maxMoves = 3, n = 4
›Output:1
💡 Note:Starting from node 0, cannot reach any other nodes since node 0 is isolated. Only node 0 itself is reachable.
Constraints
0 ≤ edges.length ≤ 104
0 ≤ ui, vi < n
There are no multiple edges in the graph
0 ≤ cnti ≤ 104
0 ≤ maxMoves ≤ 109
1 ≤ n ≤ 3000
Visualization
Tap to expand
Understanding the Visualization
1
Original Graph
Graph with edges containing subdivision counts
2
Subdivided Graph
Each edge is replaced with a chain of intermediate nodes
3
Reachable Nodes
Count all nodes reachable from node 0 within maxMoves distance
Key Takeaway
🎯 Key Insight: Use Dijkstra's algorithm to efficiently find shortest distances to original nodes, then calculate how many subdivided nodes are reachable on each edge
The key insight is to use Dijkstra's algorithm to find shortest paths to original nodes, then calculate how many subdivided nodes can be reached on each edge. Best approach is Dijkstra with Smart Edge Traversal. Time: O((V + E) log V), Space: O(V + E)
Common Approaches
Approach
Time
Space
Notes
✓
Dijkstra with Smart Edge Traversal
O((V + E) log V)
O(V + E)
Use Dijkstra's algorithm to find shortest paths to original nodes, then calculate reachable subdivided nodes on edges
Brute Force BFS
O(E × M + V + E × M)
O(E × M)
Build the entire subdivided graph and run BFS to count reachable nodes
Dijkstra with Smart Edge Traversal — Algorithm Steps
Build adjacency list from original graph
Run Dijkstra from node 0 to get shortest distances
For each edge, calculate reachable subdivided nodes from both endpoints
Sum up original nodes + subdivided nodes
Visualization
Tap to expand
Step-by-Step Walkthrough
1
Dijkstra's Algorithm
Find shortest distances to all original nodes
2
Edge Analysis
For each edge, calculate reachable subdivided nodes from both ends
3
Sum Results
Add original nodes + subdivided nodes
Code -
solution.c — C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX_NODES 3000
#define MAX_EDGES 10000
typedef struct {
int node, distance;
} HeapItem;
typedef struct {
HeapItem items[MAX_NODES * 10];
int size;
} MinHeap;
void heapPush(MinHeap* h, int node, int distance) {
int idx = h->size++;
h->items[idx].node = node;
h->items[idx].distance = distance;
while (idx > 0) {
int parent = (idx - 1) / 2;
if (h->items[parent].distance <= h->items[idx].distance) break;
HeapItem temp = h->items[parent];
h->items[parent] = h->items[idx];
h->items[idx] = temp;
idx = parent;
}
}
HeapItem heapPop(MinHeap* h) {
HeapItem result = h->items[0];
h->items[0] = h->items[--h->size];
int idx = 0;
while (2 * idx + 1 < h->size) {
int left = 2 * idx + 1;
int right = 2 * idx + 2;
int smallest = left;
if (right < h->size && h->items[right].distance < h->items[left].distance) {
smallest = right;
}
if (h->items[idx].distance <= h->items[smallest].distance) break;
HeapItem temp = h->items[idx];
h->items[idx] = h->items[smallest];
h->items[smallest] = temp;
idx = smallest;
}
return result;
}
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
int solution(int edges[][3], int edgeCount, int maxMoves, int n) {
// Build adjacency list
int graph[MAX_NODES][MAX_NODES][2]; // [node][neighbor_idx][0=neighbor, 1=weight]
int graphSize[MAX_NODES] = {0};
for (int i = 0; i < edgeCount; i++) {
int u = edges[i][0], v = edges[i][1], cnt = edges[i][2];
int weight = cnt + 1;
graph[u][graphSize[u]][0] = v;
graph[u][graphSize[u]][1] = weight;
graphSize[u]++;
graph[v][graphSize[v]][0] = u;
graph[v][graphSize[v]][1] = weight;
graphSize[v]++;
}
// Dijkstra from node 0
int dist[MAX_NODES];
for (int i = 0; i < n; i++) {
dist[i] = INT_MAX;
}
dist[0] = 0;
MinHeap heap = {.size = 0};
heapPush(&heap, 0, 0);
while (heap.size > 0) {
HeapItem current = heapPop(&heap);
int u = current.node, d = current.distance;
if (d > dist[u]) continue;
for (int i = 0; i < graphSize[u]; i++) {
int v = graph[u][i][0], weight = graph[u][i][1];
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
heapPush(&heap, v, dist[v]);
}
}
}
// Count reachable nodes
int result = 0;
// Count reachable original nodes
for (int i = 0; i < n; i++) {
if (dist[i] <= maxMoves) {
result++;
}
}
// Count reachable subdivided nodes on edges
for (int i = 0; i < edgeCount; i++) {
int u = edges[i][0], v = edges[i][1], cnt = edges[i][2];
// How many nodes can we reach from u side?
int reachableFromU = 0;
if (dist[u] <= maxMoves) {
reachableFromU = max(0, min(cnt, maxMoves - dist[u]));
}
// How many nodes can we reach from v side?
int reachableFromV = 0;
if (dist[v] <= maxMoves) {
reachableFromV = max(0, min(cnt, maxMoves - dist[v]));
}
// Total reachable on this edge (avoid double counting)
result += min(cnt, reachableFromU + reachableFromV);
}
return result;
}
int main() {
char line[10000];
fgets(line, sizeof(line), stdin);
// Simple parsing for [[a,b,c],[d,e,f]] format
int edges[MAX_EDGES][3];
int edgeCount = 0;
if (line[1] != ']') { // Not empty array
char* ptr = strchr(line, '[');
ptr++; // Skip first [
while (*ptr && *ptr != ']') {
if (*ptr == '[') {
ptr++;
edges[edgeCount][0] = strtol(ptr, &ptr, 10);
ptr++; // Skip comma
edges[edgeCount][1] = strtol(ptr, &ptr, 10);
ptr++; // Skip comma
edges[edgeCount][2] = strtol(ptr, &ptr, 10);
edgeCount++;
}
ptr++;
}
}
int maxMoves, n;
scanf("%d", &maxMoves);
scanf("%d", &n);
printf("%d\n", solution(edges, edgeCount, maxMoves, n));
return 0;
}