1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| public class boj_1389 { static int INF = (int)1e9; static int[][] map; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); map = new int[n+1][n+1]; for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ map[i][j] = INF; if(i == j) map[i][j] = 0; } } for(int i=0; i<m; i++){ st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); map[x][y] = 1; map[y][x] = 1; } for(int k=1; k<=n; k++){ for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ if(map[i][k] + map[k][j] < map[i][j]){ map[i][j] = map[i][k] + map[k][j]; } } } } int answer = INF; int idx = -1; for(int i=1; i<=n; i++){ int sum = 0; for(int j=1; j<=n; j++){ sum += map[i][j]; } if(answer > sum){ answer = sum; idx = i; } } System.out.println(idx); } }
|