Problem solution · C++

Minimum Cost to Reach Destination in Time

Minimum Cost to Reach Destination in Time: 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 Minimum Cost to Reach Destination in Time, 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 1928.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 3 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 codeMinimum Cost to Reach Destination in Time · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int minCost(int maxTime, vector<vector<int>>& edges,              vector<int>& passingFees) {    const int n = passingFees.size();    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];      graph[u].emplace_back(v, w);      graph[v].emplace_back(u, w);    }     return dijkstra(graph, 0, n - 1, maxTime, passingFees);  }  private:  int dijkstra(const vector<vector<pair<int, int>>>& graph, int src, int dst,               int maxTime, const vector<int>& passingFees) {    // cost[i] := the minimum cost to reach the i-th city    vector<int> cost(graph.size(), INT_MAX);    // dist[i] := the minimum time to reach the i-th city    vector<int> dist(graph.size(), maxTime + 1);     cost[src] = passingFees[src];    dist[src] = 0;    using T = tuple<int, int, int>;  // (cost[u], dist[u], u)    priority_queue<T, vector<T>, greater<>> minHeap;    minHeap.emplace(cost[src], dist[src], src);     while (!minHeap.empty()) {      const auto [currCost, d, u] = minHeap.top();      minHeap.pop();      if (u == dst)        return cost[dst];      if (d > dist[u] && currCost > cost[u])        continue;      for (const auto& [v, w] : graph[u]) {        if (d + w > maxTime)          continue;        // Go from u -> v.        if (currCost + passingFees[v] < cost[v]) {          cost[v] = currCost + passingFees[v];          dist[v] = d + w;          minHeap.emplace(cost[v], dist[v], v);        } else if (d + w < dist[v]) {          dist[v] = d + w;          minHeap.emplace(currCost + passingFees[v], dist[v], v);        }      }    }     return -1;  }}; 

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 ↗