Submission #960053

#TimeUsernameProblemLanguageResultExecution timeMemory
960053josanneo22From Hacks to Snitches (BOI21_watchmen)C++17
5 / 100
1189 ms102448 KiB
#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'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...