Skip to content

Commit 81a659d

Browse files
authored
Merge pull request #341 from AlgorithmWithGod/suyeun84
[20250611] BOJ / G3 / ACM Craft / 김수연
2 parents 5d8104c + 6959e78 commit 81a659d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class boj1005 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
public static void main(String[] args) throws Exception {
12+
nextLine();
13+
int T = nextInt();
14+
for (int tc = 0; tc < T; tc++) {
15+
nextLine();
16+
int N = nextInt(); // 건물 개수 (1~N)
17+
int K = nextInt(); // 건설순서 규칙의 개수
18+
int[] time = new int[N+1]; // 건물당 걸리는 시간
19+
int[] answer = new int[N+1];
20+
int[] degree = new int[N+1];
21+
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
22+
for (int i = 0; i <= N; i++) graph.add(new ArrayList<Integer>());
23+
nextLine();
24+
for (int i = 1; i <= N; i++) time[i] = nextInt();
25+
for (int i = 0; i < K; i++) {
26+
nextLine();
27+
int a = nextInt();
28+
int b = nextInt();
29+
graph.get(a).add(b); // 건물 a -> b
30+
degree[b]++;
31+
}
32+
nextLine();
33+
int W = nextInt(); // 승리하기 위해 건설해야 할 건물 번호
34+
35+
Queue<Integer> q = new LinkedList<>();
36+
for (int i = 1; i <= N; i++) {
37+
if (degree[i] == 0) {
38+
q.add(i);
39+
answer[i] = time[i];
40+
}
41+
}
42+
while (!q.isEmpty()) {
43+
int cur = q.poll();
44+
for (int next : graph.get(cur)) {
45+
answer[next] = Math.max(answer[next], answer[cur] + time[next]);
46+
degree[next]--;
47+
if (degree[next] == 0) q.add(next);
48+
}
49+
}
50+
System.out.println(answer[W]);
51+
}
52+
}
53+
}
54+
```

0 commit comments

Comments
 (0)