Skip to content

Commit 81a0b8f

Browse files
authored
[20251206] BOJ / G2 / 환승 / 권혁준
1 parent b4f51aa commit 81a0b8f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
const int INF = 1e9 + 7;
6+
7+
int N, K, M;
8+
vector<vector<int>> v(101001);
9+
10+
int bfs01(int start, int end) {
11+
vector<int> dist(101001, INF);
12+
deque<pair<int, int>> q;
13+
dist[start] = 1;
14+
q.emplace_back(1, start);
15+
while (!q.empty()) {
16+
auto [d, n] = q.front(); q.pop_front();
17+
for (int i : v[n]) {
18+
int cost = i > N ? 0 : 1;
19+
if (dist[i] > d + cost) {
20+
dist[i] = d + cost;
21+
if (cost) q.emplace_back(dist[i], i);
22+
else q.emplace_front(dist[i], i);
23+
}
24+
}
25+
}
26+
return dist[end] == INF ? -1 : dist[end];
27+
}
28+
29+
int main() {
30+
cin.tie(0)->sync_with_stdio(0);
31+
32+
cin >> N >> K >> M;
33+
for (int i = 1; i <= M; i++) {
34+
for (int j = 1, a; j <= K; j++) {
35+
cin >> a;
36+
v[a].push_back(N + i);
37+
v[N + i].push_back(a);
38+
}
39+
}
40+
41+
cout << bfs01(1, N);
42+
43+
}
44+
```

0 commit comments

Comments
 (0)