Minimum Threshold Path With Limited Heavy Edges Problem
Description
LeetCode Problem 3924.
There is an undirected weighted graph with n nodes labeled from 0 to n - 1.
The graph is represented by a 2D integer array edges, where each edge edges[i] = [u_i, v_i, w_i] indicates that there is an undirected edge between nodes u_i and v_i with weight w_i.
You are also given integers source, target and k.
A threshold value determines whether an edge is considered light or heavy:
- An edge is light if its weight is less than or equal to
threshold. - An edge is heavy if its weight is greater than
threshold.
A path from source to target is valid if it contains at most k heavy edges.
Return the minimum integer threshold such that at least one valid path exists from source to target. If no such path exists, return -1.
Example 1:

Input: n = 6, edges = [[0,1,5],[1,2,3],[3,4,4],[4,5,1],[1,4,2]], source = 0, target = 3, k = 1
Output: 4
Explanation:
The minimum threshold such that a path from node 0 to node 3 uses at most 1 heavy edge is 4.
- Light edges: [1, 2, 3], [3, 4, 4], [4, 5, 1], [1, 4, 2]
- Heavy edges: [0, 1, 5]
A valid path is 0 → 1 → 4 → 3. It uses only 1 heavy edge ([0, 1, 5]), which satisfies the limit k = 1.
Any smaller threshold would make it impossible to reach node 3 without exceeding 1 heavy edge.
Example 2:

Input: n = 6, edges = [[0,1,3],[1,2,4],[3,4,5],[4,5,6]], source = 0, target = 4, k = 1
Output: -1
Explanation:
There is no path from node 0 to node 4. Since the target cannot be reached, the output is -1.
Example 3:

Input: n = 4, edges = [[0,1,2],[1,2,2],[2,3,2],[3,0,2]], source = 0, target = 0, k = 0
Output: 0
Explanation:
The source and target are the same node. No edges need to be traversed, so the minimum threshold is 0.
Constraints:
1 <= n <= 10^30 <= edges.length <= 10^3edges[i] = [u_i, v_i, w_i]0 <= u_i, v_i <= n - 11 <= w_i <= 10^90 <= source, target <= n - 10 <= k <= edges.length
Solution
Approach: Binary Search on the Threshold + 0-1 BFS
If a threshold works, any larger threshold works too, since heavy edges only become light. So we can binary search the smallest working threshold. The answer is either 0 (when k heavy edges are enough for some path) or one of the edge weights, so we search over 0 and the sorted distinct weights.
For a fixed threshold, give every light edge cost 0 and every heavy edge cost 1. The cheapest path from source to target then uses the fewest heavy edges, and it is found with a 0-1 BFS (a deque where cost-0 edges push to the front and cost-1 edges push to the back). The threshold is valid when that cost is at most k.
First test the largest weight, where every edge is light: if even that fails, target is not reachable and the answer is -1.
Complexity: O((n + m) log m) time and O(n + m) extra space, where m is the number of edges.
from collections import deque
class Solution:
def minimumThreshold(self, n: int, edges: List[List[int]], source: int, target: int, k: int) -> int:
g = [[] for _ in range(n)]
for u, v, w in edges:
g[u].append((v, w))
g[v].append((u, w))
def ok(t: int) -> bool:
dist = [n * 2 + 5] * n
dist[source] = 0
q = deque([source])
while q:
u = q.popleft()
for v, w in g[u]:
c = dist[u] + (1 if w > t else 0)
if c < dist[v]:
dist[v] = c
if w > t:
q.append(v)
else:
q.appendleft(v)
return dist[target] <= k
cand = [0] + sorted(set(w for _, _, w in edges))
if not ok(cand[-1]):
return -1
lo, hi = 0, len(cand) - 1
while lo < hi:
mid = (lo + hi) // 2
if ok(cand[mid]):
hi = mid
else:
lo = mid + 1
return cand[lo]