#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int N = 100'005;
vector<int> adj[N];
int dist[N][150];
vector<int> a[150];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int l;
cin >> l;
a[i].resize(l);
for (int j = 0; j < l; j++) {
cin >> a[i][j];
}
}
memset(dist, '?', sizeof dist);
priority_queue<array<int, 3>> q;
q.push({1, 0, 0});
dist[1][0] = 0;
while (q.size()) {
int node = q.top()[1];
int cur = q.top()[2];
int cost = -q.top()[0];
q.pop();
int newcur = (cur + 1) % a[0].size();
if (a[0][newcur] != node && cost + 1 < dist[node][newcur]) {
dist[node][newcur] = cost + 1;
q.push({-(cost + 1), node, newcur});
}
for (auto nxt : adj[node]) {
if (nxt == a[0][cur] && node == a[0][newcur]) continue;
if (node == a[0][newcur]) continue;
if (cost + 1 < dist[nxt][newcur]) {
dist[nxt][newcur] = cost + 1;
q.push({-(cost + 1), nxt, newcur});
}
}
}
int mn = 1e9;
for (int i = 0; i < a[0].size(); i++) {
mn = min(mn, dist[n][i]);
}
if (mn == int(1e9)) {
cout << "impossible\n";
} else {
cout << mn << '\n';
}
return 0;
}
Compilation message
watchmen.cpp: In function 'int main()':
watchmen.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < a[0].size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
62300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
62300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
62300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
62300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
62300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
62300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
62300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |