제출 #507293

#제출 시각아이디문제언어결과실행 시간메모리
507293Aldas25Jakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
389 ms262148 KiB
#include <bits/stdc++.h>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;

const int MAXN = 30100;
const int MAXC = (int) (sqrt(MAXN));

int n, m, c;
int b[MAXN], p[MAXN];
vi jumps[MAXN];
int d[MAXN][MAXC];
bool vis[MAXN][MAXC];
vector<pair<pii, int>> adj[MAXN][MAXC];

void dijkstra () {
    FOR(i, 0, n-1) FOR(j, 0, c) d[i][j] = -1;
    d[b[0]][0] = 0;
    priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> q;
    q.push({0, {b[0], 0}});

    while (!q.empty()) {
        auto v = q.top().s;
        q.pop();
        int pos = v.f, j = v.s;
        if (vis[pos][j]) continue;
        vis[pos][j] = true;
        //cout << " pos = " << pos << " j = "<< j << endl;

        for (auto e : adj[pos][j]) {
            int w = e.s;
            int nPos = e.f.f, nJ = e.f.s;
            if (d[nPos][nJ] == -1 || d[nPos][nJ] > d[pos][j] + w) {
                d[nPos][nJ] = d[pos][j] + w;
                q.push({d[nPos][nJ], {nPos, nJ}});
            }
        }
    }
}

int main()
{
    FAST_IO;

    cin >> n >> m;
    c = sqrt(n);
    FOR(i, 0, m-1) cin >> b[i] >> p[i];
    FOR(i, 0, m-1) jumps[b[i]].pb(p[i]);

    FOR(pos, 0, n-1) {
        FOR(j, 0, c) {
            adj[pos][j].pb({{pos,0}, 0});

            if (pos+j < n)
                adj[pos][j].pb({{pos+j, j}, 1});
            if (pos-j >= 0)
                adj[pos][j].pb({{pos-j, j}, 1});
        }

        for (int j : jumps[pos]) {
            if (j <= c) {
                adj[pos][0].pb({{pos, j}, 0});
            } else {
                FOR(x, -c, c) {
                    int newP = pos + x * j;
                    if (0 <= newP && newP < n)
                        adj[pos][0].pb({{newP, 0}, abs(x)});
                }
            }
        }
    }

    dijkstra ();

    cout << d[b[1]][0] << "\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...