Submission #1162171

#TimeUsernameProblemLanguageResultExecution timeMemory
1162171minhpkJakarta Skyscrapers (APIO15_skyscraper)C++20
57 / 100
1100 ms155376 KiB
#include <bits/stdc++.h> using namespace std; struct PairHash { size_t operator()(const pair<int, int>& p) const { auto h1 = std::hash<int>()(p.first); auto h2 = std::hash<int>()(p.second); return h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1 << 6) + (h1 >> 2)); } }; int a, b; vector<int> z[1000005]; int blocksize; pair<int, int> mark[30005]; struct node { int val, pos, type; bool operator>(const node &other) const { return val > other.val; } }; unordered_map<pair<int, int>, int, PairHash> cnt; void dijkstra() { priority_queue<node, vector<node>, greater<node>> q; q.push({0, mark[0].first, mark[0].second}); cnt[{mark[0].first, mark[0].second}] = 0; while (!q.empty()) { node cur = q.top(); q.pop(); int val = cur.val, pos1 = cur.pos, type = cur.type; if (cnt[{pos1, type}] < val) continue; if (type == 0) { for (auto p : z[pos1]) { int newPos = pos1 + p; if (newPos < a) { pair<int,int> key = {newPos, p}; if (!cnt.count(key) || cnt[key] > val + 1) { cnt[key] = val + 1; q.push({val + 1, newPos, p}); } } newPos = pos1 - p; if (newPos >= 0) { pair<int,int> key = {newPos, p}; if (!cnt.count(key) || cnt[key] > val + 1) { cnt[key] = val + 1; q.push({val + 1, newPos, p}); } } } } else { pair<int,int> key0 = {pos1, 0}; if (!cnt.count(key0) || cnt[key0] > val) { cnt[key0] = val; q.push({val, pos1, 0}); } if (type > blocksize) { int step = 1; while (pos1 + step * type < a) { int newPos = pos1 + step * type; pair<int,int> key0 = {newPos, 0}; if (!cnt.count(key0) || cnt[key0] > val + step) { cnt[key0] = val + step; q.push({val + step, newPos, 0}); } step++; } step = 1; while (pos1 - step * type >= 0) { int newPos = pos1 - step * type; pair<int,int> key0 = {newPos, 0}; if (!cnt.count(key0) || cnt[key0] > val + step) { cnt[key0] = val + step; q.push({val + step, newPos, 0}); } step++; } } else { int newPos = pos1 + type; if (newPos < a) { pair<int,int> key = {newPos, type}; if (!cnt.count(key) || cnt[key] > val + 1) { cnt[key] = val + 1; q.push({val + 1, newPos, type}); } } newPos = pos1 - type; if (newPos >= 0) { pair<int,int> key = {newPos, type}; if (!cnt.count(key) || cnt[key] > val + 1) { cnt[key] = val + 1; q.push({val + 1, newPos, type}); } } } } } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> a >> b; blocksize = sqrt(a); for (int i = 0; i < b; i++){ int x, y; cin >> x >> y; z[x].push_back(y); mark[i] = {x, y}; } if(b == 0){ cout << -1; return 0; } dijkstra(); pair<int,int> finalKey = {mark[1].first, 0}; if(cnt.count(finalKey)){ cout << cnt[finalKey]; } else { cout << -1; } 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...