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 node0in the new graph.
Input:
edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
Output:
13
Explanation: The edge subdivisions are shown in the image above.
The nodes that are reachable are highlighted in yellow.
Input:
edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5
Output:
1
Explanation: Node 0 is disconnected from the rest of the graph, so only node 0 is reachable.
The key idea is to use Dijkstra’s algorithm to find the maximum number of moves left when reaching each node, and for each edge, track how many new subdivided nodes can be reached from both sides. The answer is the sum of all reachable original nodes and all reachable subdivided nodes on each edge.
classSolution {
public:int reachableNodes(vector<vector<int>>& edges, int maxMoves, int n) {
vector<vector<pair<int,int>>> g(n);
for (auto& e : edges) {
g[e[0]].emplace_back(e[1], e[2]);
g[e[1]].emplace_back(e[0], e[2]);
}
vector<int> moves(n, -1);
priority_queue<pair<int,int>> pq;
pq.emplace(maxMoves, 0);
while (!pq.empty()) {
auto [mv, u] = pq.top(); pq.pop();
if (moves[u] >=0) continue;
moves[u] = mv;
for (auto& [v, cnt] : g[u]) {
int left = mv - cnt -1;
if (left >=0&& moves[v] <0) pq.emplace(left, v);
}
}
int ans =0;
for (int x : moves) if (x >=0) ans++;
for (auto& e : edges) {
int a = moves[e[0]] <0?0: moves[e[0]];
int b = moves[e[1]] <0?0: moves[e[1]];
ans += min(e[2], a + b);
}
return ans;
}
};
classSolution {
publicintreachableNodes(int[][] edges, int maxMoves, int n) {
List<int[]>[] g =new List[n];
for (int i = 0; i < n; i++) g[i]=new ArrayList<>();
for (int[] e : edges) {
g[e[0]].add(newint[]{e[1], e[2]});
g[e[1]].add(newint[]{e[0], e[2]});
}
int[] moves =newint[n];
Arrays.fill(moves, -1);
PriorityQueue<int[]> pq =new PriorityQueue<>((a, b) -> b[0]- a[0]);
pq.offer(newint[]{maxMoves, 0});
while (!pq.isEmpty()) {
int[] cur = pq.poll();
int mv = cur[0], u = cur[1];
if (moves[u]>= 0) continue;
moves[u]= mv;
for (int[] vw : g[u]) {
int v = vw[0], cnt = vw[1];
int left = mv - cnt - 1;
if (left >= 0 && moves[v]< 0) pq.offer(newint[]{left, v});
}
}
int ans = 0;
for (int x : moves) if (x >= 0) ans++;
for (int[] e : edges) {
int a = moves[e[0]]< 0 ? 0 : moves[e[0]];
int b = moves[e[1]]< 0 ? 0 : moves[e[1]];
ans += Math.min(e[2], a + b);
}
return ans;
}
}
classSolution {
funreachableNodes(edges: Array<IntArray>, maxMoves: Int, n: Int): Int {
val g = Array(n) { mutableListOf<Pair<Int,Int>>() }
for (e in edges) {
g[e[0]].add(e[1] to e[2])
g[e[1]].add(e[0] to e[2])
}
val moves = IntArray(n) { -1 }
val pq = java.util.PriorityQueue(compareByDescending<Pair<Int,Int>> { it.first })
pq.add(maxMoves to 0)
while (pq.isNotEmpty()) {
val(mv, u) = pq.remove()
if (moves[u] >=0) continue moves[u] = mv
for ((v, cnt) in g[u]) {
val left = mv - cnt - 1if (left >=0&& moves[v] < 0) pq.add(left to v)
}
}
var ans = moves.count { it>=0 }
for (e in edges) {
val a = if (moves[e[0]] < 0) 0else moves[e[0]]
val b = if (moves[e[1]] < 0) 0else moves[e[1]]
ans += minOf(e[2], a + b)
}
return ans
}
}
classSolution:
defreachableNodes(self, edges: list[list[int]], maxMoves: int, n: int) -> int:
import heapq
g = [[] for _ in range(n)]
for u, v, cnt in edges:
g[u].append((v, cnt))
g[v].append((u, cnt))
moves = [-1] * n
h = [(-maxMoves, 0)]
while h:
mv, u = heapq.heappop(h)
mv =-mv
if moves[u] >=0:
continue moves[u] = mv
for v, cnt in g[u]:
left = mv - cnt -1if left >=0and moves[v] <0:
heapq.heappush(h, (-left, v))
ans = sum(x >=0for x in moves)
for u, v, cnt in edges:
a = moves[u] if moves[u] >=0else0 b = moves[v] if moves[v] >=0else0 ans += min(cnt, a + b)
return ans