-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1005.java
More file actions
65 lines (57 loc) · 2.2 KB
/
Copy pathBOJ_1005.java
File metadata and controls
65 lines (57 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// BOJ - 1005
// Problem Sheet - https://www.acmicpc.net/problem/1005
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
for(int i=0; i<t ; i++) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] buildTime = new int[n + 1];
int[] totalBuildTime = new int[n + 1];
int[] prevBuildCnt = new int[n + 1];
List<List<Integer>> al = new ArrayList<>(n);
for(int j=0 ; j<n ; j++) {
al.add(new LinkedList<>());
}
st = new StringTokenizer(br.readLine());
for(int j=1 ; j<=n ; j++) {
buildTime[j] = Integer.parseInt(st.nextToken());
}
for(int j=0 ; j<k ; j++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
al.get(x-1).add(y);
prevBuildCnt[y]++;
}
Queue<Integer> waiting = new LinkedList<>();
for(int j=1 ; j<=n ; j++) {
if(prevBuildCnt[j] == 0) {
waiting.add(j);
totalBuildTime[j] = buildTime[j];
}
}
while(!waiting.isEmpty()) {
int cur = waiting.poll();
for(int b : al.get(cur-1)) {
totalBuildTime[b] = Math.max(totalBuildTime[b], buildTime[b] + totalBuildTime[cur]);
prevBuildCnt[b]--;
if(prevBuildCnt[b] == 0) {
waiting.add(b);
}
}
}
int target = Integer.parseInt(br.readLine());
sb.append(totalBuildTime[target]).append("\n");
}
System.out.println(sb);
br.close();
System.exit(0);
}
}