This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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+c<n&&w+1<dp[c][l+c]) dp[c][l+c]=w+1, q.push({c, w+1, l+c});
if (l-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 time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |