#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
const int inf = 1e9+10;
const int maxn = 3e5+10;
int n, m, k;
int szrt[maxn], idrt[maxn], thrt[maxn], isrt[maxn], nxrt[maxn], pvrt[maxn], dlol[maxn];
vector<int> g[maxn], d[maxn];
int32_t main() {
// freopen("in.in", "r", stdin);
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u,v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
cin >> k;
for(int i = 1; i <= k; i++) {
cin >> szrt[i];
for(int j = 0; j < szrt[i]; j++) {
int v; cin >> v;
idrt[v] = i;
isrt[v] = 1;
thrt[v] = j;
}
}
szrt[0] = 1;
for(int i = 1; i <= n; i++) {
dlol[i] = inf;
if(isrt[i]) {
for(int j = 0; j < szrt[idrt[i]]; j++) {
d[i].pb(inf);
}
int u = i;
for(auto v : g[u]) {
if(isrt[v] && idrt[u] == idrt[v] && thrt[v] == (thrt[u]+1)%szrt[idrt[u]]) {
nxrt[u] = v;
}
if(isrt[v] && idrt[u] == idrt[v] && thrt[u] == (thrt[v]+1)%szrt[idrt[u]]) {
pvrt[u] = v;
}
}
assert(nxrt[u] != 0);
}
else {
d[i].pb(inf);
}
}
d[1][0] = 0;
priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>>> pq;
pq.push(mp(0,mp(1,0)));
while(pq.size()) {
int u = pq.top().sc.fr;
int t = pq.top().sc.sc;
int dist = pq.top().fr;
pq.pop();
if(t != -1 && dist != d[u][t]) continue;
if(t == -1 && dist != dlol[u]) continue;
if(t == -1) {
for(auto v : g[u]) {
if(isrt[v]) continue;
int d1 = dlol[u]+1;
int t1 = 0;
if(d[v][t1] > d1) {
d[v][t1] = d1;
pq.push(mp(d[v][t1],mp(v,t1)));
}
}
continue;
}
if(isrt[u]) {
for(auto v : g[u]) {
// if(v == nxrt[u] || v == pvrt[u]) {
if(isrt[v]) {
int d1 = d[u][t]+1;
int t1 = d1%szrt[idrt[v]];
// Vou encontrar o guarda em v ou indo para v
// indo para v == u é depois de v e em t1 ele vai estar em u
if(t1 == thrt[v] || (thrt[u] == (thrt[v]+1)%szrt[idrt[u]] && t1 == thrt[u])) continue;
if(d[v][t1] > d1) {
d[v][t1] = d1;
pq.push(mp(d[v][t1],mp(v,t1)));
}
}
}
if(dlol[u] > d[u][t]) {
dlol[u] = d[u][t];
pq.push(mp(dlol[u],mp(u,-1)));
}
}
else {
for(auto v : g[u]) {
if(isrt[v]) {
// d[u][t]+1+i = (thrt[v]+1) MOD
// i = trrt[v]-d[u][t] MOD
int i = ((thrt[v]-d[u][t])%szrt[idrt[v]]+szrt[idrt[v]])%szrt[idrt[v]];
int d1 = d[u][t]+1+i;
int t1 = d1%szrt[idrt[v]];
if(t1 == thrt[v]) continue;
if(d[v][t1] > d1) {
d[v][t1] = d1;
pq.push(mp(d[v][t1],mp(v,t1)));
}
d1 = d[u][t]+1;
t1 = d1%szrt[idrt[v]];
if(t1 == thrt[v]) continue;
if(d[v][t1] > d1) {
d[v][t1] = d1;
pq.push(mp(d[v][t1],mp(v,t1)));
}
}
else {
int d1 = d[u][t]+1;
int t1 = 0;
if(d[v][t1] > d1) {
d[v][t1] = d1;
pq.push(mp(d[v][t1],mp(v,t1)));
}
}
}
}
}
if(d[n][0] == inf) cout << "impossible" << endl;
else cout << d[n][0] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15336 KB |
Output is correct |
2 |
Correct |
109 ms |
22524 KB |
Output is correct |
3 |
Correct |
152 ms |
22068 KB |
Output is correct |
4 |
Correct |
133 ms |
22044 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
106 ms |
21868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15316 KB |
Output is correct |
2 |
Correct |
143 ms |
22512 KB |
Output is correct |
3 |
Correct |
107 ms |
22036 KB |
Output is correct |
4 |
Correct |
141 ms |
22040 KB |
Output is correct |
5 |
Correct |
9 ms |
14460 KB |
Output is correct |
6 |
Correct |
117 ms |
21836 KB |
Output is correct |
7 |
Correct |
116 ms |
21816 KB |
Output is correct |
8 |
Correct |
132 ms |
21884 KB |
Output is correct |
9 |
Correct |
114 ms |
21840 KB |
Output is correct |
10 |
Correct |
143 ms |
22056 KB |
Output is correct |
11 |
Correct |
159 ms |
21984 KB |
Output is correct |
12 |
Correct |
139 ms |
21864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15316 KB |
Output is correct |
2 |
Correct |
143 ms |
22512 KB |
Output is correct |
3 |
Correct |
107 ms |
22036 KB |
Output is correct |
4 |
Correct |
141 ms |
22040 KB |
Output is correct |
5 |
Correct |
9 ms |
14460 KB |
Output is correct |
6 |
Correct |
117 ms |
21836 KB |
Output is correct |
7 |
Correct |
116 ms |
21816 KB |
Output is correct |
8 |
Correct |
132 ms |
21884 KB |
Output is correct |
9 |
Correct |
114 ms |
21840 KB |
Output is correct |
10 |
Correct |
143 ms |
22056 KB |
Output is correct |
11 |
Correct |
159 ms |
21984 KB |
Output is correct |
12 |
Correct |
139 ms |
21864 KB |
Output is correct |
13 |
Correct |
78 ms |
15376 KB |
Output is correct |
14 |
Correct |
127 ms |
22516 KB |
Output is correct |
15 |
Correct |
149 ms |
21936 KB |
Output is correct |
16 |
Correct |
194 ms |
22020 KB |
Output is correct |
17 |
Correct |
9 ms |
14420 KB |
Output is correct |
18 |
Correct |
141 ms |
21884 KB |
Output is correct |
19 |
Correct |
125 ms |
21872 KB |
Output is correct |
20 |
Correct |
132 ms |
21864 KB |
Output is correct |
21 |
Correct |
138 ms |
21792 KB |
Output is correct |
22 |
Correct |
140 ms |
22132 KB |
Output is correct |
23 |
Correct |
140 ms |
21872 KB |
Output is correct |
24 |
Correct |
138 ms |
21864 KB |
Output is correct |
25 |
Correct |
2823 ms |
66004 KB |
Output is correct |
26 |
Correct |
2621 ms |
71216 KB |
Output is correct |
27 |
Correct |
2360 ms |
66932 KB |
Output is correct |
28 |
Correct |
2266 ms |
70860 KB |
Output is correct |
29 |
Correct |
3245 ms |
61352 KB |
Output is correct |
30 |
Correct |
3382 ms |
64788 KB |
Output is correct |
31 |
Correct |
2879 ms |
71220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15316 KB |
Output is correct |
2 |
Correct |
143 ms |
22512 KB |
Output is correct |
3 |
Correct |
107 ms |
22036 KB |
Output is correct |
4 |
Correct |
141 ms |
22040 KB |
Output is correct |
5 |
Correct |
9 ms |
14460 KB |
Output is correct |
6 |
Correct |
117 ms |
21836 KB |
Output is correct |
7 |
Correct |
116 ms |
21816 KB |
Output is correct |
8 |
Correct |
132 ms |
21884 KB |
Output is correct |
9 |
Correct |
114 ms |
21840 KB |
Output is correct |
10 |
Correct |
143 ms |
22056 KB |
Output is correct |
11 |
Correct |
159 ms |
21984 KB |
Output is correct |
12 |
Correct |
139 ms |
21864 KB |
Output is correct |
13 |
Correct |
78 ms |
15376 KB |
Output is correct |
14 |
Correct |
127 ms |
22516 KB |
Output is correct |
15 |
Correct |
149 ms |
21936 KB |
Output is correct |
16 |
Correct |
194 ms |
22020 KB |
Output is correct |
17 |
Correct |
9 ms |
14420 KB |
Output is correct |
18 |
Correct |
141 ms |
21884 KB |
Output is correct |
19 |
Correct |
125 ms |
21872 KB |
Output is correct |
20 |
Correct |
132 ms |
21864 KB |
Output is correct |
21 |
Correct |
138 ms |
21792 KB |
Output is correct |
22 |
Correct |
140 ms |
22132 KB |
Output is correct |
23 |
Correct |
140 ms |
21872 KB |
Output is correct |
24 |
Correct |
138 ms |
21864 KB |
Output is correct |
25 |
Correct |
2823 ms |
66004 KB |
Output is correct |
26 |
Correct |
2621 ms |
71216 KB |
Output is correct |
27 |
Correct |
2360 ms |
66932 KB |
Output is correct |
28 |
Correct |
2266 ms |
70860 KB |
Output is correct |
29 |
Correct |
3245 ms |
61352 KB |
Output is correct |
30 |
Correct |
3382 ms |
64788 KB |
Output is correct |
31 |
Correct |
2879 ms |
71220 KB |
Output is correct |
32 |
Correct |
78 ms |
15372 KB |
Output is correct |
33 |
Correct |
144 ms |
22608 KB |
Output is correct |
34 |
Correct |
123 ms |
21964 KB |
Output is correct |
35 |
Correct |
175 ms |
22044 KB |
Output is correct |
36 |
Correct |
9 ms |
14420 KB |
Output is correct |
37 |
Correct |
136 ms |
21892 KB |
Output is correct |
38 |
Correct |
129 ms |
21952 KB |
Output is correct |
39 |
Correct |
137 ms |
21852 KB |
Output is correct |
40 |
Correct |
127 ms |
21900 KB |
Output is correct |
41 |
Correct |
135 ms |
22112 KB |
Output is correct |
42 |
Correct |
121 ms |
21916 KB |
Output is correct |
43 |
Correct |
129 ms |
21928 KB |
Output is correct |
44 |
Correct |
2928 ms |
65932 KB |
Output is correct |
45 |
Correct |
2637 ms |
71232 KB |
Output is correct |
46 |
Correct |
2590 ms |
66928 KB |
Output is correct |
47 |
Correct |
2243 ms |
70876 KB |
Output is correct |
48 |
Correct |
3327 ms |
61408 KB |
Output is correct |
49 |
Correct |
4034 ms |
64940 KB |
Output is correct |
50 |
Correct |
2792 ms |
71296 KB |
Output is correct |
51 |
Correct |
3119 ms |
79900 KB |
Output is correct |
52 |
Correct |
3163 ms |
94612 KB |
Output is correct |
53 |
Correct |
2937 ms |
78304 KB |
Output is correct |
54 |
Correct |
2402 ms |
64008 KB |
Output is correct |
55 |
Execution timed out |
6058 ms |
80832 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15336 KB |
Output is correct |
2 |
Correct |
109 ms |
22524 KB |
Output is correct |
3 |
Correct |
152 ms |
22068 KB |
Output is correct |
4 |
Correct |
133 ms |
22044 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
106 ms |
21868 KB |
Output is correct |
7 |
Correct |
72 ms |
15316 KB |
Output is correct |
8 |
Correct |
143 ms |
22512 KB |
Output is correct |
9 |
Correct |
107 ms |
22036 KB |
Output is correct |
10 |
Correct |
141 ms |
22040 KB |
Output is correct |
11 |
Correct |
9 ms |
14460 KB |
Output is correct |
12 |
Correct |
117 ms |
21836 KB |
Output is correct |
13 |
Correct |
116 ms |
21816 KB |
Output is correct |
14 |
Correct |
132 ms |
21884 KB |
Output is correct |
15 |
Correct |
114 ms |
21840 KB |
Output is correct |
16 |
Correct |
143 ms |
22056 KB |
Output is correct |
17 |
Correct |
159 ms |
21984 KB |
Output is correct |
18 |
Correct |
139 ms |
21864 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
15 ms |
14424 KB |
Output is correct |
22 |
Correct |
89 ms |
15368 KB |
Output is correct |
23 |
Correct |
137 ms |
22476 KB |
Output is correct |
24 |
Correct |
119 ms |
21960 KB |
Output is correct |
25 |
Correct |
149 ms |
22040 KB |
Output is correct |
26 |
Correct |
10 ms |
14420 KB |
Output is correct |
27 |
Correct |
124 ms |
21872 KB |
Output is correct |
28 |
Correct |
128 ms |
21988 KB |
Output is correct |
29 |
Correct |
139 ms |
21848 KB |
Output is correct |
30 |
Correct |
121 ms |
21816 KB |
Output is correct |
31 |
Correct |
135 ms |
22144 KB |
Output is correct |
32 |
Correct |
117 ms |
21984 KB |
Output is correct |
33 |
Correct |
124 ms |
21852 KB |
Output is correct |
34 |
Correct |
3333 ms |
66084 KB |
Output is correct |
35 |
Correct |
3080 ms |
62436 KB |
Output is correct |
36 |
Correct |
2842 ms |
62520 KB |
Output is correct |
37 |
Incorrect |
2420 ms |
67540 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15336 KB |
Output is correct |
2 |
Correct |
109 ms |
22524 KB |
Output is correct |
3 |
Correct |
152 ms |
22068 KB |
Output is correct |
4 |
Correct |
133 ms |
22044 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
106 ms |
21868 KB |
Output is correct |
7 |
Correct |
72 ms |
15316 KB |
Output is correct |
8 |
Correct |
143 ms |
22512 KB |
Output is correct |
9 |
Correct |
107 ms |
22036 KB |
Output is correct |
10 |
Correct |
141 ms |
22040 KB |
Output is correct |
11 |
Correct |
9 ms |
14460 KB |
Output is correct |
12 |
Correct |
117 ms |
21836 KB |
Output is correct |
13 |
Correct |
116 ms |
21816 KB |
Output is correct |
14 |
Correct |
132 ms |
21884 KB |
Output is correct |
15 |
Correct |
114 ms |
21840 KB |
Output is correct |
16 |
Correct |
143 ms |
22056 KB |
Output is correct |
17 |
Correct |
159 ms |
21984 KB |
Output is correct |
18 |
Correct |
139 ms |
21864 KB |
Output is correct |
19 |
Correct |
78 ms |
15376 KB |
Output is correct |
20 |
Correct |
127 ms |
22516 KB |
Output is correct |
21 |
Correct |
149 ms |
21936 KB |
Output is correct |
22 |
Correct |
194 ms |
22020 KB |
Output is correct |
23 |
Correct |
9 ms |
14420 KB |
Output is correct |
24 |
Correct |
141 ms |
21884 KB |
Output is correct |
25 |
Correct |
125 ms |
21872 KB |
Output is correct |
26 |
Correct |
132 ms |
21864 KB |
Output is correct |
27 |
Correct |
138 ms |
21792 KB |
Output is correct |
28 |
Correct |
140 ms |
22132 KB |
Output is correct |
29 |
Correct |
140 ms |
21872 KB |
Output is correct |
30 |
Correct |
138 ms |
21864 KB |
Output is correct |
31 |
Correct |
2823 ms |
66004 KB |
Output is correct |
32 |
Correct |
2621 ms |
71216 KB |
Output is correct |
33 |
Correct |
2360 ms |
66932 KB |
Output is correct |
34 |
Correct |
2266 ms |
70860 KB |
Output is correct |
35 |
Correct |
3245 ms |
61352 KB |
Output is correct |
36 |
Correct |
3382 ms |
64788 KB |
Output is correct |
37 |
Correct |
2879 ms |
71220 KB |
Output is correct |
38 |
Correct |
7 ms |
14420 KB |
Output is correct |
39 |
Correct |
7 ms |
14420 KB |
Output is correct |
40 |
Correct |
15 ms |
14424 KB |
Output is correct |
41 |
Correct |
89 ms |
15368 KB |
Output is correct |
42 |
Correct |
137 ms |
22476 KB |
Output is correct |
43 |
Correct |
119 ms |
21960 KB |
Output is correct |
44 |
Correct |
149 ms |
22040 KB |
Output is correct |
45 |
Correct |
10 ms |
14420 KB |
Output is correct |
46 |
Correct |
124 ms |
21872 KB |
Output is correct |
47 |
Correct |
128 ms |
21988 KB |
Output is correct |
48 |
Correct |
139 ms |
21848 KB |
Output is correct |
49 |
Correct |
121 ms |
21816 KB |
Output is correct |
50 |
Correct |
135 ms |
22144 KB |
Output is correct |
51 |
Correct |
117 ms |
21984 KB |
Output is correct |
52 |
Correct |
124 ms |
21852 KB |
Output is correct |
53 |
Correct |
3333 ms |
66084 KB |
Output is correct |
54 |
Correct |
3080 ms |
62436 KB |
Output is correct |
55 |
Correct |
2842 ms |
62520 KB |
Output is correct |
56 |
Incorrect |
2420 ms |
67540 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15336 KB |
Output is correct |
2 |
Correct |
109 ms |
22524 KB |
Output is correct |
3 |
Correct |
152 ms |
22068 KB |
Output is correct |
4 |
Correct |
133 ms |
22044 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
106 ms |
21868 KB |
Output is correct |
7 |
Correct |
72 ms |
15316 KB |
Output is correct |
8 |
Correct |
143 ms |
22512 KB |
Output is correct |
9 |
Correct |
107 ms |
22036 KB |
Output is correct |
10 |
Correct |
141 ms |
22040 KB |
Output is correct |
11 |
Correct |
9 ms |
14460 KB |
Output is correct |
12 |
Correct |
117 ms |
21836 KB |
Output is correct |
13 |
Correct |
116 ms |
21816 KB |
Output is correct |
14 |
Correct |
132 ms |
21884 KB |
Output is correct |
15 |
Correct |
114 ms |
21840 KB |
Output is correct |
16 |
Correct |
143 ms |
22056 KB |
Output is correct |
17 |
Correct |
159 ms |
21984 KB |
Output is correct |
18 |
Correct |
139 ms |
21864 KB |
Output is correct |
19 |
Correct |
78 ms |
15376 KB |
Output is correct |
20 |
Correct |
127 ms |
22516 KB |
Output is correct |
21 |
Correct |
149 ms |
21936 KB |
Output is correct |
22 |
Correct |
194 ms |
22020 KB |
Output is correct |
23 |
Correct |
9 ms |
14420 KB |
Output is correct |
24 |
Correct |
141 ms |
21884 KB |
Output is correct |
25 |
Correct |
125 ms |
21872 KB |
Output is correct |
26 |
Correct |
132 ms |
21864 KB |
Output is correct |
27 |
Correct |
138 ms |
21792 KB |
Output is correct |
28 |
Correct |
140 ms |
22132 KB |
Output is correct |
29 |
Correct |
140 ms |
21872 KB |
Output is correct |
30 |
Correct |
138 ms |
21864 KB |
Output is correct |
31 |
Correct |
2823 ms |
66004 KB |
Output is correct |
32 |
Correct |
2621 ms |
71216 KB |
Output is correct |
33 |
Correct |
2360 ms |
66932 KB |
Output is correct |
34 |
Correct |
2266 ms |
70860 KB |
Output is correct |
35 |
Correct |
3245 ms |
61352 KB |
Output is correct |
36 |
Correct |
3382 ms |
64788 KB |
Output is correct |
37 |
Correct |
2879 ms |
71220 KB |
Output is correct |
38 |
Correct |
78 ms |
15372 KB |
Output is correct |
39 |
Correct |
144 ms |
22608 KB |
Output is correct |
40 |
Correct |
123 ms |
21964 KB |
Output is correct |
41 |
Correct |
175 ms |
22044 KB |
Output is correct |
42 |
Correct |
9 ms |
14420 KB |
Output is correct |
43 |
Correct |
136 ms |
21892 KB |
Output is correct |
44 |
Correct |
129 ms |
21952 KB |
Output is correct |
45 |
Correct |
137 ms |
21852 KB |
Output is correct |
46 |
Correct |
127 ms |
21900 KB |
Output is correct |
47 |
Correct |
135 ms |
22112 KB |
Output is correct |
48 |
Correct |
121 ms |
21916 KB |
Output is correct |
49 |
Correct |
129 ms |
21928 KB |
Output is correct |
50 |
Correct |
2928 ms |
65932 KB |
Output is correct |
51 |
Correct |
2637 ms |
71232 KB |
Output is correct |
52 |
Correct |
2590 ms |
66928 KB |
Output is correct |
53 |
Correct |
2243 ms |
70876 KB |
Output is correct |
54 |
Correct |
3327 ms |
61408 KB |
Output is correct |
55 |
Correct |
4034 ms |
64940 KB |
Output is correct |
56 |
Correct |
2792 ms |
71296 KB |
Output is correct |
57 |
Correct |
3119 ms |
79900 KB |
Output is correct |
58 |
Correct |
3163 ms |
94612 KB |
Output is correct |
59 |
Correct |
2937 ms |
78304 KB |
Output is correct |
60 |
Correct |
2402 ms |
64008 KB |
Output is correct |
61 |
Execution timed out |
6058 ms |
80832 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |