-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ1005.java
More file actions
98 lines (80 loc) · 2.92 KB
/
BOJ1005.java
File metadata and controls
98 lines (80 loc) · 2.92 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class BOJ1005 {
/**
* 1. 입력값 세팅
* 2. 위상 정렬 이용
* 2-1. 각 노드는 이전 계산값에 현재 노드의 건설 시간을 더해주어야 함
* 2-2. 특정 노드를 계산하기 위해서는 입력으로 들어오는 노드 중 최대값을 선택하여 현재 노드의 건설 시간을 더해주어야 함
* 2-3. 즉, 특정 노드가 계산이 되려면 입력으로 들어오는 노드가 모두 계산이 되어있어야 함 -> 위상 정렬 이용
*
* 3. w에 해당하는 노드의 총 건설시간인 answer 출력
*/
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int n, k, w;
static int[] DTime, sumTime, inDegree;
static List<Integer>[] graph;
private static void input() throws IOException {
StringTokenizer st;
// set n, k
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
// set DTime, sumTime, inDegree
st = new StringTokenizer(br.readLine());
inDegree = new int[n + 1];
sumTime = new int[n + 1];
DTime = new int[n + 1];
for (int i = 1; i <= n; i++) {
DTime[i] = Integer.parseInt(st.nextToken());
}
// set graph
graph = new List[n + 1];
for (int i = 1; i <= n; i++)
graph[i] = new ArrayList<>();
for (int i = 0; i < k; i++) {
st = new StringTokenizer(br.readLine());
int parent = Integer.parseInt(st.nextToken());
int child = Integer.parseInt(st.nextToken());
graph[parent].add(child);
inDegree[child]++;
}
// set w
w = Integer.parseInt(br.readLine());
}
public static void main(String[] args) throws IOException {
int testCase = Integer.parseInt(br.readLine());
for (int i = 0; i < testCase; i++) {
input();
process();
}
}
private static void process() {
// set sumTime
setSumTime();
// print answer
System.out.println(sumTime[w]);
}
private static void setSumTime() {
Queue<Integer> queue = new LinkedList<>();
// add init value (no inDegree)
for(int i = 1; i <= n; i++){
if(inDegree[i] == 0){
queue.add(i);
sumTime[i] = DTime[i];
}
}
while(!queue.isEmpty()){
int thisVertex = queue.poll();
for(int i : graph[thisVertex]){
inDegree[i]--;
sumTime[i] = Math.max(sumTime[i], sumTime[thisVertex] + DTime[i]);
if(inDegree[i] == 0){
queue.add(i);
}
}
}
}
}