#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 250000;
vector<unsigned> g[N];
unsigned mod[N], resid_class[N], dis[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, m;
cin >> n >> m;
for (size_t i = 0; i < m; ++i)
{
unsigned u, v;
cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
size_t k;
cin >> k;
for (size_t i = 0; i < k; ++i)
{
size_t l;
cin >> l;
for (size_t j = 0; j < l; ++j)
{
unsigned u;
cin >> u;
mod[u - 1] = l;
resid_class[u - 1] = j;
}
}
priority_queue<pair<unsigned, unsigned>, vector<pair<unsigned, unsigned>>, greater<pair<unsigned, unsigned>>> q;
memset(dis, 255, sizeof dis);
q.emplace(0, 0);
dis[0] = 0;
while (!q.empty())
{
auto const [d, u] = q.top();
q.pop();
if (d != dis[u])
continue;
for (unsigned const &v : g[u])
if (!(mod[v] && resid_class[v] + 1 == resid_class[u] &&
d % mod[v] == resid_class[v]))
{
if (!mod[u] && mod[v] && (d + 1) % mod[v] == resid_class[v])
{
if (d + 2 < dis[v])
{
dis[v] = d + 2;
q.emplace(dis[v], v);
}
}
else if (d + 1 < dis[v])
{
dis[v] = d + 1;
q.emplace(dis[v], v);
}
}
}
cout << dis[n - 1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8084 KB |
Output is correct |
2 |
Incorrect |
48 ms |
10708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
8000 KB |
Output is correct |
2 |
Incorrect |
49 ms |
10736 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
8000 KB |
Output is correct |
2 |
Incorrect |
49 ms |
10736 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
8000 KB |
Output is correct |
2 |
Incorrect |
49 ms |
10736 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8084 KB |
Output is correct |
2 |
Incorrect |
48 ms |
10708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8084 KB |
Output is correct |
2 |
Incorrect |
48 ms |
10708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8084 KB |
Output is correct |
2 |
Incorrect |
48 ms |
10708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |