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.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- 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.
Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 int minimumThreshold(int n, vector<vector<int>>& edges, int source, int target, int k) {8 const auto& binary_search = [](int left, int right, const auto& check) {9 while (left <= right) {10 const auto& mid = left + (right - left) / 2;11 if (check(mid)) {12 right = mid - 1;13 } else {14 left = mid + 1;15 }16 }17 return left;18 };19 20 vector<vector<pair<int, int>>> adj(n);21 vector<int> weights = {0};22 const auto& check = [&](int i) {23 const auto& t = weights[i];24 vector<bool> lookup(n);25 deque<pair<int, int>> dq = {{source, 0}};26 while (!empty(dq)) {27 const auto [u, d] = dq.front(); dq.pop_front();28 if (lookup[u]) {29 continue;30 }31 lookup[u] = true;32 if (u == target) {33 return d <= k;34 }35 for (const auto& [v, w] : adj[u]) {36 if (w <= t) {37 dq.emplace_front(v, d);38 } else {39 dq.emplace_back(v, d + 1);40 }41 }42 }43 return false;44 };45 46 for (const auto& e : edges) {47 adj[e[0]].emplace_back(e[1], e[2]);48 adj[e[1]].emplace_back(e[0], e[2]);49 weights.emplace_back(e[2]);50 }51 ranges::sort(weights);52 weights.erase(unique(begin(weights), end(weights)), end(weights));53 const auto& i = binary_search(0, size(weights) - 1, check);54 return i < size(weights) ? weights[i] : -1;55 }56};57