Network Recovery Pathways Problem
Description
LeetCode Problem 3620.
You are given a directed acyclic graph of n nodes numbered from 0 to n - 1. This is represented by a 2D array edges of length m, where edges[i] = [u_i, v_i, cost_i] indicates a one-way communication from node u_i to node v_i with a recovery cost of cost_i.
Some nodes may be offline. You are given a boolean array online where online[i] = true means node i is online. Nodes 0 and n - 1 are always online.
A path from 0 to n - 1 is valid if:
- All intermediate nodes on the path are online.
- The total recovery cost of all edges on the path does not exceed
k.
For each valid path, define its score as the minimum edge-cost along that path.
Return the maximum path score (i.e., the largest minimum-edge cost) among all valid paths. If no valid path exists, return -1.
Example 1:
Input: edges = [[0,1,5],[1,3,10],[0,2,3],[2,3,4]], online = [true,true,true,true], k = 10
Output: 3
Explanation:

- The graph has two possible routes from node 0 to node 3:
1. Path 0 → 1 → 3
- Total cost = 5 + 10 = 15, which exceeds k (15 > 10), so this path is invalid.
2. Path 0 → 2 → 3
- Total cost = 3 + 4 = 7 <= k, so this path is valid.
- The minimum edge‐cost along this path is min(3, 4) = 3.
- There are no other valid paths. Hence, the maximum among all valid path‐scores is 3.
Example 2:
Input: edges = [[0,1,7],[1,4,5],[0,2,6],[2,3,6],[3,4,2],[2,4,6]], online = [true,true,true,false,true], k = 12
Output: 6
Explanation:

- Node 3 is offline, so any path passing through 3 is invalid.
- Consider the remaining routes from 0 to 4:
1. Path 0 → 1 → 4
- Total cost = 7 + 5 = 12 <= k, so this path is valid.
- The minimum edge‐cost along this path is min(7, 5) = 5.
2. Path 0 → 2 → 3 → 4
- Node 3 is offline, so this path is invalid regardless of cost.
3. Path 0 → 2 → 4
- Total cost = 6 + 6 = 12 <= k, so this path is valid.
- The minimum edge‐cost along this path is min(6, 6) = 6.
- Among the two valid paths, their scores are 5 and 6. Therefore, the answer is 6.
Constraints:
n == online.length2 <= n <= 5 * 10^40 <= m == edges.length <= min(10^5, n * (n - 1) / 2)edges[i] = [u_i, v_i, cost_i]0 <= u_i, v_i < nu_i != v_i0 <= cost_i <= 10^90 <= k <= 5 * 10^13online[i]is eithertrueorfalse, and bothonline[0]andonline[n - 1]aretrue.- The given graph is a directed acyclic graph.
Solution
Approach: Binary Search + Dijkstra
The score of a path is its smallest edge cost, and we want the largest possible score. Fix a candidate score mid and keep only the edges with cost at least mid; the score of any path in that graph is at least mid. Then ask whether some path from 0 to n - 1 costs at most k, which Dijkstra with a heap answers. Edges that touch an offline node are dropped up front, which also keeps offline nodes off every path.
If a larger mid works, a smaller one does as well, so we can binary search mid between the smallest and largest edge cost. If even the smallest cost fails, return -1.
Complexity: O((n + m) log n · log W) time and O(n + m) space, where W is the largest edge cost.
from heapq import heappop, heappush
from math import inf
class Solution:
def findMaxPathScore(
self, edges: List[List[int]], online: List[bool], k: int
) -> int:
def check(mid: int) -> int:
dist = [inf] * n
dist[0] = 0
pq = [(0, 0)]
while pq:
d, u = heappop(pq)
if d > k:
return False
if u == n - 1:
return True
if dist[u] < d:
continue
for v, w in g[u]:
if w < mid:
continue
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
heappush(pq, (dist[v], v))
return False
n = len(online)
g = [[] for _ in range(n)]
l, r = inf, 0
for (
u,
v,
w,
) in edges:
if not online[u] or not online[v]:
continue
g[u].append((v, w))
l = min(l, w)
r = max(r, w)
while l < r:
mid = (l + r + 1) >> 1
if check(mid):
l = mid
else:
r = mid - 1
return l if check(l) else -1