Skip to content

Commit 5713f6c

Browse files
authored
[20250203] BOJ / 골드4 / 가장 먼 곳 / 권혁준
1 parent ceeb5aa commit 5713f6c

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Node{
7+
int d, n;
8+
Node(int d, int n){
9+
this.d = d;
10+
this.n = n;
11+
}
12+
}
13+
14+
class Main {
15+
16+
// IO field
17+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
19+
static StringTokenizer st;
20+
21+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
22+
static int nextInt() {return Integer.parseInt(st.nextToken());}
23+
static long nextLong() {return Long.parseLong(st.nextToken());}
24+
static void bwEnd() throws Exception {bw.flush();bw.close();}
25+
26+
// Additional field
27+
static int N;
28+
static int INF = 1234567890;
29+
static ArrayList<int[]>[] V;
30+
31+
static int[] dijk(int s) {
32+
int[] D = new int[N+1];
33+
Arrays.fill(D, INF);
34+
D[s] = 0;
35+
PriorityQueue<Node> PQ = new PriorityQueue<>((a,b) -> a.d - b.d);
36+
PQ.offer(new Node(0,s));
37+
while(!PQ.isEmpty()) {
38+
Node now = PQ.poll();
39+
if(now.d > D[now.n]) continue;
40+
for(int[] next : V[now.n]) {
41+
if(D[next[0]] > now.d + next[1]) {
42+
D[next[0]] = now.d + next[1];
43+
PQ.offer(new Node(now.d+next[1],next[0]));
44+
}
45+
}
46+
}
47+
return D;
48+
}
49+
50+
public static void main(String[] args) throws Exception {
51+
52+
nextLine();
53+
N = nextInt();
54+
V = new ArrayList[N+1];
55+
for(int i=1;i<=N;i++) V[i] = new ArrayList<>();
56+
nextLine();
57+
int A = nextInt(), B = nextInt(), C = nextInt();
58+
nextLine();
59+
int M = nextInt();
60+
for(int i=0;i<M;i++) {
61+
nextLine();
62+
int a = nextInt(), b = nextInt(), c = nextInt();
63+
V[a].add(new int[] {b,c});
64+
V[b].add(new int[] {a,c});
65+
}
66+
int[] DA = dijk(A);
67+
int[] DB = dijk(B);
68+
int[] DC = dijk(C);
69+
70+
int ans = 0, res = 0;
71+
for(int i=1;i<=N;i++) {
72+
int min = Math.min(DA[i], Math.min(DB[i], DC[i]));
73+
if(min > ans) {
74+
ans = min;
75+
res = i;
76+
}
77+
}
78+
bw.write(res+"\n");
79+
80+
bwEnd();
81+
}
82+
83+
}
84+
85+
```

0 commit comments

Comments
 (0)