Skip to content

Commit bd64892

Browse files
committed
BOJ_1389 : 케빈 베이컨의 6단계 법칙(플로이드)
1 parent d64b4ad commit bd64892

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int M = Integer.parseInt(st.nextToken());
11+
int[][] graph = new int[N+1][N+1];
12+
13+
for(int i=1; i<=N; i++) {
14+
for(int j=1; j<=N; j++) {
15+
if(i == j) {
16+
graph[i][j] = 0;
17+
}else {
18+
graph[i][j] = 1000;
19+
}
20+
}
21+
}
22+
23+
for(int i=0; i<M; i++) {
24+
st = new StringTokenizer(br.readLine());
25+
int A = Integer.parseInt(st.nextToken());
26+
int B = Integer.parseInt(st.nextToken());
27+
graph[A][B] = graph[B][A] = 1;
28+
}
29+
30+
for(int k=1; k<=N; k++) {
31+
for(int i=1; i<=N; i++) {
32+
for(int j=1; j<=N; j++) {
33+
graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
34+
}
35+
}
36+
}
37+
38+
int[] count = new int[N+1];
39+
for(int i=1; i<=N; i++) {
40+
for(int j=1; j<=N; j++) {
41+
count[i] += graph[i][j];
42+
}
43+
}
44+
int answer = 1;
45+
for(int i=1; i<=N; i++) {
46+
if(count[answer] > count[i]) {
47+
answer = i;
48+
}
49+
}
50+
System.out.println(answer);
51+
}
52+
}

0 commit comments

Comments
 (0)