Submission #699935

#TimeUsernameProblemLanguageResultExecution timeMemory
699935Abrar_Al_SamitJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1094 ms81204 KiB
#include<bits/stdc++.h> using namespace std; const int nax = 3e4 + 3; const int INF = 1e9; int b[nax], p[nax]; vector<int>doges[nax]; unordered_map<long long,int>dist; int get(int x, int y) { long long H = x * 10000000LL + y; if(dist.find(H)!=dist.end()) return dist[H]; return INF; } void put(int x, int y, int val) { long long H = x * 10000000LL + y; dist[H] = val; } void PlayGround() { int n, m; cin>>n>>m; for(int i=0; i<m; ++i) { cin>>b[i]>>p[i]; doges[b[i]].push_back(p[i]); } put(b[0], 0, 0); queue<tuple<int,int,int>>q; q.push({b[0], 0, 0}); while(!q.empty()) { auto x = q.front(); q.pop(); int ss, j, d; tie(ss, j, d) = x; if(d!=get(ss, j)) continue; if(j==0) { for(int z : doges[ss]) { if(get(ss, z)>d) { put(ss, z, d); q.emplace(ss, z, d); } } } else { if(ss+j<n) { if(get(ss+j, j)>d+1) { put(ss+j, j, d+1); q.emplace(ss+j, j, d+1); } if(get(ss+j, 0)>d+1) { put(ss+j, 0, d+1); q.emplace(ss+j, 0, d+1); } } if(ss-j>=0) { if(get(ss-j, j)>d+1) { put(ss-j, j, d+1); q.emplace(ss-j, j, d+1); } if(get(ss-j, 0)>d+1) { put(ss-j, 0, d+1); q.emplace(ss-j, 0, d+1); } } } } int ans = get(b[1], 0); if(ans==INF) ans = -1; cout<<ans<<'\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); PlayGround(); return 0; }
#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...