제출 #1014256

#제출 시각아이디문제언어결과실행 시간메모리
1014256aufanJakarta Skyscrapers (APIO15_skyscraper)C++17
10 / 100
1 ms452 KiB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

const int inff = 1e18;

int32_t main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        int n, m;
        cin >> n >> m;

        vector<int> b(m), p(m);
        for (int i = 0; i < m; i++) cin >> b[i] >> p[i];

        vector<vector<int>> v(n);
        for (int i = 0; i < m; i++) v[b[i]].push_back(p[i]);

        for (int i = 0; i < n; i++) {
                sort(v[i].begin(), v[i].end());
                v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
        }

        vector<int> dist(n, inff);
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        dist[b[0]] = 0;
        pq.push({dist[b[0]], 0});
        while (!pq.empty()) {
                auto [w, x] = pq.top();
                pq.pop();

                if (w > dist[x]) continue;

                for (auto k : v[x]) {
                        for (int i = x + k; i < n; i += k) {
                                if (w + (i - x) / k < dist[i]) {
                                        dist[i] = w + (i - x) / k;
                                        pq.push({dist[i], i});
                                }
                        }

                        for (int i = x - k; i >= 0; i -= k) {
                                if (w + (x - i) / k < dist[i]) {
                                        dist[i] = w + (x - i) / k;
                                        pq.push({dist[i], i});
                                }
                        }
                }
        }

        cout << (dist[b[1]] == inff ? -1 : dist[b[1]]) << '\n';
        
        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...