#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define L(i,j,k) for(int i=(j);i<=(k);++i)
#define R(i,j,k) for(int i=(j);i>=(k);--i)
#define rep(i, n) L(i, 1, n)
#define all(x) x.begin(),x.end()
#define me(x,a) memset(x,a,sizeof(x))
#include<random>
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int nax = 100005;
int N, M, K, a[nax], dis[nax][126], vis[nax][126];
int nxt[nax * 2], head[nax * 2], ver[nax * 2], tot = 0;
void add_edge(int u, int v) {
++tot;
ver[tot] = v;
nxt[tot] = head[u];
head[u] = tot;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
/* Given a graph of N vertices and M edges
* We are also given K guards where the guard will petrol a -> b -> c -> d -> a -> ...
* Print the minimum time for which we can reach node N or print impossible
*/
cin >> N >> M;
L(i, 1, M) {
int u, v; cin >> u >> v;
add_edge(u, v);
add_edge(v, u);
}
int X; cin >> X;
cin >> K; L(i, 0, K - 1) cin >> a[i];
queue<pair<int,int>> Q;
L(i, 1, N) L(j, 0, K - 1) { vis[i][j] = false; dis[i][j] = 1E9;}
Q.push(make_pair(1, 0));
dis[1][0] = 0;
while (Q.size()) {
pair<int,int> top = Q.front(); Q.pop();
int u = top.first, i = top.second;
if (vis[u][i]) continue;
vis[u][i] = true;
int guard_position = a[(i + 1) % K], next_ind = (i + 1) % K;
if (guard_position != u && dis[u][next_ind] > dis[u][i]) {
dis[u][next_ind] = dis[u][i] + 1;
Q.push(make_pair(u, next_ind));
}
for (int P = head[u]; P; P = nxt[P]) {
int v = ver[P];
if (guard_position == u && a[i] == v) continue;
if (guard_position == v) continue;
if (dis[v][next_ind] > dis[u][i] + 1) {
dis[v][next_ind] = dis[u][i] + 1;
Q.push(make_pair(v, next_ind));
}
}
}
int ans = 1E9;
L(i, 0, K - 1) ans = min(ans, dis[N][i]);
if (ans == 1E9) cout << "impossible\n";
else cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
5832 KB |
Output is correct |
2 |
Correct |
515 ms |
102404 KB |
Output is correct |
3 |
Correct |
699 ms |
92956 KB |
Output is correct |
4 |
Correct |
1173 ms |
93304 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
794 ms |
93264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
5828 KB |
Output is correct |
2 |
Correct |
501 ms |
102448 KB |
Output is correct |
3 |
Correct |
743 ms |
92960 KB |
Output is correct |
4 |
Correct |
1189 ms |
93316 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
823 ms |
92912 KB |
Output is correct |
7 |
Incorrect |
336 ms |
93008 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
5828 KB |
Output is correct |
2 |
Correct |
501 ms |
102448 KB |
Output is correct |
3 |
Correct |
743 ms |
92960 KB |
Output is correct |
4 |
Correct |
1189 ms |
93316 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
823 ms |
92912 KB |
Output is correct |
7 |
Incorrect |
336 ms |
93008 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
5828 KB |
Output is correct |
2 |
Correct |
501 ms |
102448 KB |
Output is correct |
3 |
Correct |
743 ms |
92960 KB |
Output is correct |
4 |
Correct |
1189 ms |
93316 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
823 ms |
92912 KB |
Output is correct |
7 |
Incorrect |
336 ms |
93008 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
5832 KB |
Output is correct |
2 |
Correct |
515 ms |
102404 KB |
Output is correct |
3 |
Correct |
699 ms |
92956 KB |
Output is correct |
4 |
Correct |
1173 ms |
93304 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
794 ms |
93264 KB |
Output is correct |
7 |
Correct |
229 ms |
5828 KB |
Output is correct |
8 |
Correct |
501 ms |
102448 KB |
Output is correct |
9 |
Correct |
743 ms |
92960 KB |
Output is correct |
10 |
Correct |
1189 ms |
93316 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
823 ms |
92912 KB |
Output is correct |
13 |
Incorrect |
336 ms |
93008 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
5832 KB |
Output is correct |
2 |
Correct |
515 ms |
102404 KB |
Output is correct |
3 |
Correct |
699 ms |
92956 KB |
Output is correct |
4 |
Correct |
1173 ms |
93304 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
794 ms |
93264 KB |
Output is correct |
7 |
Correct |
229 ms |
5828 KB |
Output is correct |
8 |
Correct |
501 ms |
102448 KB |
Output is correct |
9 |
Correct |
743 ms |
92960 KB |
Output is correct |
10 |
Correct |
1189 ms |
93316 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
823 ms |
92912 KB |
Output is correct |
13 |
Incorrect |
336 ms |
93008 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
5832 KB |
Output is correct |
2 |
Correct |
515 ms |
102404 KB |
Output is correct |
3 |
Correct |
699 ms |
92956 KB |
Output is correct |
4 |
Correct |
1173 ms |
93304 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
794 ms |
93264 KB |
Output is correct |
7 |
Correct |
229 ms |
5828 KB |
Output is correct |
8 |
Correct |
501 ms |
102448 KB |
Output is correct |
9 |
Correct |
743 ms |
92960 KB |
Output is correct |
10 |
Correct |
1189 ms |
93316 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
823 ms |
92912 KB |
Output is correct |
13 |
Incorrect |
336 ms |
93008 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |