Submission #761439

#TimeUsernameProblemLanguageResultExecution timeMemory
761439gun_ganJakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 100
11 ms21576 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MX = 3e4 + 5, B = 175; vector<int> g[MX]; int N, M; int dist[MX][B]; bool vis[MX]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); for(int i = 0; i < MX; i++) { for(int j = 0; j < B; j++) { dist[i][j] = 1e9; } } cin >> N >> M; int s = 0, f = 0; for(int i = 0; i < M; i++) { int x, k; cin >> x >> k; g[x].push_back(k); if(!i) s = x; if(i == 1) f = x; } priority_queue<array<int,3>> pq; for(auto x : g[s]) { if(x < B) { dist[s][x] = 0; pq.push({dist[s][x], s, x}); } else { for(int k = 1; s + k * x < N; k++) { dist[s + k * x][0] = k; pq.push({-dist[s + k * x][0], s + k * x, 0}); } for(int k = 1; s - k * x >= 0; k++) { dist[s - k * x][0] = k; pq.push({-dist[s - k * x][0], s - k * x, 0}); } } } vis[s] = 1; while(!pq.empty()) { auto [d, v, p] = pq.top(); pq.pop(); d = -d; if(d > dist[v][p]) continue; if(!vis[v]) { for(auto x : g[v]) { if(x < B) { dist[v][x] = d; pq.push({-dist[v][x], v, x}); } else { for(int k = 1; v + k * x < N; k++) { if(dist[v + k * x][0] > d + k) { dist[v + k * x][0] = d + k; pq.push({-dist[v + k * x][0], v + k * x, 0}); } } for(int k = 1; v - k * x >= 0; k++) { if(dist[v - k * x][0] > d + k) { dist[v - k * x][0] = d + k; pq.push({-dist[v - k * x][0], v - k * x, 0}); } } } } vis[v] = 1; } if(v + p < N && dist[v + p][p] > d + 1) { dist[v + p][p] = d + 1; pq.push({-dist[v + p][p], v + p, p}); } if(v - p >= 0 && dist[v - p][p] > d + 1) { dist[v - p][p] = d + 1; pq.push({-dist[v - p][p], v - p, p}); } } int ans = 1e9; for(int k = 0; k < B; k++) ans = min(ans, dist[f][k]); cout << ans << '\n'; }
#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...