Problem solution · C++

Reachable Nodes In Subdivided Graph

Reachable Nodes In Subdivided Graph: a C++ solution using heap or priority queue. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Heap or priority queue
Source
walkccc LeetCode Solutions
Length
58 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Heap or priority queue

For Reachable Nodes In Subdivided Graph, the implementation repeatedly takes the currently best candidate from a heap while inserting newly available choices.

  1. Define the priority key and whether the smallest or largest item should lead.
  2. Push each candidate when it becomes eligible.
  3. Discard stale entries when necessary and process the best live candidate.

Code notes

  • 58 lines of C++ from the credited upstream file 882.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 4 loop blocks detected.

Complexity

Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeReachable Nodes In Subdivided Graph · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int reachableNodes(vector<vector<int>>& edges, int maxMoves, int n) {    vector<vector<pair<int, int>>> graph(n);    vector<int> dist(graph.size(), maxMoves + 1);     for (const vector<int>& edge : edges) {      const int u = edge[0];      const int v = edge[1];      const int cnt = edge[2];      graph[u].emplace_back(v, cnt);      graph[v].emplace_back(u, cnt);    }     const int reachableNodes = dijkstra(graph, 0, maxMoves, dist);    int reachableSubnodes = 0;     for (const vector<int>& edge : edges) {      const int u = edge[0];      const int v = edge[1];      const int cnt = edge[2];      // the number of reachable nodes of `edge` from `u`      const int a = dist[u] > maxMoves ? 0 : min(maxMoves - dist[u], cnt);      // the number of reachable nodes of `edge` from `v`      const int b = dist[v] > maxMoves ? 0 : min(maxMoves - dist[v], cnt);      reachableSubnodes += min(a + b, cnt);    }     return reachableNodes + reachableSubnodes;  }  private:  int dijkstra(const vector<vector<pair<int, int>>>& graph, int src,               int maxMoves, vector<int>& dist) {    dist[src] = 0;    using P = pair<int, int>;  // (d, u)    priority_queue<P, vector<P>, greater<>> minHeap;    minHeap.emplace(dist[src], src);     while (!minHeap.empty()) {      const auto [d, u] = minHeap.top();      minHeap.pop();      // Already took `maxMoves` to reach `u`, so can't explore anymore.      if (d >= maxMoves)        break;      if (d > dist[u])        continue;      for (const auto& [v, w] : graph[u])        if (d + w + 1 < dist[v]) {          dist[v] = d + w + 1;          minHeap.emplace(dist[v], v);        }    }     return ranges::count_if(dist, [&](int d) { return d <= maxMoves; });  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗