Problem solution · C++

Minimum Threshold Path with Limited Heavy Edges

Minimum Threshold Path with Limited Heavy Edges: a C++ solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
57 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

Breadth-first search

For Minimum Threshold Path with Limited Heavy Edges, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 57 lines of C++ from the credited upstream file minimum-threshold-path-with-limited-heavy-edges.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 4 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

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

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Threshold Path with Limited Heavy Edges · C++C++
Use this to learn the idea, then write your own version.
// Time:  O((n + e) * loge)// Space: O(n + e) // binary search, 0-1 bfs, dequeclass Solution {public:    int minimumThreshold(int n, vector<vector<int>>& edges, int source, int target, int k) {        const auto& binary_search = [](int left, int right, const auto& check) {            while (left <= right) {                const auto& mid = left + (right - left) / 2;                if (check(mid)) {                    right = mid - 1;                } else {                    left = mid + 1;                }            }            return left;        };         vector<vector<pair<int, int>>> adj(n);        vector<int> weights = {0};        const auto& check = [&](int i) {            const auto& t = weights[i];            vector<bool> lookup(n);            deque<pair<int, int>> dq = {{source, 0}};            while (!empty(dq)) {                const auto [u, d] = dq.front(); dq.pop_front();                if (lookup[u]) {                    continue;                }                lookup[u] = true;                if (u == target) {                    return d <= k;                }                for (const auto& [v, w] : adj[u]) {                    if (w <= t) {                        dq.emplace_front(v, d);                    } else {                        dq.emplace_back(v, d + 1);                    }                }            }            return false;        };         for (const auto& e : edges) {            adj[e[0]].emplace_back(e[1], e[2]);            adj[e[1]].emplace_back(e[0], e[2]);            weights.emplace_back(e[2]);        }        ranges::sort(weights);        weights.erase(unique(begin(weights), end(weights)), end(weights));        const auto& i = binary_search(0, size(weights) - 1, check);        return i < size(weights) ? weights[i] : -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 ↗