이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
const int BLOCK=226;
int m,n,s,t,u,vv;
int dp[50005];
bool check[50005][BLOCK+5];
vector<int> g[50005];
struct jakarta
{
int v,p,w;
jakarta(int v,int p,int w): v(v),p(p),w(w) {}
};
queue<jakarta> Q;
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for (int i=0;i<m;++i)
{
cin>>u>>vv;
g[u].push_back(vv);
if (i==0)
{
s=u;
}
if (i==1)
{
t=u;
}
}
memset(dp,-1,sizeof dp);
Q.emplace(s,0,0);
while (Q.size())
{
jakarta hao=Q.front();
Q.pop();
if (hao.v<0 || hao.v>=n) continue;
if (abs(hao.p)<BLOCK)
{
if (check[hao.v][abs(hao.p)]) continue;
check[hao.v][abs(hao.p)]=1;
}
if (dp[hao.v]==-1)
{
dp[hao.v]=hao.w;
for (auto it: g[hao.v])
{
Q.emplace(hao.v+it,it,hao.w+1);
Q.emplace(hao.v-it,-it,hao.w+1);
}
}
Q.emplace(hao.v+hao.p,hao.p,hao.w+1);
}
cout<<dp[t];
}
# | 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... |