#include <bits/stdc++.h>
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9+7;
const int MX=30015;
int n,m;
int x[MX],y[MX];
const int sq=173;
int dp[30005][sq+4];
vector<int> v[MX];
int DP(int x,int j){
if(x<0||x>=n)R INF;
// cout<<x<<" "<<j<<endl;
if(x==1)R 0;
int &ret=dp[x][j];
if(ret!=-1)R ret;
ret=INF;
for(auto i : v[x]){
{ret=min(ret,DP(x,i));}
}
if(j!=0)
ret=min(ret,DP(x+j,j)+1),ret=min(ret,DP(x-j,j)+1);
// cout<<x<<" "<<j<<" "<<ret<<endl;
R ret;
}
int main(){
MEM(dp,-1);
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>x[i]>>y[i];
v[x[i]].pb(y[i]);
}
if(DP(0,0)==INF)cout<<-1;
else
cout<<DP(0,0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
21880 KB |
Output is correct |
2 |
Incorrect |
17 ms |
21880 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
21932 KB |
Output is correct |
2 |
Incorrect |
17 ms |
21932 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
22160 KB |
Output is correct |
2 |
Incorrect |
17 ms |
22160 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
22160 KB |
Output is correct |
2 |
Incorrect |
17 ms |
22160 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
22160 KB |
Output is correct |
2 |
Incorrect |
17 ms |
22160 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |