#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
const int NS = 250004;
int n, m, k;
vector<int> way[NS];
int dist[NS][2], period[NS], col[NS], st[NS], num[NS];
vector<int> vc;
int vcdist[2754][2754][2];
struct Node{
int x, y, z;
Node(){}
Node(int x, int y, int z):x(x), y(y), z(z){}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; ++i){
int x, y;
cin >> x >> y;
--x, --y;
way[x].push_back(y);
way[y].push_back(x);
}
cin >> k;
for(int i = 0; i < k; ++i){
int l;
cin >> l;
for(int j = 0; j < l; ++j){
int x;
cin >> x;
--x;
num[x] = (int)vc.size();
vc.push_back(x);
col[x] = i + 1;
st[x] = j;
period[x] = l;
}
}
queue<Node> que;
for(int i = 0; i < n; ++i){
dist[i][0] = dist[i][1] = (int)1e9;
}
for(int i = 0; i < 2754; ++i){
for(int j = 0; j < 2754; ++j){
vcdist[i][j][0] = vcdist[i][j][1] = (int)1e9;
}
}
dist[0][1] = 0;
que.push(Node(0, -1, 1));
while(!que.empty()){
int now = que.front().x, t = que.front().y, z = que.front().z;
if(now == n - 1){
cout << dist[now][z] << '\n';
return 0;
}
if(col[now]){
//cout << now + 1 << ' ' << t << ' ' << z << ' ' << (int)vcdist[num[now]][t][z] << endl;
}
que.pop();
if(t == -1){
if(z == 1){
if(dist[now][1] + 1 < dist[now][0]){
dist[now][0] = dist[now][1] + 1;
que.push(Node(now, -1, 0));
}
}
for(auto&nxt:way[now]){
//cout << now << ' ' << nxt << ' ' << col[nxt] << endl;
if(!col[nxt]){
if(dist[now][z] + 1 < dist[nxt][1]){
dist[nxt][1] = dist[now][z] + 1;
que.push(Node(nxt, -1, 1));
}
}
else{
int md = (st[nxt] - dist[now][z] % period[nxt] - 1 + period[nxt] * 2) % period[nxt];
if(md != 0){
if(dist[now][z] + 1 < vcdist[num[nxt]][md][1]){
vcdist[num[nxt]][md][1] = dist[now][z] + 1;
que.push(Node(nxt, md, 1));
}
}
}
}
}
else{
int nd = vcdist[num[now]][t][z];
if(z == 1 && nd + 1 < vcdist[num[now]][(t - 1 + period[now]) % period[now]][1]){
vcdist[num[now]][(t - 1 + period[now]) % period[now]][1] = nd + 1;
que.push(Node(now, (t - 1 + period[now]) % period[now], 1));
}
for(auto&nxt:way[now]){
if(!col[nxt]){
if(nd + 1 < dist[nxt][1]){
dist[nxt][1] = nd + 1;
que.push(Node(nxt, -1, 1));
}
}
else{
int md = (st[nxt] - nd % period[nxt] - 1 + period[nxt] * 2) % period[nxt];
if(md != 0){
if(md != (period[nxt] - 1) || t != 1){
if(t != 0){
if(nd + 1 < vcdist[num[nxt]][md][0]){
vcdist[num[nxt]][md][0] = nd + 1;
que.push(Node(nxt, md, 0));
}
}
}
}
}
}
}
}
cout << "impossible\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
127308 KB |
Output is correct |
2 |
Correct |
99 ms |
132060 KB |
Output is correct |
3 |
Correct |
94 ms |
132296 KB |
Output is correct |
4 |
Correct |
143 ms |
132236 KB |
Output is correct |
5 |
Correct |
49 ms |
124944 KB |
Output is correct |
6 |
Correct |
91 ms |
132268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
127308 KB |
Output is correct |
2 |
Correct |
97 ms |
132100 KB |
Output is correct |
3 |
Correct |
101 ms |
132356 KB |
Output is correct |
4 |
Correct |
136 ms |
132292 KB |
Output is correct |
5 |
Correct |
49 ms |
124872 KB |
Output is correct |
6 |
Correct |
107 ms |
132412 KB |
Output is correct |
7 |
Correct |
99 ms |
132096 KB |
Output is correct |
8 |
Correct |
99 ms |
132164 KB |
Output is correct |
9 |
Correct |
92 ms |
132080 KB |
Output is correct |
10 |
Correct |
102 ms |
132300 KB |
Output is correct |
11 |
Correct |
101 ms |
132288 KB |
Output is correct |
12 |
Correct |
91 ms |
132280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
127308 KB |
Output is correct |
2 |
Correct |
97 ms |
132100 KB |
Output is correct |
3 |
Correct |
101 ms |
132356 KB |
Output is correct |
4 |
Correct |
136 ms |
132292 KB |
Output is correct |
5 |
Correct |
49 ms |
124872 KB |
Output is correct |
6 |
Correct |
107 ms |
132412 KB |
Output is correct |
7 |
Correct |
99 ms |
132096 KB |
Output is correct |
8 |
Correct |
99 ms |
132164 KB |
Output is correct |
9 |
Correct |
92 ms |
132080 KB |
Output is correct |
10 |
Correct |
102 ms |
132300 KB |
Output is correct |
11 |
Correct |
101 ms |
132288 KB |
Output is correct |
12 |
Correct |
91 ms |
132280 KB |
Output is correct |
13 |
Correct |
59 ms |
127416 KB |
Output is correct |
14 |
Correct |
101 ms |
132136 KB |
Output is correct |
15 |
Correct |
91 ms |
132324 KB |
Output is correct |
16 |
Correct |
148 ms |
132428 KB |
Output is correct |
17 |
Correct |
45 ms |
124972 KB |
Output is correct |
18 |
Correct |
98 ms |
132360 KB |
Output is correct |
19 |
Correct |
103 ms |
132060 KB |
Output is correct |
20 |
Correct |
90 ms |
132124 KB |
Output is correct |
21 |
Correct |
92 ms |
132184 KB |
Output is correct |
22 |
Correct |
101 ms |
132300 KB |
Output is correct |
23 |
Correct |
88 ms |
132240 KB |
Output is correct |
24 |
Correct |
98 ms |
132300 KB |
Output is correct |
25 |
Correct |
1429 ms |
248828 KB |
Output is correct |
26 |
Correct |
1502 ms |
261436 KB |
Output is correct |
27 |
Correct |
1283 ms |
250152 KB |
Output is correct |
28 |
Correct |
984 ms |
260224 KB |
Output is correct |
29 |
Correct |
5523 ms |
237580 KB |
Output is correct |
30 |
Execution timed out |
6064 ms |
243908 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
127308 KB |
Output is correct |
2 |
Correct |
97 ms |
132100 KB |
Output is correct |
3 |
Correct |
101 ms |
132356 KB |
Output is correct |
4 |
Correct |
136 ms |
132292 KB |
Output is correct |
5 |
Correct |
49 ms |
124872 KB |
Output is correct |
6 |
Correct |
107 ms |
132412 KB |
Output is correct |
7 |
Correct |
99 ms |
132096 KB |
Output is correct |
8 |
Correct |
99 ms |
132164 KB |
Output is correct |
9 |
Correct |
92 ms |
132080 KB |
Output is correct |
10 |
Correct |
102 ms |
132300 KB |
Output is correct |
11 |
Correct |
101 ms |
132288 KB |
Output is correct |
12 |
Correct |
91 ms |
132280 KB |
Output is correct |
13 |
Correct |
59 ms |
127416 KB |
Output is correct |
14 |
Correct |
101 ms |
132136 KB |
Output is correct |
15 |
Correct |
91 ms |
132324 KB |
Output is correct |
16 |
Correct |
148 ms |
132428 KB |
Output is correct |
17 |
Correct |
45 ms |
124972 KB |
Output is correct |
18 |
Correct |
98 ms |
132360 KB |
Output is correct |
19 |
Correct |
103 ms |
132060 KB |
Output is correct |
20 |
Correct |
90 ms |
132124 KB |
Output is correct |
21 |
Correct |
92 ms |
132184 KB |
Output is correct |
22 |
Correct |
101 ms |
132300 KB |
Output is correct |
23 |
Correct |
88 ms |
132240 KB |
Output is correct |
24 |
Correct |
98 ms |
132300 KB |
Output is correct |
25 |
Correct |
1429 ms |
248828 KB |
Output is correct |
26 |
Correct |
1502 ms |
261436 KB |
Output is correct |
27 |
Correct |
1283 ms |
250152 KB |
Output is correct |
28 |
Correct |
984 ms |
260224 KB |
Output is correct |
29 |
Correct |
5523 ms |
237580 KB |
Output is correct |
30 |
Execution timed out |
6064 ms |
243908 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
127308 KB |
Output is correct |
2 |
Correct |
99 ms |
132060 KB |
Output is correct |
3 |
Correct |
94 ms |
132296 KB |
Output is correct |
4 |
Correct |
143 ms |
132236 KB |
Output is correct |
5 |
Correct |
49 ms |
124944 KB |
Output is correct |
6 |
Correct |
91 ms |
132268 KB |
Output is correct |
7 |
Correct |
58 ms |
127308 KB |
Output is correct |
8 |
Correct |
97 ms |
132100 KB |
Output is correct |
9 |
Correct |
101 ms |
132356 KB |
Output is correct |
10 |
Correct |
136 ms |
132292 KB |
Output is correct |
11 |
Correct |
49 ms |
124872 KB |
Output is correct |
12 |
Correct |
107 ms |
132412 KB |
Output is correct |
13 |
Correct |
99 ms |
132096 KB |
Output is correct |
14 |
Correct |
99 ms |
132164 KB |
Output is correct |
15 |
Correct |
92 ms |
132080 KB |
Output is correct |
16 |
Correct |
102 ms |
132300 KB |
Output is correct |
17 |
Correct |
101 ms |
132288 KB |
Output is correct |
18 |
Correct |
91 ms |
132280 KB |
Output is correct |
19 |
Correct |
46 ms |
124892 KB |
Output is correct |
20 |
Correct |
47 ms |
124876 KB |
Output is correct |
21 |
Correct |
51 ms |
124900 KB |
Output is correct |
22 |
Correct |
59 ms |
127312 KB |
Output is correct |
23 |
Correct |
102 ms |
132140 KB |
Output is correct |
24 |
Correct |
95 ms |
132452 KB |
Output is correct |
25 |
Correct |
147 ms |
132308 KB |
Output is correct |
26 |
Correct |
46 ms |
124884 KB |
Output is correct |
27 |
Correct |
98 ms |
132288 KB |
Output is correct |
28 |
Correct |
94 ms |
132140 KB |
Output is correct |
29 |
Correct |
93 ms |
132104 KB |
Output is correct |
30 |
Correct |
94 ms |
132088 KB |
Output is correct |
31 |
Correct |
108 ms |
132340 KB |
Output is correct |
32 |
Correct |
99 ms |
132164 KB |
Output is correct |
33 |
Correct |
105 ms |
132296 KB |
Output is correct |
34 |
Correct |
1454 ms |
248804 KB |
Output is correct |
35 |
Correct |
1561 ms |
242128 KB |
Output is correct |
36 |
Correct |
1561 ms |
241928 KB |
Output is correct |
37 |
Incorrect |
1452 ms |
255720 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
127308 KB |
Output is correct |
2 |
Correct |
99 ms |
132060 KB |
Output is correct |
3 |
Correct |
94 ms |
132296 KB |
Output is correct |
4 |
Correct |
143 ms |
132236 KB |
Output is correct |
5 |
Correct |
49 ms |
124944 KB |
Output is correct |
6 |
Correct |
91 ms |
132268 KB |
Output is correct |
7 |
Correct |
58 ms |
127308 KB |
Output is correct |
8 |
Correct |
97 ms |
132100 KB |
Output is correct |
9 |
Correct |
101 ms |
132356 KB |
Output is correct |
10 |
Correct |
136 ms |
132292 KB |
Output is correct |
11 |
Correct |
49 ms |
124872 KB |
Output is correct |
12 |
Correct |
107 ms |
132412 KB |
Output is correct |
13 |
Correct |
99 ms |
132096 KB |
Output is correct |
14 |
Correct |
99 ms |
132164 KB |
Output is correct |
15 |
Correct |
92 ms |
132080 KB |
Output is correct |
16 |
Correct |
102 ms |
132300 KB |
Output is correct |
17 |
Correct |
101 ms |
132288 KB |
Output is correct |
18 |
Correct |
91 ms |
132280 KB |
Output is correct |
19 |
Correct |
59 ms |
127416 KB |
Output is correct |
20 |
Correct |
101 ms |
132136 KB |
Output is correct |
21 |
Correct |
91 ms |
132324 KB |
Output is correct |
22 |
Correct |
148 ms |
132428 KB |
Output is correct |
23 |
Correct |
45 ms |
124972 KB |
Output is correct |
24 |
Correct |
98 ms |
132360 KB |
Output is correct |
25 |
Correct |
103 ms |
132060 KB |
Output is correct |
26 |
Correct |
90 ms |
132124 KB |
Output is correct |
27 |
Correct |
92 ms |
132184 KB |
Output is correct |
28 |
Correct |
101 ms |
132300 KB |
Output is correct |
29 |
Correct |
88 ms |
132240 KB |
Output is correct |
30 |
Correct |
98 ms |
132300 KB |
Output is correct |
31 |
Correct |
1429 ms |
248828 KB |
Output is correct |
32 |
Correct |
1502 ms |
261436 KB |
Output is correct |
33 |
Correct |
1283 ms |
250152 KB |
Output is correct |
34 |
Correct |
984 ms |
260224 KB |
Output is correct |
35 |
Correct |
5523 ms |
237580 KB |
Output is correct |
36 |
Execution timed out |
6064 ms |
243908 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
127308 KB |
Output is correct |
2 |
Correct |
99 ms |
132060 KB |
Output is correct |
3 |
Correct |
94 ms |
132296 KB |
Output is correct |
4 |
Correct |
143 ms |
132236 KB |
Output is correct |
5 |
Correct |
49 ms |
124944 KB |
Output is correct |
6 |
Correct |
91 ms |
132268 KB |
Output is correct |
7 |
Correct |
58 ms |
127308 KB |
Output is correct |
8 |
Correct |
97 ms |
132100 KB |
Output is correct |
9 |
Correct |
101 ms |
132356 KB |
Output is correct |
10 |
Correct |
136 ms |
132292 KB |
Output is correct |
11 |
Correct |
49 ms |
124872 KB |
Output is correct |
12 |
Correct |
107 ms |
132412 KB |
Output is correct |
13 |
Correct |
99 ms |
132096 KB |
Output is correct |
14 |
Correct |
99 ms |
132164 KB |
Output is correct |
15 |
Correct |
92 ms |
132080 KB |
Output is correct |
16 |
Correct |
102 ms |
132300 KB |
Output is correct |
17 |
Correct |
101 ms |
132288 KB |
Output is correct |
18 |
Correct |
91 ms |
132280 KB |
Output is correct |
19 |
Correct |
59 ms |
127416 KB |
Output is correct |
20 |
Correct |
101 ms |
132136 KB |
Output is correct |
21 |
Correct |
91 ms |
132324 KB |
Output is correct |
22 |
Correct |
148 ms |
132428 KB |
Output is correct |
23 |
Correct |
45 ms |
124972 KB |
Output is correct |
24 |
Correct |
98 ms |
132360 KB |
Output is correct |
25 |
Correct |
103 ms |
132060 KB |
Output is correct |
26 |
Correct |
90 ms |
132124 KB |
Output is correct |
27 |
Correct |
92 ms |
132184 KB |
Output is correct |
28 |
Correct |
101 ms |
132300 KB |
Output is correct |
29 |
Correct |
88 ms |
132240 KB |
Output is correct |
30 |
Correct |
98 ms |
132300 KB |
Output is correct |
31 |
Correct |
1429 ms |
248828 KB |
Output is correct |
32 |
Correct |
1502 ms |
261436 KB |
Output is correct |
33 |
Correct |
1283 ms |
250152 KB |
Output is correct |
34 |
Correct |
984 ms |
260224 KB |
Output is correct |
35 |
Correct |
5523 ms |
237580 KB |
Output is correct |
36 |
Execution timed out |
6064 ms |
243908 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |