-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_118669.java
More file actions
81 lines (67 loc) Β· 2.27 KB
/
JY_118669.java
File metadata and controls
81 lines (67 loc) Β· 2.27 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
import java.util.*;
class Solution {
static int N;
static List<Node>[] g;
static Set<Integer> gSet;
static Set<Integer> sSet;
static PriorityQueue<int[]> pq;
static class Node {
int edge, cost;
public Node(int edge, int cost) {
this.edge = edge;
this.cost = cost;
}
}
public int[] solution(int n, int[][] paths, int[] gates, int[] summits) {
N = n;
g = new ArrayList[N+1];
for(int i=1; i<N+1; i++) {
g[i] = new ArrayList<>();
}
for(int i=0; i<paths.length; i++) {
int[] p = paths[i];
g[p[0]].add(new Node(p[1], p[2]));
g[p[1]].add(new Node(p[0], p[2]));
}
gSet = new HashSet<>();
for(int gate: gates) {
gSet.add(gate);
}
sSet = new HashSet<>();
for(int summit: summits) {
sSet.add(summit);
}
pq = new PriorityQueue<>((o1, o2) -> o1[1]==o2[1] ? o1[0]-o2[0] :o1[1]-o2[1]);
// μ°λ΄μ°λ¦¬λ₯Ό μμμ μΌλ‘ bfs
for(int summit: summits) {
bfs(summit);
}
int[] answer = pq.poll();
return answer;
}
public static void bfs(int start) {
// νλ₯Ό μ°μ μμ νλ‘ μ€μ νμ¬, λΉμ©μ΄ μμ μλΆν° μ°μ νμνλλ‘ ν¨
PriorityQueue<int[]> q = new PriorityQueue<>((o1, o2)-> o1[0]-o2[0]);
boolean[] visited = new boolean[N+1];
// {λΉμ©, λ
Έλ}
q.add(new int[] {0, start});
visited[start] = true;
int intensity = Integer.MAX_VALUE;
while(!q.isEmpty()) {
int[] now = q.poll();
// μΆμ
ꡬλ₯Ό λ§λ¨
if(gSet.contains(now[1])) {
pq.add(new int[] {start, now[0]});
break;
}
// λ±μ°λ‘ λ°λ³΅
for(Node next: g[now[1]]) {
if(sSet.contains(next.edge)) continue;
if(visited[next.edge]) continue;
visited[now[1]] = true;
int max = Math.max(now[0], next.cost);
q.add(new int[] {max, next.edge});
}
}
}
}