Submission #537839

#TimeUsernameProblemLanguageResultExecution timeMemory
537839GurbanJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
755 ms28224 KiB
#include "bits/stdc++.h" using namespace std; using ll = long long; const int maxn=3e4+5; const int B = 192; int n,m,st,fn; int b[maxn],p[maxn]; int dis[maxn * B]; bool vis[maxn * B],done[maxn]; vector<int>doge[maxn]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; int sq = sqrt(n)+1; for(int i = 0;i < m;i++){ cin >> b[i] >> p[i]; if(i == 0) st = b[i]; if(i == 1) fn = b[i]; doge[b[i]].push_back(p[i]); } for(int i = 0;i < n * sq;i++) dis[i] = 1e9; dis[st * sq] = 0; priority_queue<pair<int,int>>q; q.push({0,st * sq}); while(!q.empty()){ int x = q.top().second; q.pop(); if(vis[x]) continue; vis[x] = 1; int bb = x / sq; int pw = x % sq; // cout<<bb<<' '<<pw<<'\n'; if(!pw){ for(auto i : doge[bb]){ if(i < sq){ int to = bb * sq + i; if(dis[to] > dis[x]){ dis[to] = dis[x]; q.push({-dis[to],to}); } } else { for(int j = 1;;j++){ int to = bb - i * j; if(to < 0) break; if(dis[to * sq] > dis[x] + j){ dis[to * sq] = dis[x] + j; q.push({-dis[to * sq],to * sq}); } } for(int j = 1;;j++){ int to = bb + i * j; if(to >= n) break; if(dis[to * sq] > dis[x] + j){ dis[to * sq] = dis[x] + j; q.push({-dis[to * sq],to * sq}); } } } } } else { if(dis[bb * sq] > dis[x]){ dis[bb * sq] = dis[x]; q.push({-dis[bb*sq],bb*sq}); } if(bb - pw >= 0 and dis[(bb - pw) * sq + pw] > dis[x] + 1){ dis[(bb - pw) * sq + pw] = dis[x] + 1; q.push({-dis[(bb - pw) * sq + pw],(bb - pw) * sq + pw}); } if(bb + pw < n and dis[(bb + pw) * sq + pw] > dis[x] + 1){ dis[(bb + pw) * sq + pw] = dis[x] + 1; q.push({-dis[(bb + pw) * sq + pw],(bb + pw) * sq + pw}); } } } if(dis[fn * sq] == 1e9) cout<<-1; else cout<<dis[fn * sq]; }
#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...