#include <bits/stdc++.h>
using namespace std;
vector<int> g[250005];
int cnum[250005];
int cidx[250005];
int dist[250005];
vector<vector<int>> cyc;
int main() {
memset(dist, 0x3f, sizeof(dist));
memset(cnum, -1, sizeof(cnum));
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int l;
cin >> l;
cyc.push_back({});
for (int j = 0; j < l; j++) {
int x;
cin >> x;
cnum[x] = i;
cidx[x] = j;
cyc.back().push_back(x);
}
}
priority_queue<pair<int, int>> pq;
dist[1] = 0;
pq.push({0, 1});
while (!pq.empty()) {
auto p = pq.top();
pq.pop();
int o = -p.first;
// cout << "DBG " << o << " " << p.second << endl;
if (cnum[p.second] != -1) {
int base = cidx[p.second];
int l = cyc[cnum[p.second]].size();
int j = (o + l - base) % l;
for (int ii = 0; ii < l; ii++) {
int i = (ii + l - base) % l;
int d = min(j == 0 ? i + 1 : i,
l + j < 2 * i ? l - i : 2 * l - i - j + 1);
// cout << "DELTA " << i << " " << j << " " << d << endl;
if (o + d < dist[cyc[cnum[p.second]][ii]]) {
dist[cyc[cnum[p.second]][ii]] = o + d;
pq.push({-(o + d), cyc[cnum[p.second]][ii]});
}
}
for (int v : g[p.second]) {
if (cnum[v] != -1)
continue;
if (o + 1 < dist[v]) {
dist[v] = o + 1;
pq.push({-(o + 1), v});
}
}
} else {
for (int v : g[p.second]) {
int wait = 0;
if (cnum[v] != -1 && cidx[v] == (o + 1) % cyc[cnum[v]].size())
wait++;
if (o + 1 < dist[v]) {
dist[v] = o + 1 + wait;
pq.push({-(o + 1 + wait), v});
}
}
}
}
if (dist[n] >= 1e9)
cout << "impossible" << endl;
else
cout << dist[n] << endl;
return 0;
}
Compilation message
watchmen.cpp: In function 'int main()':
watchmen.cpp:69:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if (cnum[v] != -1 && cidx[v] == (o + 1) % cyc[cnum[v]].size())
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10588 KB |
Output is correct |
2 |
Incorrect |
24 ms |
13452 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
10832 KB |
Output is correct |
2 |
Incorrect |
22 ms |
13524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
10832 KB |
Output is correct |
2 |
Incorrect |
22 ms |
13524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
10832 KB |
Output is correct |
2 |
Incorrect |
22 ms |
13524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10588 KB |
Output is correct |
2 |
Incorrect |
24 ms |
13452 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10588 KB |
Output is correct |
2 |
Incorrect |
24 ms |
13452 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10588 KB |
Output is correct |
2 |
Incorrect |
24 ms |
13452 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |