Create A2050.java

This commit is contained in:
Mrhongxing committed 2026-10-25 14:00:47 +08:00
1 parent 75701ddb29
commit d4e97d77f7
1 file changed
+38
+38
View File
@@ -0,0 +1,38 @@
package com.hong.www;
import java.util.*;
public class A2050 {
public int minimumTime(int n, int[][] relations, int[] time) {
HashMap<Integer, Integer> map = new HashMap<>();
HashMap<Integer, List<Integer>> map2 = new HashMap<>();
PriorityQueue<int[]> temp = new PriorityQueue<>((a, b) -> a[1] - b[1]);
int s=0;
for (int[] x : relations) {
map.put(x[1], map.getOrDefault(x[1], 0) + 1);
map2.computeIfAbsent(x[0], k -> new ArrayList<>()).add(x[1]);
}
for (int i = 0; i < n; i++) {
if (!map.containsKey(i + 1)) {
temp.add(new int[] { i+1, time[i] });
}
}
while (!temp.isEmpty()) {
int[] now = temp.poll();
if(now[1]>s){
s=now[1];
}
for (int x : map2.getOrDefault(now[0], new ArrayList<>())) {
if (map.containsKey(x)) {
map.put(x, map.get(x) - 1);
if (map.get(x) == 0) {
map.remove(x);
temp.offer(new int[] { x, now[1] + time[x-1]});
}
}
}
}
return s;
}
}