# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1224622 | sokratisi | From Hacks to Snitches (BOI21_watchmen) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, a, b, l;
vector<int> adj[100010];
int cl[130], dst[100010][130];
bool vis[100010][130];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
scanf("%d%d", &k, &l);
for (int i = 0; i < l; i++) scanf("%d", &cl[i]);
cl[l] = cl[0];
for (int i = 1; i <= n; i++) {
for (int j = 0; j < l; j++) {
dst[i][j] = -1;
}
}
dst[1][0] = 0;
queue<pair<int, int>> q;
q.push({1, 0});
// careful with staying still
while (!q.empty()) {
int node = q.front().first, tm = q.front().second;
q.pop();
if (vis[node][tm]) continue;
vis[node][tm] = true;
if (!vis[node][(tm+1)%l] && cl[tm+1] != node) {
q.push({node, (tm+1)%l});
dst[node][(tm+1)%l] = dst[node][tm] + 1;
}
for (auto u: adj[node]) {
if (!vis[u][tm+1] && /*fill in*/) {
q.push({u, (tm+1)%l});
dst[u][(tm+1)%l] = dst[node][tm] + 1;
}
}
}
if (dst[n][0] == INT_MAX) printf("impossible\n");
else {
int ans = INT_MAX;
for (int j = 0; j < l; j++) ans = min(ans, dst[n][j]);
printf("%d\n", ans);
}
return 0;
}