제출 #556170

#제출 시각아이디문제언어결과실행 시간메모리
556170SweezyJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
175 ms5360 KiB
#include <bits/stdc++.h> using namespace std; // #define int long long const int inf = 1e9; void solve() { int n, m; cin >> n >> m; vector<set<int>> doges(n); vector<pair<int, int>> doge(m); for (auto &[b, p] : doge) { cin >> b >> p; doges[b].insert(p); } vector<int> d(n, inf); vector<int> p_a(n, -1); d[doge[0].first] = 0; set<pair<int, int>> st; st.insert({0, doge[0].first}); while (!st.empty()) { int v = st.begin()->second; if (v == doge[1].first) { cout << d[v]; return; } st.erase(st.begin()); for (auto &p : doges[v]) { for (int mul = 1; v + p * mul < n; mul++) { int nd = d[v] + mul; int r = v + p * mul; if (nd < d[r]) { st.erase({d[r], r}); d[r] = nd; p_a[r] = p; st.insert({d[r], r}); } else if (p_a[r] == p) { break; } } for (int mul = 1; v - p * mul >= 0; mul++) { int nd = d[v] + mul; int l = v - p * mul; if (nd < d[l]) { st.erase({d[l], l}); d[l] = nd; p_a[l] = p; st.insert({d[l], l}); } else if (p_a[l] == p) { break; } } } } cout << -1; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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...