Submission #1322071

#TimeUsernameProblemLanguageResultExecution timeMemory
1322071aaaaaaaaJakarta Skyscrapers (APIO15_skyscraper)C++20
0 / 100
18 ms31876 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 2e18;
const int mxN = 2005;
vector<int> pos[mxN];
int n, m, x, y, dp[mxN][mxN];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);
    cin >> n >> m;
    vector<int> d(m), s(m);
    for(int i = 0; i < mxN; ++i){
        for(int j = 0; j < mxN; ++j){
            dp[i][j] = inf;
        }
    }
    for(int i = 0; i < m; ++i){
        cin >> d[i] >> s[i];
        if(i == 0) x = d[i], y = s[i];
        pos[d[i]].push_back(s[i]);
    }
    priority_queue<tuple<int, int, int>> pq;
    pq.push({0, x, y});
    dp[x][y] = 0;
    int ans = inf;
    while(pq.size()){
        auto [dist, id, mov] = pq.top();
        pq.pop();
        dist = -dist;
        if(id == 1){
            ans = min(ans, dist);
            continue;
        }
        //if(dp[id][mov] < dist) continue;
        for(auto it : pos[id]){
            if((id + it) <= (n - 1) && dp[id + it][it] > (dist + 1)){
                dp[id + it][it] = dist + 1;
                pq.push({-dp[id + it][it], id + it, it});
            }
            if((id - it) >= 0 && dp[id - it][it] > (dist + 1)){
                dp[id - it][it] = dist + 1;
                pq.push({-dp[id - it][it], id - it, it});
            }
        }
        if((id + mov) <= (n - 1) && dp[id + mov][mov] > (dist + 1)){
            dp[id + mov][mov] = dist + 1;
            pq.push({-dp[id + mov][mov], id + mov, mov});
        }
        if((id - mov) >= 0 && dp[id - mov][mov] > (dist + 1)){
            dp[id - mov][mov] = dist + 1;
            pq.push({-dp[id - mov][mov], id - mov, mov});
        }
    }
    if(ans == inf) cout << -1 << "\n";
    else cout << ans << "\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...