Submission #170937

#TimeUsernameProblemLanguageResultExecution timeMemory
170937ne4eHbKaJakarta Skyscrapers (APIO15_skyscraper)C++17
22 / 100
7 ms1272 KiB
//{ <defines>
#ifndef _LOCAL
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#endif

#include <bits/stdc++.h>
using namespace std;

#define fr(i, n) for(int i = 0; i < n; ++i)
#define fo(n) fr(i, n)

#define re return
#define ef else if
#define ifn(x) if(!(x))
#define _  << ' ' <<

#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back

#define sz(x) int(x.size())
#define pw(x) (1 << (x))
#define PW(x) (1ll << (x))
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset(x, y, sizeof x)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ve<int> vi;

const int oo = 2e9;
const ll OO = 4e18;
//const ld pi = arg(complex<ld>(-1, 0));
//const ld pi2 = pi + pi;
const int md = 0x3b800001;
const int MD = 1e9 + 7;

inline ll time() {re chrono :: system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
mt19937_64 RND(time());

template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}
//} </defines>
const int N = 3e4 + 5;

int n, m, st, fn, d[N];
vi g[N];
bool mk[N];

void solve() {
    cin >> n >> m;
    fo(m) {
        int b, p;
        cin >> b >> p;
        if(i == 0) st = b;
        if(i == 1) fn = b;
        g[b].pb(p);
    }

    fo(n) d[i] = N;
    d[st] = 0;

    struct sc {
        inline bool operator() (const int &a, const int &b) const
            {re d[a] == d[b] ? a < b : d[a] < d[b];}
    };

    set<int, sc> f;
    f.insert(st);

    for(; !f.empty(); ) {
        int v = *f.begin();
        f.erase(f.begin());
        mk[v] = 1;
        if(v == fn) re void(cout << d[fn] << endl);
        for(int p : g[v]) {
            for(int sign = -1; sign <= 1; sign += 2) {
                bool better = true;
                for(int w = 1, u = v + sign * p;
                    better && u >= 0 && u < n;
                    ++w, u += sign * p) ifn(mk[u]) {
                    int dd = d[v] + w;
                    if(dd < d[u]) {
                        auto c = f.find(u);
                        if(c != f.end()) f.erase(c);
                        d[u] = dd;
                        f.insert(u);
                    } else better = false;
                }
            }
        }
    }

    cout << -1 << endl;
}

int main() {
    solve();
}
#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...