Submission #196999

#TimeUsernameProblemLanguageResultExecution timeMemory
196999quocnguyen1012Jakarta Skyscrapers (APIO15_skyscraper)C++14
10 / 100
4 ms1272 KiB
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back using namespace std; typedef long long ll; const int maxn = 3e4 + 5; int N, M; vector<int> have[maxn]; int dist[maxn], b[maxn], p[maxn]; bool Have(int pos, int val) { return binary_search(have[pos].begin(), have[pos].end(), val); } signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen("A.INP", "r")){ freopen("A.INP", "r", stdin); freopen("A.OUT", "w", stdout); } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; cin >> N >> M; for (int i = 0; i < M; ++i){ cin >> b[i] >> p[i]; have[b[i]].pb(p[i]); } pq.push(mp(b[0], 0)); fill_n(&dist[0], maxn, 1e9 + 5); dist[b[0]] = 0; for (int i = 0; i < N; ++i){ sort(have[i].begin(), have[i].end()); have[i].erase(unique(have[i].begin(), have[i].end()), have[i].end()); } while (pq.size()){ auto now = pq.top(); pq.pop(); if (dist[now.se] != now.fi) continue; for (auto all : have[now.se]){ for (int i = 1; i < maxn; ++i){ if (now.se + i * all < N){ if (dist[now.se + i * all] > now.fi + i){ dist[now.se + i * all] = now.fi + i; pq.push(mp(now.fi + i, now.se + i * all)); if (Have(now.se + i * all, all)) break; } } else break; } for (int i = 1; i < maxn; ++i){ if (now.se - i * all >= 0){ if (dist[now.se - i * all] > now.fi + i){ dist[now.se - i * all] = now.fi + i; pq.push(mp(now.fi + i, now.se - i * all)); if (Have(now.se - i * all, all)) break; } } else break; } } } if (dist[b[1]] >= 1e9 + 5) dist[b[1]] = -1; cout << dist[b[1]] << '\n'; }

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...