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
- 52 lines of Python from the credited upstream file minimum-threshold-path-with-limited-heavy-edges.py.
- The implementation visibly relies on sequence storage, ordered lookup, work queue.
- No explicit 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 4import collections5 6 78class Solution(object):9 def minimumThreshold(self, n, edges, source, target, k):10 """11 :type n: int12 :type edges: List[List[int]]13 :type source: int14 :type target: int15 :type k: int16 :rtype: int17 """18 def binary_search(left, right, check):19 while left <= right:20 mid = left+(right-left)221 if check(mid):22 right = mid-123 else:24 left = mid+125 return left26 27 def check(i):28 t = weights[i]29 lookup = [False]*n30 dq = collections.deque([(source, 0)])31 while dq:32 u, d = dq.popleft()33 if lookup[u]:34 continue35 lookup[u] = True36 if u == target:37 return d <= k38 for v, w in adj[u]:39 if w <= t:40 dq.appendleft((v, d))41 else:42 dq.append((v, d+1))43 return False44 45 adj = [[] for _ in xrange(n)]46 for u, v, w in edges:47 adj[u].append((v, w))48 adj[v].append((u, w))49 weights = sorted(set([0]+[w[2] for w in edges]))50 i = binary_search(0, len(weights)-1, check)51 return weights[i] if i < len(weights) else -152