Submission #30891

#TimeUsernameProblemLanguageResultExecution timeMemory
30891NavickJakarta Skyscrapers (APIO15_skyscraper)C++14
10 / 100
0 ms3036 KiB
#include <bits/stdc++.h> #define F first #define S second #define pii pair<int, int> #define pb push_back using namespace std; typedef long long ll; typedef long double ld; const int N = 3e4 + 10, M = 5e6, INF = 1e9; unordered_set<ll> st; vector<int> t[N]; bool vis[N]; int d[N]; deque<pii> q; int sp , sb, se; ll gg(ll a, ll b){ return a * N + b; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for(int i=0; i<m; i++){ int p, b; cin >> p >> b; t[p].pb(b); if(i == 0) sp = p, sb = b; if(i == 1) se = p; } fill(d, d + n, INF); q.push_back({sp, sb}); st.insert(gg(sp, sb)); d[sp] = 0; while(!q.empty()){ int p = q.front().F, b = q.front().S; q.pop_front(); int dis = d[p]; if(p == se)return cout << dis << '\n', 0; if(!vis[p]){ for(auto l : t[p]){ if(st.find(gg(p, l)) == st.end()) q.push_front({p, l}), st.insert(gg(p, l)); } vis[p] = true; } if(p + b < n){ if(st.find(gg(p + b, b)) == st.end()){ d[p + b] = dis + 1; q.push_back({p + b, b}); st.insert(gg(p + b, b)); } } if(p - b >= 0){ if(st.find(gg(p - b, b)) == st.end()){ d[p - b] = dis + 1; q.push_back({p - b, b}); st.insert(gg(p - b, b)); } } } if(d[se] != INF) cout << d[se] << endl; else cout << -1 << endl; }
#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...