-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210217_BOJ_18352.cpp
More file actions
63 lines (47 loc) · 956 Bytes
/
Copy path210217_BOJ_18352.cpp
File metadata and controls
63 lines (47 loc) · 956 Bytes
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 987654321;
int N, M, K, X;
vector<int> v[300001];
vector<int> D(300001, INF);
void dijkstra() {
D[X] = 0;
queue<pair<int, int>> pq;
pq.push({ D[X], X });
while (!pq.empty()) {
int weight = pq.front().first;
int n1 = pq.front().second;
pq.pop();
for (int i = 0; i < v[n1].size(); ++i) {
int sum = weight + 1;
int n2 = v[n1][i];
if (D[n2] > sum) {
D[n2] = sum;
pq.push({sum, n2});
}
}
}
vector<int> result;
for (int i = 1; i <= N; ++i) {
if (D[i] == K)
result.push_back(i);
}
if (result.empty())
cout << "-1\n";
else {
for (int i = 0; i < result.size(); ++i)
cout << result[i] << "\n";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> N >> M >> K >> X;
for (int i = 0; i < M; ++i) {
int a, b; cin >> a >> b;
v[a].push_back(b);
}
dijkstra();
return 0;
}