#include <stdio.h> #include <iostream> #include <set> #include <queue> #include <vector> #include <string> #include <algorithm> using namespace std; int map[110][110]; int peopleNum; int relation; int Min = 9999999; int ans; queue<int> q; int visited[110]; void init() { for(int i = 1; i <= peopleNum; i++) { visited[i] = 0; } } void BFS(int start) { visited[start] = 1; q.push(start); while(!q.empty()) { start = q.front(); q.pop(); for(int i = 1; i <= peopleNum; i++) { if(map[start][i] == 1 && visited[i] == 0) { visited[i] = visited[start] + 1; q.push(i); } } } } int main(void) { // freopen("B1389_input.txt", "r", stdin); scanf("%d %d", &peopleNum, &relation); for(int i = 1; i <= relation; i++) { int a, b; scanf("%d %d", &a, &b); map[a][b] = 1; map[b][a] = 1; } for(int i = 1; i <= peopleNum; i++) { init(); BFS(i); int sum = 0; for(int j = 1; j <= peopleNum; j++) { if(i == j) { continue; } else { sum += (visited[j]-1); } } if(sum < Min) { Min = sum; ans = i; } } cout << ans; return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 16234] 인구이동 (BFS) (C/C++) (★★) (0) | 2019.12.07 |
---|---|
[백준 2573] 빙산 (BFS) (C/C++) (★) (0) | 2019.12.06 |
[백준 1261] 알고스팟 (BFS) (C/C++) (★) (0) | 2019.12.06 |
[백준 14395] 4연산 (BFS) (C/C++) (0) | 2019.12.06 |
[백준 7576] 토마토 (BFS) (C/C++) (0) | 2019.12.06 |