File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments