Submission #857610

#TimeUsernameProblemLanguageResultExecution timeMemory
85761012345678Jakarta Skyscrapers (APIO15_skyscraper)C++17
0 / 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];
queue<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[0][st]=0;
    q.push({0, 0, st});
    while (!q.empty())
    {
        auto [c, w, l]=q.front();
        if (c==1)
        {
            cout<<dp[1][ed];
            return 0;
        }
        //cout<<c<<' '<<w<<' '<<l<<'\n';
        q.pop();
        if (l+p[c]<n&&w+1<dp[c][l+p[c]]) dp[c][l+p[c]]=w+1, q.push({c, w+1, l+p[c]});
        if (l-p[c]>=0&&w+1<dp[c][l-p[c]]) dp[c][l-p[c]]=w+1, q.push({c, w+1, l-p[c]});
        for (auto nw:v[l]) if (w<dp[nw][l]) dp[nw][l]=w, q.push({nw, w, l});
    }
}
#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...