Minimize the Maximum Edge Weight of Graph Problem
Description
LeetCode Problem 3419.
You are given two integers, n and threshold, as well as a directed weighted graph of n nodes numbered from 0 to n - 1. The graph is represented by a 2D integer array edges, where edges[i] = [A_i, B_i, W_i] indicates that there is an edge going from node A_i to node B_i with weight W_i.
You have to remove some edges from this graph (possibly none), so that it satisfies the following conditions:
- Node 0 must be reachable from all other nodes.
- The maximum edge weight in the resulting graph is minimized.
- Each node has at most
thresholdoutgoing edges.
Return the minimum possible value of the maximum edge weight after removing the necessary edges. If it is impossible for all conditions to be satisfied, return -1.
Example 1:
Input: n = 5, edges = [[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]], threshold = 2
Output: 1
Explanation:

Remove the edge 2 -> 0. The maximum weight among the remaining edges is 1.
Example 2:
Input: n = 5, edges = [[0,1,1],[0,2,2],[0,3,1],[0,4,1],[1,2,1],[1,4,1]], threshold = 1
Output: -1
Explanation:
It is impossible to reach node 0 from node 2.
Example 3:
Input: n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[3,4,2],[4,0,1]], threshold = 1
Output: 2
Explanation:

Remove the edges 1 -> 3 and 1 -> 4. The maximum weight among the remaining edges is 2.
Example 4:
Input: n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[4,0,1]], threshold = 1
Output: -1
Constraints:
2 <= n <= 10^51 <= threshold <= n - 11 <= edges.length <= min(10^5, n * (n - 1) / 2).edges[i].length == 30 <= A_i, B_i < nA_i != B_i1 <= W_i <= 10^6- There may be multiple edges between a pair of nodes, but they must have unique weights.
Solution
Approach: Binary Search on the Answer + Graph Search
Node 0 must be reachable from every node, so we only need, for each node, one path of outgoing edges that ends at node 0. Keeping a single outgoing edge per node (the first edge on its path) is enough, so the limit of threshold outgoing edges never gets in the way, since threshold >= 1.
The question becomes: what is the smallest limit such that every node can reach node 0 using only edges of weight at most limit? Reverse the edges and search from node 0 over edges with weight <= limit; the limit works if all n nodes are visited. Reachability only improves as the limit grows, so binary search for the smallest working limit. If even the largest edge weight does not reach every node, the answer is -1.
Complexity: O((n + m) log W) time and O(n + m) space, where m is the number of edges and W the largest weight.
class Solution:
def minMaxWeight(self, n: int, edges: List[List[int]], threshold: int) -> int:
# Keeping one outgoing edge per node (a tree of paths towards node 0) always satisfies
# `threshold`, so only reachability matters. Walk the reversed graph from node 0 using edges
# of weight <= limit and binary search the smallest limit that reaches every node.
g = [[] for _ in range(n)]
for a, b, w in edges:
g[b].append((a, w))
def reach_all(limit):
seen = [False] * n
seen[0] = True
stack = [0]
count = 1
while stack:
u = stack.pop()
for v, w in g[u]:
if w <= limit and not seen[v]:
seen[v] = True
count += 1
stack.append(v)
return count == n
lo, hi = 1, max(e[2] for e in edges)
if not reach_all(hi):
return -1
while lo < hi:
mid = (lo + hi) // 2
if reach_all(mid):
hi = mid
else:
lo = mid + 1
return lo