-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSB_118669.java
More file actions
86 lines (76 loc) ยท 3.11 KB
/
SB_118669.java
File metadata and controls
86 lines (76 loc) ยท 3.11 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
public class SB_118669 {
static int[] info;
static int[] intensity;
static ArrayList<ArrayList<Node>> adj = new ArrayList<>();
private static void dijkstra() {
PriorityQueue<Node> pq = new PriorityQueue<>();
for (int i = 0; i < info.length; i++) {
if (info[i] == 1) { // ์
๊ตฌ์ผ๋
pq.offer(new Node(i, 0)); // ์ฐ์ ์์ํ์ ๋ฃ๊ณ
intensity[i] = 0; // ์ฌ๋๊ตฌ๊ฐ 0 ์ด๊ธฐํ
}
}
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (intensity[cur.u] < cur.cost) continue; // ์ด๋ฏธ ๋ ์์ ๊ฐ์ผ๋ก ๋ฐฉ๋ฌธํ์ผ๋ฉด ํจ์ฐ
for (Node nxt : adj.get(cur.u)) {
if (intensity[nxt.u] > Math.max(intensity[cur.u], nxt.cost)) { // ์ด๋๊ณผ์ ์ค ์ต๋๊ฐ์ด intensity๊ฐ์ด ๋จ
intensity[nxt.u] = Math.max(intensity[cur.u], nxt.cost);
pq.offer(nxt);
}
}
}
}
public static int[] solution(int n, int[][] paths, int[] gates, int[] summits) {
// ์ง์ ์ ๋ณด ์ธํ
info = new int[n + 1];
intensity = new int[n + 1];
for (int g : gates) {
info[g] = 1; // ์ถ์
๊ตฌ 1
}
for (int s : summits) {
info[s] = 2; // ์ฐ๋ด์ฐ๋ฆฌ 2
}
Arrays.fill(intensity, Integer.MAX_VALUE);
// adj๋ง๋ค๊ธฐ (์
๊ตฌ๋ ๋๊ฐ๋ ๋ฐฉํฅ, ๋ด์ฐ๋ฆฌ๋ ๋ค์ด์ค๋ ๋ฐฉํฅ, ์ผํฐ๋ ์๋ฐฉํฅ)
for (int i = 0; i < n + 1; i++) {
adj.add(new ArrayList<>());
}
for (int[] path : paths) {
int u = path[0];
int v = path[1];
int c = path[2];
if (info[u] == 1 || info[v] == 2) adj.get(u).add(new Node(v, c)); // ์์์ด ์ฐ๋ด์ฐ๋ฆฌ๊ฑฐ๋ ๋์ด ์
๊ตฌ๋ฉด ๊ฐ์ ๋ค์ด์ค๊ณ ๋๊ฐ๋๊ฑฐ ์ ์ฅ์ํด๋ ๋จ
else if (info[v] == 1 || info[u] == 2) adj.get(v).add(new Node(u, c)); // ์์์ด ์
๊ตฌ๊ฑฐ๋ ๋์ด ์ฐ๋ด์ฐ๋ฆฌ๋ฉด ๊ฐ์ ๋๊ฐ๊ฑฐ๋ ๋ค์ด์ค๋๊ฑฐ ์ ์ฅ์ํด๋ ๋จ
else { // ์ผํฐ๋ ์๋ฐฉํฅ์ผ๋ก ์ ์ฅ
adj.get(u).add(new Node(v, c));
adj.get(v).add(new Node(u, c));
}
}
dijkstra();
Arrays.sort(summits);
int mnP = 0;
int mnC = Integer.MAX_VALUE;
for (int s : summits) { // ๊ตฌํ ์ฌ๋ ๊ตฌ๊ฐ ์ค ๊ฐ์ ์งง์ ๊ฐ ๊ตฌํ๊ธฐ
if (intensity[s] < mnC) {
mnC = Math.min(mnC, intensity[s]);
mnP = s;
}
}
return new int[]{mnP, mnC};
}
static class Node implements Comparable<Node>{
int u, cost;
public Node(int u, int cost) {
this.u = u;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return this.cost-o.cost;
}
}
}