Submission #170572

#TimeUsernameProblemLanguageResultExecution timeMemory
170572VEGAnnJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
61 ms764 KiB
#include <bits/stdc++.h>
#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 = 2010;
const int oo = 2e9;
set<pii> st;
vector<int> g[N];
int n, m, bg, dst[N], ed;

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++)
        dst[i] = oo;
    dst[bg] = 0;
    st.insert(MP(0, bg));
    while (sz(st)){
        int cur = (*st.begin()).sd;
        st.erase(st.begin());
        for (int cr : g[cur]){
            int steps = dst[cur];
            for (int i = cur; i >= 0; i -= cr, steps++){
                if (steps < dst[i]){
                    st.erase(MP(dst[i], i));
                    dst[i] = steps;
                    st.insert(MP(dst[i], i));
                }
            }
            steps = dst[cur];
            for (int i = cur; i < n; i += cr, steps++){
                if (steps < dst[i]){
                    st.erase(MP(dst[i], i));
                    dst[i] = steps;
                    st.insert(MP(dst[i], i));
                }
            }
        }
    }
    cout << (dst[ed] == oo ? -1 : dst[ed]);
    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...