This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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(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] == cnum[p.second]) {
if ((cidx[v] + 1) % l == base)
continue;
int wait = 0;
if ((o + 1) % cyc[cnum[v]].size() == cidx[v])
wait++;
if (o + 1 + wait < dist[v]) {
dist[v] = o + 1 + wait;
pq.push({-(o + 1 + wait), v});
}
} else {
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 + wait < 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 (stderr)
watchmen.cpp: In function 'int main()':
watchmen.cpp:62:55: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
62 | if ((o + 1) % cyc[cnum[v]].size() == cidx[v])
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
watchmen.cpp:78:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | if (cnum[v] != -1 && cidx[v] == (o + 1) % cyc[cnum[v]].size())
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |