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] = [Ai, Bi, Wi]
indicates that there is an edge going from node Ai to node Bi with weight
Wi.
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 mostthreshold outgoing 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.
Input: n =5, edges =[[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]], threshold
=2Output: 1Explanation:

Remove the edge `2 -> 0`. The maximum weight among the remaining edges is1.
Input: n =5, edges =[[1,2,1],[1,3,3],[1,4,5],[2,3,2],[3,4,2],[4,0,1]],threshold =1Output: 2Explanation:

Remove the edges `1 -> 3` and `1 -> 4`. The maximum weight among the remaining
edges is2.
To minimize the maximum edge weight, we can use binary search on the possible edge weights. For each candidate maximum weight, we check if it’s possible to keep only edges with weight ≤ candidate, prune outgoing edges to threshold per node, and ensure node 0 is reachable from all other nodes.
classSolution {
public:int minimizeMaxEdgeWeight(int n, vector<vector<int>>& edges, int threshold) {
int lo =1, hi =0;
for (auto& e : edges) hi = max(hi, e[2]);
int ans =-1;
while (lo <= hi) {
int mid = lo + (hi - lo) /2;
vector<vector<pair<int,int>>> g(n);
for (auto& e : edges) {
if (e[2] <= mid) g[e[0]].emplace_back(e[1], e[2]);
}
for (int i =0; i < n; ++i) {
sort(g[i].begin(), g[i].end(), [](auto& a, auto& b){ return a.second < b.second; });
if (g[i].size() > threshold) g[i].resize(threshold);
}
vector<bool> vis(n);
queue<int> q;
q.push(0); vis[0] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto& [v, w] : g[u]) {
if (!vis[v]) { vis[v] = true; q.push(v); }
}
}
bool ok = true;
for (int i =0; i < n; ++i) if (!vis[i]) ok = false;
if (ok) { ans = mid; hi = mid -1; }
else lo = mid +1;
}
return ans;
}
};
classSolution {
publicintminimizeMaxEdgeWeight(int n, int[][] edges, int threshold) {
int lo = 1, hi = 0;
for (int[] e : edges) hi = Math.max(hi, e[2]);
int ans =-1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
List<List<int[]>> g =new ArrayList<>();
for (int i = 0; i < n; ++i) g.add(new ArrayList<>());
for (int[] e : edges) {
if (e[2]<= mid) g.get(e[0]).add(newint[]{e[1], e[2]});
}
for (int i = 0; i < n; ++i) {
g.get(i).sort(Comparator.comparingInt(a -> a[1]));
if (g.get(i).size() > threshold) g.get(i).subList(threshold, g.get(i).size()).clear();
}
boolean[] vis =newboolean[n];
Queue<Integer> q =new LinkedList<>();
q.add(0); vis[0]=true;
while (!q.isEmpty()) {
int u = q.poll();
for (int[] vw : g.get(u)) {
int v = vw[0];
if (!vis[v]) { vis[v]=true; q.add(v); }
}
}
boolean ok =true;
for (int i = 0; i < n; ++i) if (!vis[i]) ok =false;
if (ok) { ans = mid; hi = mid - 1; }
else lo = mid + 1;
}
return ans;
}
}
classSolution {
funminimizeMaxEdgeWeight(n: Int, edges: Array<IntArray>, threshold: Int): Int {
var lo = 1var hi = edges.maxOf { it[2] }
var ans = -1while (lo <= hi) {
val mid = lo + (hi-lo)/2val g = Array(n) { mutableListOf<Pair<Int,Int>>() }
for (e in edges) if (e[2] <= mid) g[e[0]].add(e[1] to e[2])
for (i in0 until n) {
g[i].sortBy { it.second }
if (g[i].size > threshold) g[i] = g[i].take(threshold).toMutableList()
}
val vis = BooleanArray(n)
val q = ArrayDeque<Int>()
q.add(0); vis[0] = truewhile (q.isNotEmpty()) {
val u = q.removeFirst()
for ((v, _) in g[u]) {
if (!vis[v]) { vis[v] = true; q.add(v) }
}
}
val ok = vis.all { it }
if (ok) { ans = mid; hi = mid-1 }
else lo = mid+1 }
return ans
}
}
defminimize_max_edge_weight(n: int, edges: list[list[int]], threshold: int) -> int:
lo, hi =1, max(w for _,_,w in edges)
ans =-1while lo <= hi:
mid = (lo + hi) //2 g = [[] for _ in range(n)]
for a, b, w in edges:
if w <= mid:
g[a].append((b, w))
for i in range(n):
g[i].sort(key=lambda x: x[1])
if len(g[i]) > threshold:
g[i] = g[i][:threshold]
vis = [False]*n
q = [0]
vis[0] =Truewhile q:
u = q.pop(0)
for v, _ in g[u]:
ifnot vis[v]:
vis[v] =True q.append(v)
if all(vis):
ans = mid
hi = mid -1else:
lo = mid +1return ans
impl Solution {
pubfnminimize_max_edge_weight(n: i32, edges: Vec<Vec<i32>>, threshold: i32) -> i32 {
let n = n asusize;
letmut lo =1;
letmut hi = edges.iter().map(|e| e[2]).max().unwrap();
letmut ans =-1;
while lo <= hi {
let mid = lo + (hi-lo)/2;
letmut g =vec![vec![]; n];
for e in edges.iter() {
if e[2] <= mid {
g[e[0] asusize].push((e[1] asusize, e[2]));
}
}
for i in0..n {
g[i].sort_by_key(|x| x.1);
if g[i].len() > threshold asusize {
g[i].truncate(threshold asusize);
}
}
letmut vis =vec![false; n];
letmut q = std::collections::VecDeque::new();
q.push_back(0); vis[0] =true;
whilelet Some(u) = q.pop_front() {
for&(v, _) in&g[u] {
if!vis[v] { vis[v] =true; q.push_back(v); }
}
}
if vis.iter().all(|&x| x) {
ans = mid;
hi = mid-1;
} else {
lo = mid+1;
}
}
ans
}
}