#include <bits/stdc++.h>
#define ll long long
#define state tuple<ll, int, int, int>
#define F first
#define S second
using namespace std;
int N, M, K, L, l[2500005], r[2500005], nxt[2500005];
vector<int> v[2000];
vector<ll> dis[2500005];
vector<bitset<1501>> vis[2500005];
vector<int> E[2500005];
bool good(int u, ll t)
{
return r[u] == 0 || v[r[u]][t % l[r[u]]] != u;
}
bool goodmove(int u, int v, ll t)
{
if (u == nxt[v])
return good(u, t) && good(v, t);
else
return good(v, t);
}
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);
}
l[0] = 1;
cin >> K;
for (int i = 1; i <= K; i++)
{
cin >> l[i];
L = max(L, l[i]);
v[i].resize(l[i]);
for (int j = 0; j < l[i]; j++)
{
cin >> v[i][j];
r[v[i][j]] = i;
vis[v[i][j]].resize(l[i]);
}
for (int j = 0; j < l[i]; j++)
nxt[v[i][j]] = v[i][(j + 1) % l[i]];
}
for (int i = 1; i <= N; i++)
dis[i].resize(l[r[i]], (ll)1e18);
dis[1][0] = 0;
priority_queue<state, vector<state>, greater<state>> pq;
pq.push(state{0, 1, 0, 0});
while (!pq.empty())
{
auto [d, u, k, b] = pq.top();
pq.pop();
if (!b && d > dis[u][k])
continue;
if (!b)
{
for (auto v : E[u])
if (r[v] == 0 && d + 1 < dis[v][0])
{
dis[v][0] = d + 1;
pq.push(state{d + 1, v, 0, 0});
}
else if (r[v])
{
if (!goodmove(u, v, d + 1) && l[r[u]] % l[r[v]] == 0)
continue;
pq.push(state{d, u, k, v});
}
int rem = (d + 1) % l[r[u]];
if (good(u, d + 1) && dis[u][rem] > d + 1)
{
dis[u][rem] = d + 1;
pq.push(state{d + 1, u, rem, 0});
}
}
else
{
// u -> b
int rem = (d + 1) % l[r[b]];
if (vis[b][rem][l[r[u]]])
continue;
if (goodmove(u, b, d + 1))
{
vis[b][rem][l[r[u]]] = 1;
if (dis[b][rem] > d + 1)
{
dis[b][rem] = d + 1;
pq.push(state{d + 1, b, rem, 0});
}
}
pq.push(state{d + l[r[u]], u, k, b});
}
}
// for (int i = 1; i <= N; i++)
// {
// cout << i << ":";
// for (auto j : dis[i])
// cout << ' ' << j;
// cout << '\n';
// }
if (dis[N][0] >= 1e18)
cout << "impossible\n";
else
cout << dis[N][0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
181480 KB |
Output is correct |
2 |
Correct |
132 ms |
187956 KB |
Output is correct |
3 |
Correct |
136 ms |
187312 KB |
Output is correct |
4 |
Correct |
396 ms |
187552 KB |
Output is correct |
5 |
Correct |
87 ms |
179896 KB |
Output is correct |
6 |
Correct |
135 ms |
187404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
181524 KB |
Output is correct |
2 |
Correct |
130 ms |
187920 KB |
Output is correct |
3 |
Correct |
136 ms |
187408 KB |
Output is correct |
4 |
Correct |
389 ms |
187560 KB |
Output is correct |
5 |
Correct |
88 ms |
179984 KB |
Output is correct |
6 |
Correct |
133 ms |
187360 KB |
Output is correct |
7 |
Correct |
121 ms |
185488 KB |
Output is correct |
8 |
Correct |
130 ms |
185192 KB |
Output is correct |
9 |
Correct |
117 ms |
184396 KB |
Output is correct |
10 |
Correct |
194 ms |
185796 KB |
Output is correct |
11 |
Correct |
130 ms |
184616 KB |
Output is correct |
12 |
Correct |
135 ms |
185612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
181524 KB |
Output is correct |
2 |
Correct |
130 ms |
187920 KB |
Output is correct |
3 |
Correct |
136 ms |
187408 KB |
Output is correct |
4 |
Correct |
389 ms |
187560 KB |
Output is correct |
5 |
Correct |
88 ms |
179984 KB |
Output is correct |
6 |
Correct |
133 ms |
187360 KB |
Output is correct |
7 |
Correct |
121 ms |
185488 KB |
Output is correct |
8 |
Correct |
130 ms |
185192 KB |
Output is correct |
9 |
Correct |
117 ms |
184396 KB |
Output is correct |
10 |
Correct |
194 ms |
185796 KB |
Output is correct |
11 |
Correct |
130 ms |
184616 KB |
Output is correct |
12 |
Correct |
135 ms |
185612 KB |
Output is correct |
13 |
Correct |
355 ms |
181524 KB |
Output is correct |
14 |
Correct |
137 ms |
188032 KB |
Output is correct |
15 |
Correct |
140 ms |
187408 KB |
Output is correct |
16 |
Correct |
396 ms |
187484 KB |
Output is correct |
17 |
Correct |
86 ms |
179960 KB |
Output is correct |
18 |
Correct |
141 ms |
187380 KB |
Output is correct |
19 |
Correct |
146 ms |
185552 KB |
Output is correct |
20 |
Correct |
118 ms |
185072 KB |
Output is correct |
21 |
Correct |
116 ms |
184408 KB |
Output is correct |
22 |
Correct |
201 ms |
185776 KB |
Output is correct |
23 |
Correct |
130 ms |
184612 KB |
Output is correct |
24 |
Correct |
127 ms |
185592 KB |
Output is correct |
25 |
Correct |
1209 ms |
274144 KB |
Output is correct |
26 |
Correct |
1121 ms |
281408 KB |
Output is correct |
27 |
Correct |
1043 ms |
272504 KB |
Output is correct |
28 |
Correct |
840 ms |
270184 KB |
Output is correct |
29 |
Execution timed out |
6073 ms |
267628 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
181524 KB |
Output is correct |
2 |
Correct |
130 ms |
187920 KB |
Output is correct |
3 |
Correct |
136 ms |
187408 KB |
Output is correct |
4 |
Correct |
389 ms |
187560 KB |
Output is correct |
5 |
Correct |
88 ms |
179984 KB |
Output is correct |
6 |
Correct |
133 ms |
187360 KB |
Output is correct |
7 |
Correct |
121 ms |
185488 KB |
Output is correct |
8 |
Correct |
130 ms |
185192 KB |
Output is correct |
9 |
Correct |
117 ms |
184396 KB |
Output is correct |
10 |
Correct |
194 ms |
185796 KB |
Output is correct |
11 |
Correct |
130 ms |
184616 KB |
Output is correct |
12 |
Correct |
135 ms |
185612 KB |
Output is correct |
13 |
Correct |
355 ms |
181524 KB |
Output is correct |
14 |
Correct |
137 ms |
188032 KB |
Output is correct |
15 |
Correct |
140 ms |
187408 KB |
Output is correct |
16 |
Correct |
396 ms |
187484 KB |
Output is correct |
17 |
Correct |
86 ms |
179960 KB |
Output is correct |
18 |
Correct |
141 ms |
187380 KB |
Output is correct |
19 |
Correct |
146 ms |
185552 KB |
Output is correct |
20 |
Correct |
118 ms |
185072 KB |
Output is correct |
21 |
Correct |
116 ms |
184408 KB |
Output is correct |
22 |
Correct |
201 ms |
185776 KB |
Output is correct |
23 |
Correct |
130 ms |
184612 KB |
Output is correct |
24 |
Correct |
127 ms |
185592 KB |
Output is correct |
25 |
Correct |
1209 ms |
274144 KB |
Output is correct |
26 |
Correct |
1121 ms |
281408 KB |
Output is correct |
27 |
Correct |
1043 ms |
272504 KB |
Output is correct |
28 |
Correct |
840 ms |
270184 KB |
Output is correct |
29 |
Execution timed out |
6073 ms |
267628 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
181480 KB |
Output is correct |
2 |
Correct |
132 ms |
187956 KB |
Output is correct |
3 |
Correct |
136 ms |
187312 KB |
Output is correct |
4 |
Correct |
396 ms |
187552 KB |
Output is correct |
5 |
Correct |
87 ms |
179896 KB |
Output is correct |
6 |
Correct |
135 ms |
187404 KB |
Output is correct |
7 |
Correct |
362 ms |
181524 KB |
Output is correct |
8 |
Correct |
130 ms |
187920 KB |
Output is correct |
9 |
Correct |
136 ms |
187408 KB |
Output is correct |
10 |
Correct |
389 ms |
187560 KB |
Output is correct |
11 |
Correct |
88 ms |
179984 KB |
Output is correct |
12 |
Correct |
133 ms |
187360 KB |
Output is correct |
13 |
Correct |
121 ms |
185488 KB |
Output is correct |
14 |
Correct |
130 ms |
185192 KB |
Output is correct |
15 |
Correct |
117 ms |
184396 KB |
Output is correct |
16 |
Correct |
194 ms |
185796 KB |
Output is correct |
17 |
Correct |
130 ms |
184616 KB |
Output is correct |
18 |
Correct |
135 ms |
185612 KB |
Output is correct |
19 |
Correct |
77 ms |
176424 KB |
Output is correct |
20 |
Correct |
80 ms |
176388 KB |
Output is correct |
21 |
Correct |
84 ms |
176460 KB |
Output is correct |
22 |
Correct |
353 ms |
181524 KB |
Output is correct |
23 |
Correct |
134 ms |
187924 KB |
Output is correct |
24 |
Correct |
149 ms |
187376 KB |
Output is correct |
25 |
Correct |
415 ms |
187532 KB |
Output is correct |
26 |
Correct |
91 ms |
179976 KB |
Output is correct |
27 |
Correct |
137 ms |
187368 KB |
Output is correct |
28 |
Correct |
132 ms |
185496 KB |
Output is correct |
29 |
Correct |
126 ms |
185036 KB |
Output is correct |
30 |
Correct |
118 ms |
184484 KB |
Output is correct |
31 |
Correct |
184 ms |
185812 KB |
Output is correct |
32 |
Correct |
138 ms |
184560 KB |
Output is correct |
33 |
Correct |
126 ms |
185636 KB |
Output is correct |
34 |
Correct |
1136 ms |
266340 KB |
Output is correct |
35 |
Correct |
1284 ms |
262744 KB |
Output is correct |
36 |
Correct |
1212 ms |
262752 KB |
Output is correct |
37 |
Correct |
1143 ms |
267096 KB |
Output is correct |
38 |
Correct |
1126 ms |
265440 KB |
Output is correct |
39 |
Correct |
5379 ms |
263784 KB |
Output is correct |
40 |
Correct |
4342 ms |
262872 KB |
Output is correct |
41 |
Correct |
2625 ms |
262008 KB |
Output is correct |
42 |
Correct |
1296 ms |
265036 KB |
Output is correct |
43 |
Correct |
1217 ms |
269360 KB |
Output is correct |
44 |
Correct |
1286 ms |
270640 KB |
Output is correct |
45 |
Correct |
1332 ms |
265016 KB |
Output is correct |
46 |
Correct |
1151 ms |
264448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
181480 KB |
Output is correct |
2 |
Correct |
132 ms |
187956 KB |
Output is correct |
3 |
Correct |
136 ms |
187312 KB |
Output is correct |
4 |
Correct |
396 ms |
187552 KB |
Output is correct |
5 |
Correct |
87 ms |
179896 KB |
Output is correct |
6 |
Correct |
135 ms |
187404 KB |
Output is correct |
7 |
Correct |
362 ms |
181524 KB |
Output is correct |
8 |
Correct |
130 ms |
187920 KB |
Output is correct |
9 |
Correct |
136 ms |
187408 KB |
Output is correct |
10 |
Correct |
389 ms |
187560 KB |
Output is correct |
11 |
Correct |
88 ms |
179984 KB |
Output is correct |
12 |
Correct |
133 ms |
187360 KB |
Output is correct |
13 |
Correct |
121 ms |
185488 KB |
Output is correct |
14 |
Correct |
130 ms |
185192 KB |
Output is correct |
15 |
Correct |
117 ms |
184396 KB |
Output is correct |
16 |
Correct |
194 ms |
185796 KB |
Output is correct |
17 |
Correct |
130 ms |
184616 KB |
Output is correct |
18 |
Correct |
135 ms |
185612 KB |
Output is correct |
19 |
Correct |
355 ms |
181524 KB |
Output is correct |
20 |
Correct |
137 ms |
188032 KB |
Output is correct |
21 |
Correct |
140 ms |
187408 KB |
Output is correct |
22 |
Correct |
396 ms |
187484 KB |
Output is correct |
23 |
Correct |
86 ms |
179960 KB |
Output is correct |
24 |
Correct |
141 ms |
187380 KB |
Output is correct |
25 |
Correct |
146 ms |
185552 KB |
Output is correct |
26 |
Correct |
118 ms |
185072 KB |
Output is correct |
27 |
Correct |
116 ms |
184408 KB |
Output is correct |
28 |
Correct |
201 ms |
185776 KB |
Output is correct |
29 |
Correct |
130 ms |
184612 KB |
Output is correct |
30 |
Correct |
127 ms |
185592 KB |
Output is correct |
31 |
Correct |
1209 ms |
274144 KB |
Output is correct |
32 |
Correct |
1121 ms |
281408 KB |
Output is correct |
33 |
Correct |
1043 ms |
272504 KB |
Output is correct |
34 |
Correct |
840 ms |
270184 KB |
Output is correct |
35 |
Execution timed out |
6073 ms |
267628 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
181480 KB |
Output is correct |
2 |
Correct |
132 ms |
187956 KB |
Output is correct |
3 |
Correct |
136 ms |
187312 KB |
Output is correct |
4 |
Correct |
396 ms |
187552 KB |
Output is correct |
5 |
Correct |
87 ms |
179896 KB |
Output is correct |
6 |
Correct |
135 ms |
187404 KB |
Output is correct |
7 |
Correct |
362 ms |
181524 KB |
Output is correct |
8 |
Correct |
130 ms |
187920 KB |
Output is correct |
9 |
Correct |
136 ms |
187408 KB |
Output is correct |
10 |
Correct |
389 ms |
187560 KB |
Output is correct |
11 |
Correct |
88 ms |
179984 KB |
Output is correct |
12 |
Correct |
133 ms |
187360 KB |
Output is correct |
13 |
Correct |
121 ms |
185488 KB |
Output is correct |
14 |
Correct |
130 ms |
185192 KB |
Output is correct |
15 |
Correct |
117 ms |
184396 KB |
Output is correct |
16 |
Correct |
194 ms |
185796 KB |
Output is correct |
17 |
Correct |
130 ms |
184616 KB |
Output is correct |
18 |
Correct |
135 ms |
185612 KB |
Output is correct |
19 |
Correct |
355 ms |
181524 KB |
Output is correct |
20 |
Correct |
137 ms |
188032 KB |
Output is correct |
21 |
Correct |
140 ms |
187408 KB |
Output is correct |
22 |
Correct |
396 ms |
187484 KB |
Output is correct |
23 |
Correct |
86 ms |
179960 KB |
Output is correct |
24 |
Correct |
141 ms |
187380 KB |
Output is correct |
25 |
Correct |
146 ms |
185552 KB |
Output is correct |
26 |
Correct |
118 ms |
185072 KB |
Output is correct |
27 |
Correct |
116 ms |
184408 KB |
Output is correct |
28 |
Correct |
201 ms |
185776 KB |
Output is correct |
29 |
Correct |
130 ms |
184612 KB |
Output is correct |
30 |
Correct |
127 ms |
185592 KB |
Output is correct |
31 |
Correct |
1209 ms |
274144 KB |
Output is correct |
32 |
Correct |
1121 ms |
281408 KB |
Output is correct |
33 |
Correct |
1043 ms |
272504 KB |
Output is correct |
34 |
Correct |
840 ms |
270184 KB |
Output is correct |
35 |
Execution timed out |
6073 ms |
267628 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |