#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
using namespace std;
typedef long long ll;
const ll MAX = 1LL << 60;
using pii = pair<int, int>;
ll iceil(ll a, ll b){
return (a + b - 1) / b;
}
int n, m, k;
vector<int> len;
vector<int> rid; // route id
vector<int> id; // id in route
vector<vector<int>> g, tg, rg;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
rid.resize(n + 1);
id.resize(n + 1);
g.resize(n + 1);
tg.resize(n + 1);
rg.resize(n + 1);
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
vector<vector<ll>> dis(n + 1, vector<ll>(1, MAX));
cin >> k;
len.resize(k + 1);
len[0] = 1;
for(int i = 1; i <= k; i++){
cin >> len[i];
for(int j = 0; j < len[i]; j++){
int v;
cin >> v;
rid[v] = i;
id[v] = j;
dis[v].resize(len[i], MAX);
}
}
for(int i = 1; i <= n; i++){
for(int j : g[i]){
if(rid[j] == 0) tg[i].eb(j);
else rg[i].eb(j);
}
}
dis[1][0] = 0;
priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<>> pq;
pq.push(mp(0, mp(1, 0)));
vector<bool> vst(n + 1);
while(!pq.empty()){
ll d = pq.top().F;
int now = pq.top().S.F;
int t = pq.top().S.S;
pq.pop();
if(d != dis[now][t]) continue;
if(!vst[now]){
for(int i : tg[now]){
if(d + 1 < dis[i][0]){ dis[i][0] = d + 1;
pq.push(mp(d + 1, mp(i, 0)));
}
}
}
vst[now] = true;
// stay
if(rid[now] != 0 && (t + 1) % len[rid[now]] != id[now]){
int tmp = (t + 1) % len[rid[now]];
if(d + 1 < dis[now][tmp]){
dis[now][tmp] = d + 1;
pq.push(mp(d + 1, mp(now, tmp)));
}
}
// move
for(int i : rg[now]){
if(rid[now] == 0){
int to = (d + 1) % len[rid[i]];
if(to != id[i]){
if(d + 1 < dis[i][to]){
dis[i][to] = d + 1;
pq.push(mp(d + 1, mp(i, to)));
}
}
to = (id[i] + 1) % len[rid[i]];
ll nd = ((d + 1) / len[rid[i]]) * len[rid[i]] + to;
if(nd < d + 1) nd += len[rid[i]];
if(nd < dis[i][to]){
dis[i][to] = nd;
pq.push(mp(nd, mp(i, to)));
}
}
else if(rid[now] == rid[i]){
int to = (d + 1) % len[rid[i]];
if(to == id[i]) continue;
if(to == id[now] && t == id[i]) continue;
if(d + 1 < dis[i][to]){
dis[i][to] = d + 1;
pq.push(mp(d + 1, mp(i, to)));
}
}
else assert(false);
}
}
if(dis[n][0] != MAX) cout << dis[n][0] << "\n";
else cout << "impossible\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2252 KB |
Output is correct |
2 |
Correct |
104 ms |
20100 KB |
Output is correct |
3 |
Correct |
101 ms |
18276 KB |
Output is correct |
4 |
Correct |
119 ms |
18540 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
106 ms |
18248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2348 KB |
Output is correct |
2 |
Correct |
122 ms |
20012 KB |
Output is correct |
3 |
Correct |
108 ms |
18244 KB |
Output is correct |
4 |
Correct |
126 ms |
18580 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
102 ms |
18328 KB |
Output is correct |
7 |
Correct |
101 ms |
18348 KB |
Output is correct |
8 |
Correct |
100 ms |
18272 KB |
Output is correct |
9 |
Correct |
104 ms |
18184 KB |
Output is correct |
10 |
Correct |
104 ms |
18604 KB |
Output is correct |
11 |
Correct |
124 ms |
18488 KB |
Output is correct |
12 |
Correct |
103 ms |
18152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2348 KB |
Output is correct |
2 |
Correct |
122 ms |
20012 KB |
Output is correct |
3 |
Correct |
108 ms |
18244 KB |
Output is correct |
4 |
Correct |
126 ms |
18580 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
102 ms |
18328 KB |
Output is correct |
7 |
Correct |
101 ms |
18348 KB |
Output is correct |
8 |
Correct |
100 ms |
18272 KB |
Output is correct |
9 |
Correct |
104 ms |
18184 KB |
Output is correct |
10 |
Correct |
104 ms |
18604 KB |
Output is correct |
11 |
Correct |
124 ms |
18488 KB |
Output is correct |
12 |
Correct |
103 ms |
18152 KB |
Output is correct |
13 |
Correct |
31 ms |
2364 KB |
Output is correct |
14 |
Correct |
100 ms |
19956 KB |
Output is correct |
15 |
Correct |
98 ms |
18284 KB |
Output is correct |
16 |
Correct |
114 ms |
18628 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
102 ms |
18312 KB |
Output is correct |
19 |
Correct |
99 ms |
18268 KB |
Output is correct |
20 |
Correct |
96 ms |
18308 KB |
Output is correct |
21 |
Correct |
115 ms |
18116 KB |
Output is correct |
22 |
Correct |
103 ms |
18592 KB |
Output is correct |
23 |
Correct |
100 ms |
18364 KB |
Output is correct |
24 |
Correct |
99 ms |
18256 KB |
Output is correct |
25 |
Correct |
1894 ms |
112224 KB |
Output is correct |
26 |
Correct |
1860 ms |
120660 KB |
Output is correct |
27 |
Correct |
1695 ms |
112596 KB |
Output is correct |
28 |
Correct |
1416 ms |
121264 KB |
Output is correct |
29 |
Correct |
1775 ms |
106336 KB |
Output is correct |
30 |
Correct |
1877 ms |
147136 KB |
Output is correct |
31 |
Correct |
1990 ms |
158420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2348 KB |
Output is correct |
2 |
Correct |
122 ms |
20012 KB |
Output is correct |
3 |
Correct |
108 ms |
18244 KB |
Output is correct |
4 |
Correct |
126 ms |
18580 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
102 ms |
18328 KB |
Output is correct |
7 |
Correct |
101 ms |
18348 KB |
Output is correct |
8 |
Correct |
100 ms |
18272 KB |
Output is correct |
9 |
Correct |
104 ms |
18184 KB |
Output is correct |
10 |
Correct |
104 ms |
18604 KB |
Output is correct |
11 |
Correct |
124 ms |
18488 KB |
Output is correct |
12 |
Correct |
103 ms |
18152 KB |
Output is correct |
13 |
Correct |
31 ms |
2364 KB |
Output is correct |
14 |
Correct |
100 ms |
19956 KB |
Output is correct |
15 |
Correct |
98 ms |
18284 KB |
Output is correct |
16 |
Correct |
114 ms |
18628 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
102 ms |
18312 KB |
Output is correct |
19 |
Correct |
99 ms |
18268 KB |
Output is correct |
20 |
Correct |
96 ms |
18308 KB |
Output is correct |
21 |
Correct |
115 ms |
18116 KB |
Output is correct |
22 |
Correct |
103 ms |
18592 KB |
Output is correct |
23 |
Correct |
100 ms |
18364 KB |
Output is correct |
24 |
Correct |
99 ms |
18256 KB |
Output is correct |
25 |
Correct |
1894 ms |
112224 KB |
Output is correct |
26 |
Correct |
1860 ms |
120660 KB |
Output is correct |
27 |
Correct |
1695 ms |
112596 KB |
Output is correct |
28 |
Correct |
1416 ms |
121264 KB |
Output is correct |
29 |
Correct |
1775 ms |
106336 KB |
Output is correct |
30 |
Correct |
1877 ms |
147136 KB |
Output is correct |
31 |
Correct |
1990 ms |
158420 KB |
Output is correct |
32 |
Correct |
31 ms |
2880 KB |
Output is correct |
33 |
Correct |
132 ms |
21204 KB |
Output is correct |
34 |
Correct |
120 ms |
19396 KB |
Output is correct |
35 |
Correct |
134 ms |
19652 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
129 ms |
19480 KB |
Output is correct |
38 |
Correct |
102 ms |
19400 KB |
Output is correct |
39 |
Correct |
95 ms |
19412 KB |
Output is correct |
40 |
Correct |
97 ms |
19268 KB |
Output is correct |
41 |
Correct |
112 ms |
19796 KB |
Output is correct |
42 |
Correct |
101 ms |
19644 KB |
Output is correct |
43 |
Correct |
110 ms |
19500 KB |
Output is correct |
44 |
Correct |
2007 ms |
150720 KB |
Output is correct |
45 |
Correct |
1786 ms |
159212 KB |
Output is correct |
46 |
Correct |
1648 ms |
150956 KB |
Output is correct |
47 |
Correct |
1447 ms |
159856 KB |
Output is correct |
48 |
Correct |
1846 ms |
145164 KB |
Output is correct |
49 |
Correct |
1844 ms |
147168 KB |
Output is correct |
50 |
Correct |
1955 ms |
158468 KB |
Output is correct |
51 |
Correct |
2475 ms |
167684 KB |
Output is correct |
52 |
Correct |
2789 ms |
189300 KB |
Output is correct |
53 |
Correct |
2327 ms |
169232 KB |
Output is correct |
54 |
Correct |
1327 ms |
142400 KB |
Output is correct |
55 |
Execution timed out |
6075 ms |
173212 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2252 KB |
Output is correct |
2 |
Correct |
104 ms |
20100 KB |
Output is correct |
3 |
Correct |
101 ms |
18276 KB |
Output is correct |
4 |
Correct |
119 ms |
18540 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
106 ms |
18248 KB |
Output is correct |
7 |
Correct |
31 ms |
2348 KB |
Output is correct |
8 |
Correct |
122 ms |
20012 KB |
Output is correct |
9 |
Correct |
108 ms |
18244 KB |
Output is correct |
10 |
Correct |
126 ms |
18580 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
102 ms |
18328 KB |
Output is correct |
13 |
Correct |
101 ms |
18348 KB |
Output is correct |
14 |
Correct |
100 ms |
18272 KB |
Output is correct |
15 |
Correct |
104 ms |
18184 KB |
Output is correct |
16 |
Correct |
104 ms |
18604 KB |
Output is correct |
17 |
Correct |
124 ms |
18488 KB |
Output is correct |
18 |
Correct |
103 ms |
18152 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2252 KB |
Output is correct |
2 |
Correct |
104 ms |
20100 KB |
Output is correct |
3 |
Correct |
101 ms |
18276 KB |
Output is correct |
4 |
Correct |
119 ms |
18540 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
106 ms |
18248 KB |
Output is correct |
7 |
Correct |
31 ms |
2348 KB |
Output is correct |
8 |
Correct |
122 ms |
20012 KB |
Output is correct |
9 |
Correct |
108 ms |
18244 KB |
Output is correct |
10 |
Correct |
126 ms |
18580 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
102 ms |
18328 KB |
Output is correct |
13 |
Correct |
101 ms |
18348 KB |
Output is correct |
14 |
Correct |
100 ms |
18272 KB |
Output is correct |
15 |
Correct |
104 ms |
18184 KB |
Output is correct |
16 |
Correct |
104 ms |
18604 KB |
Output is correct |
17 |
Correct |
124 ms |
18488 KB |
Output is correct |
18 |
Correct |
103 ms |
18152 KB |
Output is correct |
19 |
Correct |
31 ms |
2364 KB |
Output is correct |
20 |
Correct |
100 ms |
19956 KB |
Output is correct |
21 |
Correct |
98 ms |
18284 KB |
Output is correct |
22 |
Correct |
114 ms |
18628 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
102 ms |
18312 KB |
Output is correct |
25 |
Correct |
99 ms |
18268 KB |
Output is correct |
26 |
Correct |
96 ms |
18308 KB |
Output is correct |
27 |
Correct |
115 ms |
18116 KB |
Output is correct |
28 |
Correct |
103 ms |
18592 KB |
Output is correct |
29 |
Correct |
100 ms |
18364 KB |
Output is correct |
30 |
Correct |
99 ms |
18256 KB |
Output is correct |
31 |
Correct |
1894 ms |
112224 KB |
Output is correct |
32 |
Correct |
1860 ms |
120660 KB |
Output is correct |
33 |
Correct |
1695 ms |
112596 KB |
Output is correct |
34 |
Correct |
1416 ms |
121264 KB |
Output is correct |
35 |
Correct |
1775 ms |
106336 KB |
Output is correct |
36 |
Correct |
1877 ms |
147136 KB |
Output is correct |
37 |
Correct |
1990 ms |
158420 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
41 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2252 KB |
Output is correct |
2 |
Correct |
104 ms |
20100 KB |
Output is correct |
3 |
Correct |
101 ms |
18276 KB |
Output is correct |
4 |
Correct |
119 ms |
18540 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
106 ms |
18248 KB |
Output is correct |
7 |
Correct |
31 ms |
2348 KB |
Output is correct |
8 |
Correct |
122 ms |
20012 KB |
Output is correct |
9 |
Correct |
108 ms |
18244 KB |
Output is correct |
10 |
Correct |
126 ms |
18580 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
102 ms |
18328 KB |
Output is correct |
13 |
Correct |
101 ms |
18348 KB |
Output is correct |
14 |
Correct |
100 ms |
18272 KB |
Output is correct |
15 |
Correct |
104 ms |
18184 KB |
Output is correct |
16 |
Correct |
104 ms |
18604 KB |
Output is correct |
17 |
Correct |
124 ms |
18488 KB |
Output is correct |
18 |
Correct |
103 ms |
18152 KB |
Output is correct |
19 |
Correct |
31 ms |
2364 KB |
Output is correct |
20 |
Correct |
100 ms |
19956 KB |
Output is correct |
21 |
Correct |
98 ms |
18284 KB |
Output is correct |
22 |
Correct |
114 ms |
18628 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
102 ms |
18312 KB |
Output is correct |
25 |
Correct |
99 ms |
18268 KB |
Output is correct |
26 |
Correct |
96 ms |
18308 KB |
Output is correct |
27 |
Correct |
115 ms |
18116 KB |
Output is correct |
28 |
Correct |
103 ms |
18592 KB |
Output is correct |
29 |
Correct |
100 ms |
18364 KB |
Output is correct |
30 |
Correct |
99 ms |
18256 KB |
Output is correct |
31 |
Correct |
1894 ms |
112224 KB |
Output is correct |
32 |
Correct |
1860 ms |
120660 KB |
Output is correct |
33 |
Correct |
1695 ms |
112596 KB |
Output is correct |
34 |
Correct |
1416 ms |
121264 KB |
Output is correct |
35 |
Correct |
1775 ms |
106336 KB |
Output is correct |
36 |
Correct |
1877 ms |
147136 KB |
Output is correct |
37 |
Correct |
1990 ms |
158420 KB |
Output is correct |
38 |
Correct |
31 ms |
2880 KB |
Output is correct |
39 |
Correct |
132 ms |
21204 KB |
Output is correct |
40 |
Correct |
120 ms |
19396 KB |
Output is correct |
41 |
Correct |
134 ms |
19652 KB |
Output is correct |
42 |
Correct |
2 ms |
460 KB |
Output is correct |
43 |
Correct |
129 ms |
19480 KB |
Output is correct |
44 |
Correct |
102 ms |
19400 KB |
Output is correct |
45 |
Correct |
95 ms |
19412 KB |
Output is correct |
46 |
Correct |
97 ms |
19268 KB |
Output is correct |
47 |
Correct |
112 ms |
19796 KB |
Output is correct |
48 |
Correct |
101 ms |
19644 KB |
Output is correct |
49 |
Correct |
110 ms |
19500 KB |
Output is correct |
50 |
Correct |
2007 ms |
150720 KB |
Output is correct |
51 |
Correct |
1786 ms |
159212 KB |
Output is correct |
52 |
Correct |
1648 ms |
150956 KB |
Output is correct |
53 |
Correct |
1447 ms |
159856 KB |
Output is correct |
54 |
Correct |
1846 ms |
145164 KB |
Output is correct |
55 |
Correct |
1844 ms |
147168 KB |
Output is correct |
56 |
Correct |
1955 ms |
158468 KB |
Output is correct |
57 |
Correct |
2475 ms |
167684 KB |
Output is correct |
58 |
Correct |
2789 ms |
189300 KB |
Output is correct |
59 |
Correct |
2327 ms |
169232 KB |
Output is correct |
60 |
Correct |
1327 ms |
142400 KB |
Output is correct |
61 |
Execution timed out |
6075 ms |
173212 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |