1
+ import java .util .*;
2
+
3
+ class Solution {
4
+
5
+ static int N ;
6
+ static List <Node >[] g ;
7
+ static Set <Integer > gSet ;
8
+ static Set <Integer > sSet ;
9
+ static PriorityQueue <int []> pq ;
10
+ static class Node {
11
+ int edge , cost ;
12
+ public Node (int edge , int cost ) {
13
+ this .edge = edge ;
14
+ this .cost = cost ;
15
+ }
16
+ }
17
+
18
+ public int [] solution (int n , int [][] paths , int [] gates , int [] summits ) {
19
+ N = n ;
20
+ g = new ArrayList [N +1 ];
21
+ for (int i =1 ; i <N +1 ; i ++) {
22
+ g [i ] = new ArrayList <>();
23
+ }
24
+
25
+ for (int i =0 ; i <paths .length ; i ++) {
26
+ int [] p = paths [i ];
27
+ g [p [0 ]].add (new Node (p [1 ], p [2 ]));
28
+ g [p [1 ]].add (new Node (p [0 ], p [2 ]));
29
+ }
30
+
31
+ gSet = new HashSet <>();
32
+ for (int gate : gates ) {
33
+ gSet .add (gate );
34
+ }
35
+ sSet = new HashSet <>();
36
+ for (int summit : summits ) {
37
+ sSet .add (summit );
38
+ }
39
+
40
+ pq = new PriorityQueue <>((o1 , o2 ) -> o1 [1 ]==o2 [1 ] ? o1 [0 ]-o2 [0 ] :o1 [1 ]-o2 [1 ]);
41
+
42
+ // 산봉우리를 시작점으로 bfs
43
+ for (int summit : summits ) {
44
+ bfs (summit );
45
+ }
46
+
47
+ int [] answer = pq .poll ();
48
+
49
+ return answer ;
50
+ }
51
+ public static void bfs (int start ) {
52
+ // 큐를 우선순위 큐로 설정하여, 비용이 작은 순부터 우선 탐색하도록 함
53
+ PriorityQueue <int []> q = new PriorityQueue <>((o1 , o2 )-> o1 [0 ]-o2 [0 ]);
54
+ boolean [] visited = new boolean [N +1 ];
55
+
56
+ // {비용, 노드}
57
+ q .add (new int [] {0 , start });
58
+ visited [start ] = true ;
59
+
60
+ int intensity = Integer .MAX_VALUE ;
61
+ while (!q .isEmpty ()) {
62
+ int [] now = q .poll ();
63
+
64
+ // 출입구를 만남
65
+ if (gSet .contains (now [1 ])) {
66
+ pq .add (new int [] {start , now [0 ]});
67
+ break ;
68
+ }
69
+
70
+ // 등산로 반복
71
+ for (Node next : g [now [1 ]]) {
72
+ if (sSet .contains (next .edge )) continue ;
73
+ if (visited [next .edge ]) continue ;
74
+
75
+ visited [now [1 ]] = true ;
76
+ int max = Math .max (now [0 ], next .cost );
77
+ q .add (new int [] {max , next .edge });
78
+ }
79
+ }
80
+ }
81
+ }
0 commit comments