package com.hong.www; import java.util.*; public class A2050 { public int minimumTime(int n, int[][] relations, int[] time) { HashMap map = new HashMap<>(); HashMap> map2 = new HashMap<>(); PriorityQueue 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; } }