https://www.acmicpc.net/problem/18352
18352번: 특정 거리의 도시 찾기
첫째 줄에 도시의 개수 N, 도로의 개수 M, 거리 정보 K, 출발 도시의 번호 X가 주어진다. (2 ≤ N ≤ 300,000, 1 ≤ M ≤ 1,000,000, 1 ≤ K ≤ 300,000, 1 ≤ X ≤ N) 둘째 줄부터 M개의 줄에 걸쳐서 두 개
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x;
int d[300003];
vector<int> v[300003];
// basic dijkstra form
void dijk(int s) {
d[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = 0; i < v[cur].size(); i++) {
int next = v[cur][i];
if (d[next] > d[cur] + 1) {
d[next] = d[cur] + 1;
q.push(next);
}
}
}
}
int main()
{
int i, from, to;
cin >> n >> m >> k >> x;
for (i = 1; i <= n; i++) {
d[i] = 1e9;
}
for (i = 1; i <= m; i++) {
cin >> from >> to;
v[from].push_back(to);
}
dijk(x);
bool check = false;
for (i = 1; i <= n; i++) {
if (d[i] == k) {
check = true;
cout << i << '\n';
}
}
if (check == false) cout << "-1";
return 0;
}

'Algorithm Study > 백준' 카테고리의 다른 글
| [C++] LIS & DP / 백준 11053번 - 가장 긴 증가하는 부분 수열 (0) | 2023.06.15 |
|---|---|
| [C++] Dijkstra algorithm / 백준 1504번 - 특정한 최단 경로 (0) | 2023.06.14 |
| [C++] Dijkstra algorithm - 백준 1238번 파티 (1) | 2023.06.14 |
| [C++] indexed tree / 백준 2042번 (2) | 2023.06.08 |