#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int N, M, K, vis[250005][2], dis[250002][2], p[250005], r[250005];
vector<int> E[250005];
vector<pii> dv[500005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> M;
for(int i = 1; i <= M; i++)
{
int u, v;
cin >> u >> v;
E[u].emplace_back(v);
E[v].emplace_back(u);
}
cin >> K;
for(int i = 1; i <= K; i++)
{
int P, u;
cin >> P;
for(int j = 0; j < P; j++)
{
cin >> u;
p[u] = P, r[u] = j;
}
}
for(int i = 1; i <= N; i++)
dis[i][0] = dis[i][1] = 1e9;
dis[1][0] = 0;
dv[0].emplace_back(pii(1, 0));
for(int d = 0; d <= 2 * N; d++)
{
while(!dv[d].empty())
{
auto [u, s] = dv[d].back();
dv[d].pop_back();
if(dis[u][s] < d)
continue;
for(auto v : E[u])
{
int t = (p[v] == 0 ? -1 : (r[v] - (d % p[v]) + p[v]) % p[v]);
if(!p[v])
if(dis[v][0] > dis[u][s] + 1)
{
dis[v][0] = dis[u][s] + 1;
dv[d + 1].emplace_back(pii(v, 0));
}
if(p[v] && t >= 2)
if(dis[v][t <= 3] > dis[u][s] + 1)
{
dis[v][t <= 3] = dis[u][s] + 1;
dv[d + 1].emplace_back(pii(v, t <= 3));
}
if(p[v] && !s && t <= 1)
if(dis[v][0] > dis[u][s] + 1 + t)
{
dis[v][0] = dis[u][s] + 1 + t;
dv[d + 1 + t].emplace_back(pii(v, 0));
}
}
}
}
//for(int i = 1; i <= N; i++)
// cout << dis[i][0] << " " << dis[i][1] << '\n';
if(dis[N][0] >= 1e9)
cout << "impossible\n";
else cout << dis[N][0];
}
Compilation message
watchmen.cpp: In function 'int main()':
watchmen.cpp:44:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | auto [u, s] = dv[d].back();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
18900 KB |
Output is correct |
2 |
Incorrect |
53 ms |
25524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
18976 KB |
Output is correct |
2 |
Incorrect |
59 ms |
25616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
18976 KB |
Output is correct |
2 |
Incorrect |
59 ms |
25616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
18976 KB |
Output is correct |
2 |
Incorrect |
59 ms |
25616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
18900 KB |
Output is correct |
2 |
Incorrect |
53 ms |
25524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
18900 KB |
Output is correct |
2 |
Incorrect |
53 ms |
25524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
18900 KB |
Output is correct |
2 |
Incorrect |
53 ms |
25524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |