#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e16;
int dist[100001][126];
int arr[126];
int n;
vector <int> adj[100001];
signed main () {
ios::sync_with_stdio(0);
cin.tie(0);
int m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int l; cin >> l >> l;
for (int i = 0; i < l; i++) cin >> arr[i];
for (int i = 0; i < 125; i++) {
for (int j = 1; j <= n; j++) {
dist[j][i] = inf;
}
}
dist[1][0] = 0;
priority_queue <vector <int>, vector <vector <int>>, greater <vector <int>>> pq;
pq.push({0, 0, 1});
while (!pq.empty()) {
auto k = pq.top();
pq.pop();
if (arr[(k[1] + 1) % l] != k[2]) {
if (dist[k[2]][(k[1] + 1) % l] > k[0] + 1) {
dist[k[2]][(k[1] + 1) % l] = k[0] + 1;
pq.push({k[0] + 1, (k[1] + 1) % l, k[2]});
}
}
for (auto j : adj[k[2]]) {
if (j == arr[(k[1] + 1) % l] || (j == arr[k[1]] && k[2] == arr[(k[1] + 1) % l])) continue;
if (dist[j][(k[1] + 1) % l] > k[0] + 1) {
dist[j][(k[1] + 1) % l] = k[0] + 1;
pq.push({k[0] + 1, (k[1] + 1) % l, j});
}
}
}
int mn = inf;
for (int i = 0; i < l; i++) {
mn = min(mn, dist[n][i]);
}
if (mn >= inf) cout << "impossible\n";
else cout << mn << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
7184 KB |
Output is correct |
2 |
Correct |
2021 ms |
105720 KB |
Output is correct |
3 |
Correct |
2356 ms |
99848 KB |
Output is correct |
4 |
Correct |
2984 ms |
101140 KB |
Output is correct |
5 |
Correct |
4 ms |
4696 KB |
Output is correct |
6 |
Correct |
2490 ms |
99760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
7000 KB |
Output is correct |
2 |
Correct |
1989 ms |
105712 KB |
Output is correct |
3 |
Correct |
2341 ms |
99736 KB |
Output is correct |
4 |
Correct |
3074 ms |
100900 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
2522 ms |
99684 KB |
Output is correct |
7 |
Incorrect |
995 ms |
98444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
7000 KB |
Output is correct |
2 |
Correct |
1989 ms |
105712 KB |
Output is correct |
3 |
Correct |
2341 ms |
99736 KB |
Output is correct |
4 |
Correct |
3074 ms |
100900 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
2522 ms |
99684 KB |
Output is correct |
7 |
Incorrect |
995 ms |
98444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
7000 KB |
Output is correct |
2 |
Correct |
1989 ms |
105712 KB |
Output is correct |
3 |
Correct |
2341 ms |
99736 KB |
Output is correct |
4 |
Correct |
3074 ms |
100900 KB |
Output is correct |
5 |
Correct |
3 ms |
4700 KB |
Output is correct |
6 |
Correct |
2522 ms |
99684 KB |
Output is correct |
7 |
Incorrect |
995 ms |
98444 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
7184 KB |
Output is correct |
2 |
Correct |
2021 ms |
105720 KB |
Output is correct |
3 |
Correct |
2356 ms |
99848 KB |
Output is correct |
4 |
Correct |
2984 ms |
101140 KB |
Output is correct |
5 |
Correct |
4 ms |
4696 KB |
Output is correct |
6 |
Correct |
2490 ms |
99760 KB |
Output is correct |
7 |
Correct |
104 ms |
7000 KB |
Output is correct |
8 |
Correct |
1989 ms |
105712 KB |
Output is correct |
9 |
Correct |
2341 ms |
99736 KB |
Output is correct |
10 |
Correct |
3074 ms |
100900 KB |
Output is correct |
11 |
Correct |
3 ms |
4700 KB |
Output is correct |
12 |
Correct |
2522 ms |
99684 KB |
Output is correct |
13 |
Incorrect |
995 ms |
98444 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
7184 KB |
Output is correct |
2 |
Correct |
2021 ms |
105720 KB |
Output is correct |
3 |
Correct |
2356 ms |
99848 KB |
Output is correct |
4 |
Correct |
2984 ms |
101140 KB |
Output is correct |
5 |
Correct |
4 ms |
4696 KB |
Output is correct |
6 |
Correct |
2490 ms |
99760 KB |
Output is correct |
7 |
Correct |
104 ms |
7000 KB |
Output is correct |
8 |
Correct |
1989 ms |
105712 KB |
Output is correct |
9 |
Correct |
2341 ms |
99736 KB |
Output is correct |
10 |
Correct |
3074 ms |
100900 KB |
Output is correct |
11 |
Correct |
3 ms |
4700 KB |
Output is correct |
12 |
Correct |
2522 ms |
99684 KB |
Output is correct |
13 |
Incorrect |
995 ms |
98444 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
7184 KB |
Output is correct |
2 |
Correct |
2021 ms |
105720 KB |
Output is correct |
3 |
Correct |
2356 ms |
99848 KB |
Output is correct |
4 |
Correct |
2984 ms |
101140 KB |
Output is correct |
5 |
Correct |
4 ms |
4696 KB |
Output is correct |
6 |
Correct |
2490 ms |
99760 KB |
Output is correct |
7 |
Correct |
104 ms |
7000 KB |
Output is correct |
8 |
Correct |
1989 ms |
105712 KB |
Output is correct |
9 |
Correct |
2341 ms |
99736 KB |
Output is correct |
10 |
Correct |
3074 ms |
100900 KB |
Output is correct |
11 |
Correct |
3 ms |
4700 KB |
Output is correct |
12 |
Correct |
2522 ms |
99684 KB |
Output is correct |
13 |
Incorrect |
995 ms |
98444 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |