-
[BOJ] 1389 케빈 베이컨의 6단계 법칙ALGORITHM/BOJ 2023. 5. 21. 17:48
https://www.acmicpc.net/problem/1389
2023-05-21
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import java.util.Arrays;import java.util.Scanner;public class Main1389 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int[][] dist = new int[n+1][n+1];for(int[] p: dist) Arrays.fill(p, 987654321);for(int i = 1; i <= m; i++) {int a = sc.nextInt();int b = sc.nextInt();dist[a][b] = 1;dist[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(i != j && dist[i][k] + dist[k][j] < dist[i][j]) {dist[i][j] = dist[i][k] + dist[k][j];}}}}int min = 987654321;int idx = 0;for(int i = 1; i <= n; i++) {int tmp = 0;for(int j = 1; j <= n; j++) {if(i != j && dist[i][j] != 987654321) {tmp += dist[i][j];}}if(min > tmp) {min = tmp;idx = i;}}System.out.println(idx);}}cs #문제풀이
플로이드 와샬
'ALGORITHM > BOJ' 카테고리의 다른 글
[BOJ] 14940 쉬운 최단거리 (0) 2023.06.06 [BOJ] 16948 데스 나이트 (1) 2023.06.04 [백준] 2660 회장뽑기 (0) 2022.08.15 [백준] 14226 이모티콘 (0) 2022.08.15 [백준] 17298 오큰수 (0) 2022.07.25