|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 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 int N,K; |
| 10 | + static int[] duration; |
| 11 | + static List<Integer>[] adjList; |
| 12 | + static int[] degree; |
| 13 | + static int[] minTime; |
| 14 | + static int goal; |
| 15 | + |
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + int T = Integer.parseInt(br.readLine()); |
| 18 | + for (int t = 1; t <= T; t++) { |
| 19 | + st = new StringTokenizer(br.readLine()); |
| 20 | + N = Integer.parseInt(st.nextToken()); |
| 21 | + K = Integer.parseInt(st.nextToken()); |
| 22 | + duration = new int[N+1]; |
| 23 | + adjList = new List[N+1]; |
| 24 | + degree = new int[N+1]; |
| 25 | + minTime = new int[N+1]; |
| 26 | + |
| 27 | + st = new StringTokenizer(br.readLine()); |
| 28 | + for (int i = 1; i <= N; i++) { |
| 29 | + duration[i] = Integer.parseInt(st.nextToken()); |
| 30 | + minTime[i] = duration[i]; |
| 31 | + } |
| 32 | + for (int i = 1; i <= N; i++) { |
| 33 | + adjList[i] = new ArrayList<>(); |
| 34 | + } |
| 35 | + for (int i = 0; i < K; i++) { |
| 36 | + st = new StringTokenizer(br.readLine()); |
| 37 | + int first = Integer.parseInt(st.nextToken()); |
| 38 | + int second = Integer.parseInt(st.nextToken()); |
| 39 | + adjList[first].add(second); |
| 40 | + degree[second]++; |
| 41 | + } |
| 42 | + goal = Integer.parseInt(br.readLine()); |
| 43 | + |
| 44 | + go(); |
| 45 | + } |
| 46 | + |
| 47 | + bw.close(); |
| 48 | + } |
| 49 | + public static void go() throws IOException { |
| 50 | + ArrayDeque<Integer> q = new ArrayDeque<>(); |
| 51 | + for (int i = 1; i <= N; i++) { |
| 52 | + if(degree[i] == 0){ |
| 53 | + q.add(i); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + while(!q.isEmpty()) { |
| 58 | + int cur = q.poll(); |
| 59 | + for (int next : adjList[cur]) { |
| 60 | + minTime[next] = Math.max(minTime[next], minTime[cur]+duration[next]); |
| 61 | + |
| 62 | + degree[next]--; |
| 63 | + if(degree[next] == 0){ |
| 64 | + q.add(next); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + bw.write(minTime[goal]+"\n"); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
0 commit comments