Submission #857632

#TimeUsernameProblemLanguageResultExecution timeMemory
85763212345678Jakarta Skyscrapers (APIO15_skyscraper)C++17
10 / 100
5 ms16472 KiB
#include <bits/stdc++.h>

using namespace std;

const int nx=2e3+5;

int n, m, st, ed, jp, b, p[nx], dp[nx][nx];
vector<int> v[nx];
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>m>>st>>p[0]>>ed>>p[1]; v[ed].push_back(1);
    for (int i=1; i<m-1; i++) cin>>b>>p[i+1], v[b].push_back(i+1);
    for (int i=0; i<nx; i++) for (int j=0; j<nx; j++) dp[i][j]=INT_MAX;
    dp[p[0]][st]=0;
    q.push({p[0], 0, st});
    while (!q.empty())
    {
        auto [c, w, l]=q.top();
        //cout<<c<<' '<<w<<' '<<l<<'\n';
        q.pop();
        if (l+p[c]<n&&w+1<dp[c][l+c]) dp[c][l+c]=w+1, q.push({c, w+1, l+c});
        if (l-p[c]>=0&&w+1<dp[c][l-c]) dp[c][l-c]=w+1, q.push({c, w+1, l-c});
        for (auto nw:v[l]) 
        {
            if (nw==1)
            {
                cout<<w;
                return 0;
            }
            if (w<dp[p[nw]][l]) dp[p[nw]][l]=w, q.push({p[nw], w, l});
        }
    }
    cout<<-1;
}
#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...