Submission #1015716

#TimeUsernameProblemLanguageResultExecution timeMemory
1015716andrei_iorgulescuJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1048 ms136332 KiB
#include <bits/stdc++.h> using namespace std; int hsh(pair<int,int> pr) { return pr.first * 31000 + pr.second; } int n,m; int b[30005],p[30005]; unordered_map<int, bool> viz,luat; unordered_map<int, int> d; vector<int> what[30005]; int str,fin; pair<int,int> q[2000005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= m; i++) cin >> b[i] >> p[i],what[b[i]].push_back(p[i]); str = b[1],fin = b[2]; int bk = 1e6,fr = 1e6; q[bk] = {str,0}; viz[hsh({str,0})] = true; int cate = 0,lul = 1e6; while (bk <= fr) { pair<int,int> nod = q[fr]; int dehnod = hsh(nod); fr--; if (luat[dehnod]) continue; if (nod.first == fin and nod.second == 0) break; cate++; if (cate > lul) assert(false); luat[dehnod] = true; int dist = d[dehnod]; if (nod.second != 0) { pair<int,int> vec; vec = {nod.first,0}; int dehvec = hsh(vec); if (!viz[dehvec] or d[dehvec] > dist) { viz[dehvec] = true; q[++fr] = (vec); d[dehvec] = dist; } vec = {nod.first + nod.second,nod.second}; dehvec = hsh(vec); if (!viz[dehvec] and vec.first >= 0 and vec.first < n) { viz[dehvec] = true; q[--bk] = vec; d[dehvec] = dist + 1; } vec = {nod.first - nod.second,nod.second}; dehvec = hsh(vec); if (!viz[dehvec] and vec.first >= 0 and vec.first < n) { viz[dehvec] = true; q[--bk] = vec; d[dehvec] = dist + 1; } } else { for (auto it : what[nod.first]) { pair<int,int> vec = {nod.first,it}; int dehvec = hsh(vec); if (!viz[dehvec] or d[dehvec] > dist) { viz[dehvec] = true; q[++fr] = vec; d[dehvec] = dist; } } } } if (!viz[hsh({fin,0})]) cout << -1; else cout << d[hsh({fin,0})]; 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...