-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_118669.java
More file actions
109 lines (85 loc) ยท 2.6 KB
/
DH_118669.java
File metadata and controls
109 lines (85 loc) ยท 2.6 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.*;
/*
* ๋ฑ์ฐ์ฝ์ค ์ ํ๊ธฐ
*/
public class DH_118669 {
static class Node implements Comparable<Node> {
int e, w, intensity;
public Node(int e, int w) {
this.e = e;
this.w = w;
}
public Node(int e, int w, int intensity) {
this.e = e;
this.w = w;
this.intensity = intensity;
}
@Override
public int compareTo(Node o) {
return Integer.compare(this.w, o.w);
}
}
static ArrayList<Node> adj[];
static final int INF = Integer.MAX_VALUE;
static int[] solution(int n, int[][] paths, int[] gates, int[] summits) {
int[] answer = {INF, INF};
adj = new ArrayList[n + 1];
for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Node>();
// ๊ฐ์ ๋ฆฌ์คํธ ๋ง๋ค์ด์ฃผ๊ธฐ
for(int[] path: paths) {
int i = path[0];
int j = path[1];
int w = path[2];
adj[i].add(new Node(j, w));
adj[j].add(new Node(i, w));
}
// ์ฐ๋ด์ฐ๋ฆฌ๋ค์ ์ ์ฅํ๋ set. ๊ฒฐ๊ณผ ๊ตฌํ ๋, ์ถ์
๊ตฌ์ ํฌ๊ธฐ๋๋ก ๊ณ์ฐํด์ผ ๋๋ฏ๋ก treeset ์ด์ฉ
TreeSet<Integer> summitSet = new TreeSet<Integer>();
// ์์์
๊ตฌ๋ฅผ ์ ์ฅํ๋ set
HashSet<Integer> gateSet = new HashSet<Integer>();
for(int s: summits) summitSet.add(s);
for(int g: gates) gateSet.add(g);
// ์ฐ๋ด์ฐ๋ฆฌ๋ค์ ๊ธฐ์ค์ผ๋ก bfs ์์ (์ถ์
๊ตฌ๋ฅผ ๋ง๋ ๋๊น์ง ํ์)
for(int s: summitSet) bfs(n, s, gateSet, summitSet, answer);
return answer;
}
static void bfs(int n, int s, HashSet<Integer> gateSet, TreeSet<Integer> summitSet, int[] answer) {
boolean[] v = new boolean[n + 1];
PriorityQueue<Node> q = new PriorityQueue<Node>();
q.add(new Node(s, 0, 0));
v[s] = true;
int intensity = INF;
while(!q.isEmpty()) {
Node current = q.poll();
v[current.e] = true;
// ์ถ์
๊ตฌ๋ฅผ ๋ง๋ฌ๋๋ฐ, intensity๊ฐ ์๋ค๋ฉด intensity ๊ฐ update ํด์ฃผ๊ธฐ
if(gateSet.contains(current.e) && intensity > current.intensity) {
intensity = current.intensity;
break;
}
// ๋ค์ ๋
ธ๋๊ฐ ์ถ์
๊ตฌ๊ฐ ๋๋ฉด ์๋จ
for(Node next: adj[current.e]) {
if(summitSet.contains(next.e) || v[next.e]) continue;
q.add(new Node(next.e, next.w, Math.max(current.intensity, next.w)));
}
}
if(answer[1] > intensity) {
answer[1] = intensity;
answer[0] = s;
}
}
public static void main(String[] args) {
int n = 6;
int[][] paths = {{1, 2, 3},
{2, 3, 5},
{2, 4, 2},
{2, 5, 4},
{3, 4, 4},
{4, 5, 3},
{4, 6, 1},
{5, 6, 1}};
int[] gates = {1, 3};
int[] summits = {5};
System.out.println(Arrays.toString(solution(n, paths, gates, summits)));
}
}