이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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({0, p[0], st});
while (!q.empty())
{
auto [w, c, 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({w+1, c, l+c});
if (l-c>=0&&w+1<dp[c][l-c]) dp[c][l-c]=w+1, q.push({w+1, c, 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({w, p[nw], 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... |