728x90
반응형
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static final int INF = 987654321;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] arr = new int[N+1][N+1];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if(i==j) continue;
arr[i][j] = INF;
}
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine()," ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
arr[a][b] = arr[b][a] = 1;
}
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
// 단순히 갈 수 있는 경로가 있는지만 체크함.
if (arr[i][j] > arr[i][k] + arr[k][j]) {
arr[i][j] = arr[i][k] + arr[k][j];
}
}
}
}
int answer = 987654321;
int idx = -1;
for (int i = 1; i <= N; i++) {
int tot = 0;
for (int j = 1; j <= N; j++) {
tot += arr[i][j];
}
if(answer > tot){
answer = tot;
idx = i;
}
}
bw.write(idx + "\n");
bw.flush();
bw.close();
br.close();
}
}
728x90
반응형
'알고리즘 > 플로이드 워셜' 카테고리의 다른 글
[백준] 11403번 경로 찾기 #Java (0) | 2022.05.25 |
---|