Skip to content

Commit fa6dc79

Browse files
authored
Merge pull request #1610 from AlgorithmWithGod/LiiNi-coder
[20251207] BOJ / G5 / 호석이 두마리 치킨 / 이인희
2 parents e8842c6 + 5a47713 commit fa6dc79

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static final int INF = 100_000_000;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
int N = Integer.parseInt(st.nextToken());
12+
int M = Integer.parseInt(st.nextToken());
13+
int[][] dist = new int[N + 1][N + 1];
14+
for(int i = 1; i <= N; i++){
15+
for(int j = 1; j <= N; j++){
16+
if(i == j) dist[i][j] = 0;
17+
else dist[i][j] = INF;
18+
}
19+
}
20+
for(int i = 0; i < M; i++){
21+
st = new StringTokenizer(br.readLine());
22+
int a = Integer.parseInt(st.nextToken());
23+
int b = Integer.parseInt(st.nextToken());
24+
dist[a][b] = 1;
25+
dist[b][a] = 1;
26+
}
27+
//플로이드워셜
28+
for(int k = 1; k <= N; k++){
29+
for(int i = 1; i <= N; i++){
30+
for(int j = 1; j <= N; j++){
31+
if(dist[i][j] > dist[i][k] + dist[k][j]){
32+
dist[i][j] = dist[i][k] + dist[k][j];
33+
}
34+
}
35+
}
36+
}
37+
int bestI = 0;
38+
int bestJ = 0;
39+
int bestSum = INF;
40+
for(int i = 1; i <= N; i++){
41+
for(int j = i + 1; j <= N; j++){
42+
int sum = 0;
43+
for(int k = 1; k <= N; k++){
44+
int d = Math.min(dist[k][i], dist[k][j]);
45+
sum += d*2;
46+
}
47+
if(sum < bestSum){
48+
bestSum = sum;
49+
bestI = i;
50+
bestJ = j;
51+
}
52+
}
53+
}
54+
System.out.println(bestI + " " + bestJ + " " + bestSum);
55+
}
56+
}
57+
58+
```

0 commit comments

Comments
 (0)