Submission #146008

#TimeUsernameProblemLanguageResultExecution timeMemory
146008mhy908Jakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1080 ms3448 KiB
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define inf 987654321
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
int n, m;
int b[30010], p[30010];
int dijk[30010];
priority_queue<pii> pq;
vector<int> jump[30010];
void dijkstra(int st)
{
    pq.push({0, st});
    dijk[st]=0;
    while(!pq.empty()){
        pii y=pq.top();
        int here=y.S, th=-y.F;
        pq.pop();
        if(dijk[here]<th)continue;
        for(int i=0; i<jump[here].size(); i++){
            for(int j=here-jump[here][i]; j>=0; j-=jump[here][i]){
                if(dijk[j]>dijk[here]+(here-j)/jump[here][i]){
                    dijk[j]=dijk[here]+(here-j)/jump[here][i];
                    pq.push({-dijk[j], j});
                }
            }
            for(int j=here+jump[here][i]; j<n; j+=jump[here][i]){
                if(dijk[j]>dijk[here]+(j-here)/jump[here][i]){
                    dijk[j]=dijk[here]+(j-here)/jump[here][i];
                    pq.push({-dijk[j], j});
                }
            }
        }
    }
}
int main()
{
    scanf("%d %d", &n, &m);
    for(int i=1; i<=m; i++){
        scanf("%d %d", &b[i], &p[i]);
        jump[b[i]].pb(p[i]);
    }
    for(int i=0; i<n; i++){
        sort(jump[i].begin(), jump[i].end());
        jump[i].erase(unique(jump[i].begin(), jump[i].end()), jump[i].end());
    }
    fill(dijk, dijk+30005, inf);
    dijkstra(b[1]);
    printf("%d", dijk[b[2]]==inf?-1:dijk[b[2]]);
}

Compilation message (stderr)

skyscraper.cpp: In function 'void dijkstra(int)':
skyscraper.cpp:25:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0; i<jump[here].size(); i++){
                      ~^~~~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &b[i], &p[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...