Submission #580574

#TimeUsernameProblemLanguageResultExecution timeMemory
580574CyberSleeperJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
821 ms24272 KiB
#include <bits/stdc++.h>
#define fastio      ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x)    cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x)      x.begin(), x.end()
#define fi          first
#define se          second
#define mp          make_pair
#define pb          push_back
#define ll          long long
#define ull         unsigned long long
#define pll         pair<ll, ll>
#define pii         pair<int, int>
#define ld          long double
#define nl          endl
#define tb          '\t'
#define sp          ' '
#define sqr(x)      (x)*(x)
#define arr3        array<int, 3>
using namespace std;
 
const int MX=30005, MOD=1000000007, BLOCK=160, INF=2e9+7, LG=62;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;
 
int N, M, B[MX], P[MX];
int vis[MX][BLOCK+5], vis2[MX];
vector<int> doge[MX];
 
int main(){
    fastio;
    cin >> N >> M;
    for(int i=0; i<=N; i++){
        for(int j=0; j<=BLOCK; j++)
            vis[i][j]=INF;
    }
    for(int i=0; i<M; i++){
        cin >> B[i] >> P[i];
        doge[B[i]].pb(P[i]);
    }
    priority_queue<arr3, vector<arr3>, greater<arr3>> PQ;
    PQ.push({0, B[0], 0});
    vis[B[0]][0]=0;
    while(PQ.size()){
        int cost=PQ.top()[0], idx=PQ.top()[1], p=PQ.top()[2];
        PQ.pop();
        if(vis[idx][p]<cost)
            continue;
        vis[idx][p]=cost;
        if(!p){
            for(int i:doge[idx]){
                if(i<=BLOCK){
                    if(vis[idx][i]<=cost)
                        continue;
                    vis[idx][i]=cost;
                    PQ.push({cost, idx, i});
                }else{
                    for(int j=1, jj; idx+i*j<N; j++){
                        jj=idx+i*j;
                        if(vis[jj][0]>cost+j){
                            vis[jj][0]=cost+j;
                            PQ.push({cost+j, jj, 0});
                        }
                    }
                    for(int j=1, jj; idx-i*j>=0; j++){
                        jj=idx-i*j;
                        if(vis[jj][0]>cost+j){
                            vis[jj][0]=cost+j;
                            PQ.push({cost+j, jj, 0});
                        }
                    }
                }
            }
        }else{
            for(int j=1, jj; j<=1; j++){
                jj=idx+p*j;
                if(jj>=N)
                    continue;
                if(vis[jj][0]>cost+j){
                    vis[jj][0]=cost+j;
                    PQ.push({cost+j, jj, 0});
                }
                if(vis[jj][p]>cost+j){
                    vis[jj][p]=cost+j;
                    PQ.push({cost+j, jj, p});
                }
            }
            for(int j=1, jj; j<=1; j++){
                jj=idx-p*j;
                if(jj<0)
                    continue;
                if(vis[jj][0]>cost+j){
                    vis[jj][0]=cost+j;
                    PQ.push({cost+j, jj, 0});
                }
                if(vis[jj][p]>cost+j){
                    vis[jj][p]=cost+j;
                    PQ.push({cost+j, jj, p});
                }
            }
        }
    }
    int ans=vis[B[1]][0];
    if(ans==INF)
        ans=-1;
    cout << ans << nl;
}
#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...