|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main{ |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + static StringTokenizer st; |
| 9 | + static StringBuilder sb = new StringBuilder(); |
| 10 | + static int N; |
| 11 | + static List<Integer>[] prevTask; |
| 12 | + static int[] prevCnt; |
| 13 | + static int[] cost; |
| 14 | + static int ans = 0; |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + N = Integer.parseInt(br.readLine()); |
| 18 | + prevTask = new List[N+1]; |
| 19 | + for (int i = 1; i <= N; i++) { |
| 20 | + prevTask[i] = new ArrayList<>(); |
| 21 | + } |
| 22 | + prevCnt = new int[N+1]; |
| 23 | + cost = new int[N+1]; |
| 24 | + |
| 25 | + for (int i = 1; i <= N; i++) { |
| 26 | + st = new StringTokenizer(br.readLine()); |
| 27 | + cost[i] = Integer.parseInt(st.nextToken()); |
| 28 | + int tmp = Integer.parseInt(st.nextToken()); |
| 29 | + for (int j = 0; j < tmp; j++) { |
| 30 | + prevTask[Integer.parseInt(st.nextToken())].add(i); |
| 31 | + prevCnt[i]++; |
| 32 | + } |
| 33 | + } |
| 34 | + solve(); |
| 35 | + bw.write(ans+""); |
| 36 | + bw.close(); |
| 37 | + } |
| 38 | + public static void solve(){ |
| 39 | + PriorityQueue<int[]> q = new PriorityQueue<>((a,b) -> Integer.compare(a[1],b[1])); |
| 40 | + boolean[] visited = new boolean[N+1]; |
| 41 | + for (int i = 1; i <= N; i++) { |
| 42 | + if(prevCnt[i] == 0){ |
| 43 | + q.offer(new int[]{i,cost[i]}); |
| 44 | + visited[i] = true; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + while(!q.isEmpty()){ |
| 49 | + int[] cur = q.poll(); |
| 50 | + ans = Math.max(ans,cur[1]); |
| 51 | + |
| 52 | + for (int next : prevTask[cur[0]]) { |
| 53 | + if(!visited[next]){ |
| 54 | + prevCnt[next]--; |
| 55 | + if(prevCnt[next] == 0){ |
| 56 | + q.offer(new int[]{next,cur[1]+cost[next]}); |
| 57 | + visited[next] = true; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
0 commit comments