문제) 백준 - BFS - Small World Network
https://www.acmicpc.net/problem/18243
18243번: Small World Network
첫 번째 줄에 지구에 있는 사람의 수 N과 친구 관계의 개수 K가 주어진다. 모든 사람은 1부터 N까지 번호가 매겨져 있다. (1 ≤ N ≤ 100, 0 ≤ K ≤ N×(N-1)/2) 두 번째 줄부터 K+1번째 줄까지 친구
www.acmicpc.net
1부터 N까지 시작하여 각 사람마다 거리를 구하여 6번 안에 이어져있는지 확인합니다. 가중치가 없는 간선으로 이어져 있으므로 BFS를 활용합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool finished(){ | |
for(int i = 1; i <= N; ++i) | |
if(dist[i] == -1 || dist[i] >= 7) | |
return false; | |
return true; | |
} | |
void bfs(int src){ | |
queue<int> q; | |
q.push(src); | |
dist[src] = 0; | |
while(!q.empty()){ | |
int now = q.front(); | |
q.pop(); | |
for(int next: graph[now]) | |
if(dist[next]== -1){ | |
q.push(next); | |
dist[next] = dist[now] + 1; | |
} | |
} | |
} |
Full Code)
https://github.com/Chocochip101/BOJ_Solution/blob/main/Solution/18243_SmallWorldNetwork.cpp
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 15486번 - 퇴사 2 (C++) 문제 및 풀이 (0) | 2022.02.28 |
---|---|
[백준] 13418번 - 학교 탐방하기 (C++) 문제 및 풀이 (0) | 2022.02.27 |
[백준] 4779번 - 칸토어 집합 (C++) 문제 및 풀이 (0) | 2022.02.27 |
[백준] 14400번 - 편의점 2 (C++) 문제 및 풀이 (0) | 2022.02.26 |
[백준] 10988번 - 팰린드롬인지 확인하기 (Python) 문제 및 풀이 (0) | 2022.02.25 |
댓글