Submission #251328

#TimeUsernameProblemLanguageResultExecution timeMemory
251328updown1Jakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1006 ms3192 KiB
/*
Code by @marlov
*/
#include <bits/stdc++.h>
using namespace std;

#define INF 200000000
#define maxV 30005

int N,M;
int dogea[maxV], dogeb[maxV];
//distance to location 0-N-1 inclusive
int dist[maxV];
bool vdoge[maxV];
priority_queue< pair<int,int> > pq;
//location contains array
vector<int> lc[maxV];

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>N>>M;
    fill(dist,dist+N,INF);
    for(int i=0;i<M;i++){
        cin>>dogea[i]>>dogeb[i];
        lc[dogea[i]].push_back(i);
    }

    for(int j=0;j<lc[dogea[0]].size();j++){
        pq.push(make_pair(0,lc[dogea[0]][j]));
    }

    dist[dogea[0]]=0;
    while(!pq.empty()){
        //cd is number of jumps and ci is the current index
        int cd=-pq.top().first;
        int ci=pq.top().second;
        pq.pop();
        //if that dog has already been visited there is no need to check again
        if(vdoge[ci]) continue;
        vdoge[ci]=true;
        //loops from current location to 0
        for(int i=0;dogea[ci]-i*dogeb[ci]>=0;i++){
            //current location
            int cl=dogea[ci]-i*dogeb[ci];
            if(cd+i<dist[cl]){
                dist[cl]=cd+i;
                for(int j:lc[cl]){
                    pq.push(make_pair(-dist[cl],j));
                }
            }
        }
        //to N-1
        for(int i=1;dogea[ci]+i*dogeb[ci]<N;i++){
            int cl=dogea[ci]+i*dogeb[ci];
            if(cd+i<dist[cl]){
                dist[cl]=cd+i;
                for(int j:lc[cl]){
                    pq.push(make_pair(-dist[cl],j));
                }
            }
        }
    }
    if(dist[dogea[1]]==INF){
        cout<<-1<<'\n';
    }else{
        cout<<dist[dogea[1]]<<'\n';
    }
    return 0;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1,n=0?)
	* do smth instead of nothing and stay organized
*/

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:28:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<lc[dogea[0]].size();j++){
                 ~^~~~~~~~~~~~~~~~~~~~
#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...