Submission #328619

#TimeUsernameProblemLanguageResultExecution timeMemory
328619Mahdi_ShokoufiJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
541 ms7268 KiB
//In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())

const int N = 2000 + 10;
const int M = 50000 + 10;
const ll inf = 1e18 + 10;

ll b[M], P[M], d[M];
vector < ll > vec[M];
priority_queue < pll , vector < pll > , greater < pll > > pq;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll n, m;
    cin >> n >> m;
    for (ll i = 0; i < m; i ++){
        cin >> b[i] >> P[i];
        vec[b[i]].pb(P[i]);
    }
    fill(d, d + M, inf);
    d[b[0]] = 0;
    pq.push(mp(0, b[0]));
    while (!pq.empty()){
        ll x = pq.top().Y, c = pq.top().X;
        pq.pop();
        if (d[x] < c)
            continue;
        for (ll p : vec[x]){
            for (ll y = x, t = 0; y < n; y += p, t ++){
                if (c + t < d[y]){
                    d[y] = c + t;
                    pq.push(mp(d[y], y));
                }
            }
            for (ll y = x, t = 0; 0 <= y; y -= p, t ++){
                if (c + t < d[y]){
                    d[y] = c + t;
                    pq.push(mp(d[y], y));
                }
            }
        }
    }
    cout << (d[b[1]] >= inf ? -1 : d[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...