#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;
if(rid[now] == 0){
for(int i : g[now]) g[i].erase(now);
}
// 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
7984 KB |
Output is correct |
2 |
Correct |
134 ms |
20892 KB |
Output is correct |
3 |
Correct |
142 ms |
19836 KB |
Output is correct |
4 |
Correct |
206 ms |
19896 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
143 ms |
19816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
7948 KB |
Output is correct |
2 |
Correct |
127 ms |
20844 KB |
Output is correct |
3 |
Correct |
133 ms |
19948 KB |
Output is correct |
4 |
Correct |
208 ms |
19824 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
142 ms |
20028 KB |
Output is correct |
7 |
Correct |
139 ms |
19772 KB |
Output is correct |
8 |
Correct |
130 ms |
19708 KB |
Output is correct |
9 |
Correct |
132 ms |
19688 KB |
Output is correct |
10 |
Correct |
151 ms |
19972 KB |
Output is correct |
11 |
Correct |
126 ms |
19780 KB |
Output is correct |
12 |
Correct |
135 ms |
19840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
7948 KB |
Output is correct |
2 |
Correct |
127 ms |
20844 KB |
Output is correct |
3 |
Correct |
133 ms |
19948 KB |
Output is correct |
4 |
Correct |
208 ms |
19824 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
142 ms |
20028 KB |
Output is correct |
7 |
Correct |
139 ms |
19772 KB |
Output is correct |
8 |
Correct |
130 ms |
19708 KB |
Output is correct |
9 |
Correct |
132 ms |
19688 KB |
Output is correct |
10 |
Correct |
151 ms |
19972 KB |
Output is correct |
11 |
Correct |
126 ms |
19780 KB |
Output is correct |
12 |
Correct |
135 ms |
19840 KB |
Output is correct |
13 |
Correct |
163 ms |
7992 KB |
Output is correct |
14 |
Correct |
143 ms |
20676 KB |
Output is correct |
15 |
Correct |
125 ms |
19836 KB |
Output is correct |
16 |
Correct |
218 ms |
19916 KB |
Output is correct |
17 |
Correct |
3 ms |
460 KB |
Output is correct |
18 |
Correct |
138 ms |
19912 KB |
Output is correct |
19 |
Correct |
134 ms |
19824 KB |
Output is correct |
20 |
Correct |
128 ms |
19696 KB |
Output is correct |
21 |
Correct |
158 ms |
19688 KB |
Output is correct |
22 |
Correct |
143 ms |
20012 KB |
Output is correct |
23 |
Correct |
136 ms |
19728 KB |
Output is correct |
24 |
Correct |
135 ms |
19780 KB |
Output is correct |
25 |
Execution timed out |
6039 ms |
309784 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
7948 KB |
Output is correct |
2 |
Correct |
127 ms |
20844 KB |
Output is correct |
3 |
Correct |
133 ms |
19948 KB |
Output is correct |
4 |
Correct |
208 ms |
19824 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
142 ms |
20028 KB |
Output is correct |
7 |
Correct |
139 ms |
19772 KB |
Output is correct |
8 |
Correct |
130 ms |
19708 KB |
Output is correct |
9 |
Correct |
132 ms |
19688 KB |
Output is correct |
10 |
Correct |
151 ms |
19972 KB |
Output is correct |
11 |
Correct |
126 ms |
19780 KB |
Output is correct |
12 |
Correct |
135 ms |
19840 KB |
Output is correct |
13 |
Correct |
163 ms |
7992 KB |
Output is correct |
14 |
Correct |
143 ms |
20676 KB |
Output is correct |
15 |
Correct |
125 ms |
19836 KB |
Output is correct |
16 |
Correct |
218 ms |
19916 KB |
Output is correct |
17 |
Correct |
3 ms |
460 KB |
Output is correct |
18 |
Correct |
138 ms |
19912 KB |
Output is correct |
19 |
Correct |
134 ms |
19824 KB |
Output is correct |
20 |
Correct |
128 ms |
19696 KB |
Output is correct |
21 |
Correct |
158 ms |
19688 KB |
Output is correct |
22 |
Correct |
143 ms |
20012 KB |
Output is correct |
23 |
Correct |
136 ms |
19728 KB |
Output is correct |
24 |
Correct |
135 ms |
19780 KB |
Output is correct |
25 |
Execution timed out |
6039 ms |
309784 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
7984 KB |
Output is correct |
2 |
Correct |
134 ms |
20892 KB |
Output is correct |
3 |
Correct |
142 ms |
19836 KB |
Output is correct |
4 |
Correct |
206 ms |
19896 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
143 ms |
19816 KB |
Output is correct |
7 |
Correct |
165 ms |
7948 KB |
Output is correct |
8 |
Correct |
127 ms |
20844 KB |
Output is correct |
9 |
Correct |
133 ms |
19948 KB |
Output is correct |
10 |
Correct |
208 ms |
19824 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
142 ms |
20028 KB |
Output is correct |
13 |
Correct |
139 ms |
19772 KB |
Output is correct |
14 |
Correct |
130 ms |
19708 KB |
Output is correct |
15 |
Correct |
132 ms |
19688 KB |
Output is correct |
16 |
Correct |
151 ms |
19972 KB |
Output is correct |
17 |
Correct |
126 ms |
19780 KB |
Output is correct |
18 |
Correct |
135 ms |
19840 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 |
159 ms |
7984 KB |
Output is correct |
2 |
Correct |
134 ms |
20892 KB |
Output is correct |
3 |
Correct |
142 ms |
19836 KB |
Output is correct |
4 |
Correct |
206 ms |
19896 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
143 ms |
19816 KB |
Output is correct |
7 |
Correct |
165 ms |
7948 KB |
Output is correct |
8 |
Correct |
127 ms |
20844 KB |
Output is correct |
9 |
Correct |
133 ms |
19948 KB |
Output is correct |
10 |
Correct |
208 ms |
19824 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
142 ms |
20028 KB |
Output is correct |
13 |
Correct |
139 ms |
19772 KB |
Output is correct |
14 |
Correct |
130 ms |
19708 KB |
Output is correct |
15 |
Correct |
132 ms |
19688 KB |
Output is correct |
16 |
Correct |
151 ms |
19972 KB |
Output is correct |
17 |
Correct |
126 ms |
19780 KB |
Output is correct |
18 |
Correct |
135 ms |
19840 KB |
Output is correct |
19 |
Correct |
163 ms |
7992 KB |
Output is correct |
20 |
Correct |
143 ms |
20676 KB |
Output is correct |
21 |
Correct |
125 ms |
19836 KB |
Output is correct |
22 |
Correct |
218 ms |
19916 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
138 ms |
19912 KB |
Output is correct |
25 |
Correct |
134 ms |
19824 KB |
Output is correct |
26 |
Correct |
128 ms |
19696 KB |
Output is correct |
27 |
Correct |
158 ms |
19688 KB |
Output is correct |
28 |
Correct |
143 ms |
20012 KB |
Output is correct |
29 |
Correct |
136 ms |
19728 KB |
Output is correct |
30 |
Correct |
135 ms |
19780 KB |
Output is correct |
31 |
Execution timed out |
6039 ms |
309784 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
7984 KB |
Output is correct |
2 |
Correct |
134 ms |
20892 KB |
Output is correct |
3 |
Correct |
142 ms |
19836 KB |
Output is correct |
4 |
Correct |
206 ms |
19896 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
143 ms |
19816 KB |
Output is correct |
7 |
Correct |
165 ms |
7948 KB |
Output is correct |
8 |
Correct |
127 ms |
20844 KB |
Output is correct |
9 |
Correct |
133 ms |
19948 KB |
Output is correct |
10 |
Correct |
208 ms |
19824 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
142 ms |
20028 KB |
Output is correct |
13 |
Correct |
139 ms |
19772 KB |
Output is correct |
14 |
Correct |
130 ms |
19708 KB |
Output is correct |
15 |
Correct |
132 ms |
19688 KB |
Output is correct |
16 |
Correct |
151 ms |
19972 KB |
Output is correct |
17 |
Correct |
126 ms |
19780 KB |
Output is correct |
18 |
Correct |
135 ms |
19840 KB |
Output is correct |
19 |
Correct |
163 ms |
7992 KB |
Output is correct |
20 |
Correct |
143 ms |
20676 KB |
Output is correct |
21 |
Correct |
125 ms |
19836 KB |
Output is correct |
22 |
Correct |
218 ms |
19916 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
138 ms |
19912 KB |
Output is correct |
25 |
Correct |
134 ms |
19824 KB |
Output is correct |
26 |
Correct |
128 ms |
19696 KB |
Output is correct |
27 |
Correct |
158 ms |
19688 KB |
Output is correct |
28 |
Correct |
143 ms |
20012 KB |
Output is correct |
29 |
Correct |
136 ms |
19728 KB |
Output is correct |
30 |
Correct |
135 ms |
19780 KB |
Output is correct |
31 |
Execution timed out |
6039 ms |
309784 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |