Submission #170574

#TimeUsernameProblemLanguageResultExecution timeMemory
170574VEGAnnJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1082 ms24656 KiB
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define pii pair<int, int>
#define ft first
#define sd second
#define MP make_pair
#define PB push_back
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const ll PW = 43;
const int N = 30010;
const int B = 180;
const int oo = 2e9;
set<pii> st;
vector<int> g[N];
int n, m, bg, dst[N][B], ed;

int make_nom(int x, int y){
    return y * n + x;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("in.txt","r",stdin);
    cin >> n >> m;
    for (int i = 0; i < m; i++){
        int ps, vl;
        cin >> ps >> vl;
        if (i == 0)
            bg = ps;
        if (i == 1)
            ed = ps;
        g[ps].PB(vl);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < B; j++)
            dst[i][j] = oo;
        sort(all(g[i]));
        g[i].resize(unique(all(g[i])) - g[i].begin());
    }
    dst[bg][0] = 0;
    st.insert(MP(0, bg));
    while (sz(st)){
        int cur = (*st.begin()).sd;
        st.erase(st.begin());
        if (cur < n){
            for (int cr : g[cur]){
                if (cr >= B){
                    int steps = dst[cur][0];
                    for (int i = cur; i >= 0; i -= cr, steps++){
                        if (steps < dst[i][0]){
                            st.erase(MP(dst[i][0], i));
                            dst[i][0] = steps;
                            st.insert(MP(dst[i][0], i));
                        }
                    }
                    steps = dst[cur][0];
                    for (int i = cur; i < n; i += cr, steps++){
                        if (steps < dst[i][0]){
                            st.erase(MP(dst[i][0], i));
                            dst[i][0] = steps;
                            st.insert(MP(dst[i][0], i));
                        }
                    }
                } else {
                    if (dst[cur][cr] > dst[cur][0]){
                        int nom = make_nom(cur, cr);
                        st.erase(MP(dst[cur][cr], nom));
                        dst[cur][cr] = dst[cur][0];
                        st.insert(MP(dst[cur][cr], nom));
                    }
                }
            }
        } else {
            int dv = cur / n;
            cur %= n;
            int nw = cur - dv;
            int nd = dst[cur][dv] + 1;
            if (nw >= 0){
                if (dst[nw][dv] > nd){
                    int nom = make_nom(nw, dv);
                    st.erase(MP(dst[nw][dv], nom));
                    dst[nw][dv] = nd;
                    st.insert(MP(dst[nw][dv], nom));
                }
                if (dst[nw][0] > nd){
                    st.erase(MP(dst[nw][0], nw));
                    dst[nw][0] = nd;
                    st.insert(MP(dst[nw][0], nw));
                }
            }
                nw = cur + dv;
            if (nw < n){
                if (dst[nw][dv] > nd){
                    int nom = make_nom(nw, dv);
                    st.erase(MP(dst[nw][dv], nom));
                    dst[nw][dv] = nd;
                    st.insert(MP(dst[nw][dv], nom));
                }
                if (dst[nw][0] > nd){
                    st.erase(MP(dst[nw][0], nw));
                    dst[nw][0] = nd;
                    st.insert(MP(dst[nw][0], nw));
                }
            }
        }
    }
    cout << (dst[ed][0] == oo ? -1 : dst[ed][0]);
    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...