Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 코테
- Operating System
- 구현
- 그래프
- coding
- 컴퓨터공학과
- 정석
- 스택
- cs
- 오에스
- 북리뷰
- 개발
- OS
- 브루트포스
- Stack
- 컴공
- DP
- bfs
- 오퍼레이팅시스템
- c++
- 백준
- 자료구조
- 알고리즘
- 너비우선탐색
- 문제풀이
- Computer science
- 컴공과
- 정석학술정보관
- vector
- 코딩
Archives
- Today
- Total
Little Jay
[C++] 백준 1389번 - 케빈 베이컨의 6단계 법칙 본문
기본적인 Floyd Warshall문제
사실 친구의 거리를 구하는것이 1이라서 BFS로도 풀 수 있는 문제인데 플로이드와샬을 좀 더 연습해보기 위해 플로이드와샬로 풀었다. 주의할 점은 거리(?)가 같을 때 작은 것의 번호를 출력해야한다는 것이다.
#include <bits/stdc++.h>
#define endl '\n'
#define INF 987654321
using namespace std;
int n, m;
int arr[101][101];
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) arr[i][j] = 0;
else arr[i][j] = INF;
}
}
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
arr[a][b] = 1; 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++) {
int temp = arr[i][k] + arr[k][j];
if (temp < arr[i][j]) arr[i][j] = temp;
}
}
}
vector<int> v;
int ans = 9999999;
int result = 0;
for (int i = 1; i <= n; i++) {
int temp = 0;
for (int j = 1; j <= n; j++) {
if (i == j) continue;
if (arr[i][j] == INF) continue;
temp += arr[i][j];
}
if (ans > temp) {
ans = temp;
result = i;
}
}
cout << result << endl;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 1541번 - 잃어버린 괄호 (0) | 2022.07.04 |
---|---|
[C++] 백준 9375번 - 패션왕 신해빈 (0) | 2022.07.02 |
[C++] 백준 1916번 - 최소비용 구하기 (0) | 2022.07.02 |
[C++] 백준 3613번 - Java vs C++ (0) | 2022.07.01 |
[C++] 백준 11049번 - 행렬 곱셈 순서 (0) | 2022.07.01 |
Comments