Submission #769304

#TimeUsernameProblemLanguageResultExecution timeMemory
769304Ahmed57Jakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
715 ms6280 KiB
#include <bits/stdc++.h>
//#include "game.h"
using namespace std;
vector<long long> ind[30001];
long long len[30001];
long long n,m;
long long nah,z;
long long d(){
    long long cost[n+1];
    for(int i = 0;i<=n;i++)cost[i] = 1e18;
    priority_queue<pair<long long,long long>> q1;
    q1.push({0,z});
    cost[z] = 0;
    while(!q1.empty()){
        long long co = q1.top().first*-1;
        long long no = q1.top().second;
        //cout<<lin<<" "<<indx<<"\n";
        q1.pop();
        if(co>cost[no]) continue ;
        for(auto i:ind[no]){
            long long c = 1;
            for(long long j = no+len[i];j<n;j+=len[i]){
                if(cost[j]>cost[no]+c){
                    cost[j] = cost[no]+c;
                    q1.push({cost[j]*-1,j});
                }
                c++;
            }
            c = 1;
            for(long long j = no-len[i];j>=0;j-=len[i]){
                if(cost[j]>cost[no]+c){
                    cost[j] = cost[no]+c;
                    q1.push({cost[j]*-1,j});
                }
                c++;
            }
        }
    }
    return cost[nah];
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i = 0;i<m;i++){
        long long x,b;
        cin>>x>>b;
        if(i==1)nah = x;
        if(i==0)z = x;
        len[i] = b;
        ind[x].push_back(i);
    }
    //cout<<z<<" "<<nah<<"\n";
    long long val = d();
    if(val==1e18)cout<<-1<<"\n";
    else cout<<val<<"\n";
}
#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...