Problem solution · C++

Modify Graph Edge Weights

Modify Graph Edge Weights: 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
77 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 Modify Graph Edge Weights, 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

  • 77 lines of C++ from the credited upstream file 2699.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 6 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 codeModify Graph Edge Weights · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges,                                         int source, int destination,                                         int target) {    constexpr int kMax = 2'000'000'000;    vector<vector<pair<int, int>>> graph(n);     for (const vector<int>& edge : edges) {      const int u = edge[0];      const int v = edge[1];      const int w = edge[2];      if (w == -1)        continue;      graph[u].emplace_back(v, w);      graph[v].emplace_back(u, w);    }     int distToDestination = dijkstra(graph, source, destination);    if (distToDestination < target)      return {};    if (distToDestination == target) {      // Change the weights of negative edges to an impossible value.      for (vector<int>& edge : edges)        if (edge[2] == -1)          edge[2] = kMax;      return edges;    }     for (int i = 0; i < edges.size(); ++i) {      const int u = edges[i][0];      const int v = edges[i][1];      int& w = edges[i][2];      if (w != -1)        continue;      w = 1;      graph[u].emplace_back(v, 1);      graph[v].emplace_back(u, 1);      distToDestination = dijkstra(graph, source, destination);      if (distToDestination <= target) {        w += target - distToDestination;        // Change the weights of negative edges to an impossible value.        for (int j = i + 1; j < edges.size(); ++j)          if (edges[j][2] == -1)            edges[j][2] = kMax;        return edges;      }    }     return {};  }  private:  int dijkstra(const vector<vector<pair<int, int>>>& graph, int src, int dst) {    vector<int> dist(graph.size(), INT_MAX);     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();      if (d > dist[u])        continue;      for (const auto& [v, w] : graph[u])        if (d + w < dist[v]) {          dist[v] = d + w;          minHeap.emplace(dist[v], v);        }    }     return dist[dst];  }}; 

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 ↗