Skip to content

Commit b934a40

Browse files
authored
[20251017] BOJ / G4 / 가장 먼 곳 / 이인희
1 parent 1fadff5 commit b934a40

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class Main {
6+
7+
static class Node implements Comparable<Node> {
8+
int to, weight;
9+
10+
Node(int to, int weight) {
11+
this.to = to;
12+
this.weight = weight;
13+
}
14+
15+
@Override
16+
public int compareTo(Node o){
17+
return this.weight - o.weight;
18+
}
19+
}
20+
21+
static int N, M;
22+
static List<Node>[] Graph;
23+
static int[] Friends = new int[3];
24+
static final int INF = Integer.MAX_VALUE;
25+
26+
public static void main(String[] args) throws IOException {
27+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
28+
StringTokenizer st;
29+
30+
N = Integer.parseInt(br.readLine());
31+
st = new StringTokenizer(br.readLine());
32+
for(int i = 0; i < 3; i++)
33+
Friends[i] = Integer.parseInt(st.nextToken());
34+
35+
M = Integer.parseInt(br.readLine());
36+
Graph = new ArrayList[N + 1];
37+
for(int i = 1; i <= N; i++)
38+
Graph[i] = new ArrayList<>();
39+
40+
for (int i = 0; i < M; i++) {
41+
st = new StringTokenizer(br.readLine());
42+
int a = Integer.parseInt(st.nextToken());
43+
int b = Integer.parseInt(st.nextToken());
44+
int c = Integer.parseInt(st.nextToken());
45+
Graph[a].add(new Node(b, c));
46+
Graph[b].add(new Node(a, c));
47+
}
48+
49+
int[][] dist = new int[3][N + 1];
50+
for(int i = 0; i < 3; i++){
51+
dist[i] = dijkstra(Friends[i]);
52+
}
53+
int answer = 0;
54+
int maxMinDist = -1;
55+
56+
for(int i = 1; i <= N; i++){
57+
int minDist = Math.min(dist[0][i], Math.min(dist[1][i], dist[2][i]));
58+
if (minDist > maxMinDist) {
59+
maxMinDist = minDist;
60+
answer = i;
61+
}
62+
}
63+
64+
65+
System.out.println(answer);
66+
br.close();
67+
}
68+
69+
private static int[] dijkstra(int start) {
70+
int[] dist = new int[N + 1];
71+
Arrays.fill(dist, INF);
72+
dist[start] = 0;
73+
74+
PriorityQueue<Node> pq = new PriorityQueue<>();
75+
pq.offer(new Node(start, 0));
76+
while(!pq.isEmpty()){
77+
Node cur = pq.poll();
78+
if(cur.weight > dist[cur.to])
79+
continue;
80+
81+
for(Node next : Graph[cur.to]){
82+
int cost = cur.weight + next.weight;
83+
if(cost < dist[next.to]){
84+
dist[next.to] = cost;
85+
pq.offer(new Node(next.to, cost));
86+
}
87+
}
88+
}
89+
return dist;
90+
}
91+
}
92+
93+
```

0 commit comments

Comments
 (0)