Problem solution · C++

Minimize the Total Price of the Trips

Minimize the Total Price of the Trips: a C++ solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Depth-first search
Source
walkccc LeetCode Solutions
Length
67 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

Depth-first search

For Minimize the Total Price of the Trips, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 67 lines of C++ from the credited upstream file 2646.cpp.
  • The implementation visibly relies on sequence storage.
  • 6 loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

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 codeMinimize the Total Price of the Trips · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int minimumTotalPrice(int n, vector<vector<int>>& edges, vector<int>& price,                        vector<vector<int>>& trips) {    vector<vector<int>> graph(n);     for (const vector<int>& edge : edges) {      const int u = edge[0];      const int v = edge[1];      graph[u].push_back(v);      graph[v].push_back(u);    }     // count[i] := the number of times node i is traversed    vector<int> count(n);     for (const vector<int>& trip : trips) {      const int start = trip[0];      const int end = trip[1];      vector<int> path;      dfsCount(graph, start, /*prev=*/-1, end, count, path);    }     vector<vector<int>> mem(n, vector<int>(2, -1));    return dfs(graph, 0, -1, price, count, false, mem);  }  private:  void dfsCount(const vector<vector<int>>& graph, int u, int prev, int end,                vector<int>& count, vector<int>& path) {    path.push_back(u);    if (u == end) {      for (const int i : path)        ++count[i];      return;    }    for (const int v : graph[u])      if (v != prev)        dfsCount(graph, v, u, end, count, path);    path.pop_back();  }   // Returns the minimum price sum for the i-th node, where its parent is  // halved parent or not halved not.  int dfs(const vector<vector<int>>& graph, int u, int prev,          const vector<int>& price, const vector<int>& count, int parentHalved,          vector<vector<int>>& mem) {    if (mem[u][parentHalved] != -1)      return mem[u][parentHalved];     int sumWithFullNode = price[u] * count[u];    for (const int v : graph[u])      if (v != prev)        sumWithFullNode += dfs(graph, v, u, price, count, false, mem);     if (parentHalved)  // Can't halve this node if its parent was halved.      return mem[u][parentHalved] = sumWithFullNode;     int sumWithHalvedNode = (price[u] / 2) * count[u];    for (const int v : graph[u])      if (v != prev)        sumWithHalvedNode += dfs(graph, v, u, price, count, true, mem);     return mem[u][parentHalved] = min(sumWithFullNode, sumWithHalvedNode);  }}; 

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 ↗