#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], head[nax], ver[nax], 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;
memset(dis, '?', sizeof dis);
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) {
if (dis[N][i] < ans)
ans = dis[N][i];
}
if (ans == 1E9) cout << "impossible\n";
else cout << ans << '\n';
//~ cout << "+++++\n";
//~ cout << dis[1][1] << '\n';
//~ cout << dis[2][2] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
52052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
52060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
52060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
52060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
52052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
52052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
52052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |