File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments