#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<set<int>> g;
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);
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
g[u].insert(v);
g[v].insert(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);
}
}
dis[1][0] = 0;
priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<>> pq;
pq.push(mp(0, mp(1, 0)));
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;
// 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 : g[now]){
if(rid[i] == 0){
if(d + 1 < dis[i][0]){
dis[i][0] = d + 1;
pq.push(mp(d + 1, mp(i, 0)));
}
}
else 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
378 ms |
7976 KB |
Output is correct |
2 |
Correct |
122 ms |
20728 KB |
Output is correct |
3 |
Correct |
144 ms |
19908 KB |
Output is correct |
4 |
Correct |
236 ms |
19980 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
125 ms |
19888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
7896 KB |
Output is correct |
2 |
Correct |
126 ms |
20788 KB |
Output is correct |
3 |
Correct |
129 ms |
19792 KB |
Output is correct |
4 |
Correct |
251 ms |
19976 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
146 ms |
20032 KB |
Output is correct |
7 |
Correct |
129 ms |
19732 KB |
Output is correct |
8 |
Correct |
116 ms |
19740 KB |
Output is correct |
9 |
Correct |
116 ms |
19580 KB |
Output is correct |
10 |
Correct |
178 ms |
19948 KB |
Output is correct |
11 |
Correct |
168 ms |
19792 KB |
Output is correct |
12 |
Correct |
131 ms |
19784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
7896 KB |
Output is correct |
2 |
Correct |
126 ms |
20788 KB |
Output is correct |
3 |
Correct |
129 ms |
19792 KB |
Output is correct |
4 |
Correct |
251 ms |
19976 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
146 ms |
20032 KB |
Output is correct |
7 |
Correct |
129 ms |
19732 KB |
Output is correct |
8 |
Correct |
116 ms |
19740 KB |
Output is correct |
9 |
Correct |
116 ms |
19580 KB |
Output is correct |
10 |
Correct |
178 ms |
19948 KB |
Output is correct |
11 |
Correct |
168 ms |
19792 KB |
Output is correct |
12 |
Correct |
131 ms |
19784 KB |
Output is correct |
13 |
Correct |
372 ms |
8076 KB |
Output is correct |
14 |
Correct |
135 ms |
20696 KB |
Output is correct |
15 |
Correct |
123 ms |
19804 KB |
Output is correct |
16 |
Correct |
237 ms |
19908 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
136 ms |
20032 KB |
Output is correct |
19 |
Correct |
129 ms |
19816 KB |
Output is correct |
20 |
Correct |
122 ms |
19780 KB |
Output is correct |
21 |
Correct |
120 ms |
19716 KB |
Output is correct |
22 |
Correct |
175 ms |
19916 KB |
Output is correct |
23 |
Correct |
163 ms |
19780 KB |
Output is correct |
24 |
Correct |
153 ms |
19828 KB |
Output is correct |
25 |
Execution timed out |
6069 ms |
310992 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
7896 KB |
Output is correct |
2 |
Correct |
126 ms |
20788 KB |
Output is correct |
3 |
Correct |
129 ms |
19792 KB |
Output is correct |
4 |
Correct |
251 ms |
19976 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
146 ms |
20032 KB |
Output is correct |
7 |
Correct |
129 ms |
19732 KB |
Output is correct |
8 |
Correct |
116 ms |
19740 KB |
Output is correct |
9 |
Correct |
116 ms |
19580 KB |
Output is correct |
10 |
Correct |
178 ms |
19948 KB |
Output is correct |
11 |
Correct |
168 ms |
19792 KB |
Output is correct |
12 |
Correct |
131 ms |
19784 KB |
Output is correct |
13 |
Correct |
372 ms |
8076 KB |
Output is correct |
14 |
Correct |
135 ms |
20696 KB |
Output is correct |
15 |
Correct |
123 ms |
19804 KB |
Output is correct |
16 |
Correct |
237 ms |
19908 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
136 ms |
20032 KB |
Output is correct |
19 |
Correct |
129 ms |
19816 KB |
Output is correct |
20 |
Correct |
122 ms |
19780 KB |
Output is correct |
21 |
Correct |
120 ms |
19716 KB |
Output is correct |
22 |
Correct |
175 ms |
19916 KB |
Output is correct |
23 |
Correct |
163 ms |
19780 KB |
Output is correct |
24 |
Correct |
153 ms |
19828 KB |
Output is correct |
25 |
Execution timed out |
6069 ms |
310992 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
378 ms |
7976 KB |
Output is correct |
2 |
Correct |
122 ms |
20728 KB |
Output is correct |
3 |
Correct |
144 ms |
19908 KB |
Output is correct |
4 |
Correct |
236 ms |
19980 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
125 ms |
19888 KB |
Output is correct |
7 |
Correct |
369 ms |
7896 KB |
Output is correct |
8 |
Correct |
126 ms |
20788 KB |
Output is correct |
9 |
Correct |
129 ms |
19792 KB |
Output is correct |
10 |
Correct |
251 ms |
19976 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
146 ms |
20032 KB |
Output is correct |
13 |
Correct |
129 ms |
19732 KB |
Output is correct |
14 |
Correct |
116 ms |
19740 KB |
Output is correct |
15 |
Correct |
116 ms |
19580 KB |
Output is correct |
16 |
Correct |
178 ms |
19948 KB |
Output is correct |
17 |
Correct |
168 ms |
19792 KB |
Output is correct |
18 |
Correct |
131 ms |
19784 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
378 ms |
7976 KB |
Output is correct |
2 |
Correct |
122 ms |
20728 KB |
Output is correct |
3 |
Correct |
144 ms |
19908 KB |
Output is correct |
4 |
Correct |
236 ms |
19980 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
125 ms |
19888 KB |
Output is correct |
7 |
Correct |
369 ms |
7896 KB |
Output is correct |
8 |
Correct |
126 ms |
20788 KB |
Output is correct |
9 |
Correct |
129 ms |
19792 KB |
Output is correct |
10 |
Correct |
251 ms |
19976 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
146 ms |
20032 KB |
Output is correct |
13 |
Correct |
129 ms |
19732 KB |
Output is correct |
14 |
Correct |
116 ms |
19740 KB |
Output is correct |
15 |
Correct |
116 ms |
19580 KB |
Output is correct |
16 |
Correct |
178 ms |
19948 KB |
Output is correct |
17 |
Correct |
168 ms |
19792 KB |
Output is correct |
18 |
Correct |
131 ms |
19784 KB |
Output is correct |
19 |
Correct |
372 ms |
8076 KB |
Output is correct |
20 |
Correct |
135 ms |
20696 KB |
Output is correct |
21 |
Correct |
123 ms |
19804 KB |
Output is correct |
22 |
Correct |
237 ms |
19908 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
136 ms |
20032 KB |
Output is correct |
25 |
Correct |
129 ms |
19816 KB |
Output is correct |
26 |
Correct |
122 ms |
19780 KB |
Output is correct |
27 |
Correct |
120 ms |
19716 KB |
Output is correct |
28 |
Correct |
175 ms |
19916 KB |
Output is correct |
29 |
Correct |
163 ms |
19780 KB |
Output is correct |
30 |
Correct |
153 ms |
19828 KB |
Output is correct |
31 |
Execution timed out |
6069 ms |
310992 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
378 ms |
7976 KB |
Output is correct |
2 |
Correct |
122 ms |
20728 KB |
Output is correct |
3 |
Correct |
144 ms |
19908 KB |
Output is correct |
4 |
Correct |
236 ms |
19980 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
125 ms |
19888 KB |
Output is correct |
7 |
Correct |
369 ms |
7896 KB |
Output is correct |
8 |
Correct |
126 ms |
20788 KB |
Output is correct |
9 |
Correct |
129 ms |
19792 KB |
Output is correct |
10 |
Correct |
251 ms |
19976 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
146 ms |
20032 KB |
Output is correct |
13 |
Correct |
129 ms |
19732 KB |
Output is correct |
14 |
Correct |
116 ms |
19740 KB |
Output is correct |
15 |
Correct |
116 ms |
19580 KB |
Output is correct |
16 |
Correct |
178 ms |
19948 KB |
Output is correct |
17 |
Correct |
168 ms |
19792 KB |
Output is correct |
18 |
Correct |
131 ms |
19784 KB |
Output is correct |
19 |
Correct |
372 ms |
8076 KB |
Output is correct |
20 |
Correct |
135 ms |
20696 KB |
Output is correct |
21 |
Correct |
123 ms |
19804 KB |
Output is correct |
22 |
Correct |
237 ms |
19908 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
136 ms |
20032 KB |
Output is correct |
25 |
Correct |
129 ms |
19816 KB |
Output is correct |
26 |
Correct |
122 ms |
19780 KB |
Output is correct |
27 |
Correct |
120 ms |
19716 KB |
Output is correct |
28 |
Correct |
175 ms |
19916 KB |
Output is correct |
29 |
Correct |
163 ms |
19780 KB |
Output is correct |
30 |
Correct |
153 ms |
19828 KB |
Output is correct |
31 |
Execution timed out |
6069 ms |
310992 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |