#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#define ll long long
#define fi first
#define se second
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int nax=3e4+5;
vector<int> adj[nax];
int dis[nax][300];
signed main(){
iostream::sync_with_stdio(false);
int n,m;
cin>>n>>m;
pair<int,int> fir;
pair<int,int> end;
for (int i = 0; i < m; ++i)
{
int x,y;
cin>>x>>y;
if(i==0) fir={x,y};
if(i==1) end={x,y};
adj[x].pb(y);
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < 300; ++j)
{
dis[i][j]=1e9;
}
}
dis[fir.fi][0]=0;
priority_queue<tuple<int,int,int>,vector<tuple<int,int,int>>,greater<tuple<int,int,int>>> pq;
pq.push({0,fir.fi,0});
while(!pq.empty()){
auto [d , node , lst] = pq.top();
pq.pop();
if(node==end.fi){
cout <<d<<endl;
return 0;
}
if(dis[node][lst]<d) continue;
for(auto u:adj[node]){
if(u<300){
if(node-u>=0){
if(dis[node-u][u]>d+1){
dis[node-u][u]=d+1;
pq.push({d+1,node-u,u});
}
}if(node+u<n){
if(dis[node+u][u]>d+1){
dis[node+u][u]=d+1;
pq.push({d+1,node+u,u});
}
}
}else{
for (int i = 1; i < n; ++i)
{
if(u*i+node>=n) break;
if(dis[node+u*i][0]>d+i){
dis[node+u*i][0]=d+i;
pq.push({d+i,node+u*i,0});
}
}
for (int i = 1; i < n; ++i)
{
if(node-u*i<0) break;
if(dis[node-u*i][0]>d+i){
dis[node-u*i][0]=d+i;
pq.push({d+i,node-u*i,0});
}
}
}
}
if(lst!=0){
if(node-lst>=0){
if(dis[node-lst][lst]>d+1){
dis[node-lst][lst]=d+1;
pq.push({d+1,node-lst,lst});
}
}if(node+lst<n){
if(dis[node+lst][lst]>d+1){
dis[node+lst][lst]=d+1;
pq.push({d+1,node+lst,lst});
}
}
}
}
cout <<-1<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1176 KB |
Output is correct |
11 |
Correct |
1 ms |
1128 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
2 ms |
3164 KB |
Output is correct |
24 |
Correct |
2 ms |
3416 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
2 ms |
3420 KB |
Output is correct |
27 |
Correct |
2 ms |
3432 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
29 |
Correct |
6 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
3420 KB |
Output is correct |
31 |
Correct |
4 ms |
3420 KB |
Output is correct |
32 |
Correct |
3 ms |
3416 KB |
Output is correct |
33 |
Correct |
12 ms |
3420 KB |
Output is correct |
34 |
Correct |
8 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1176 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
3420 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
2 ms |
3420 KB |
Output is correct |
27 |
Correct |
2 ms |
3420 KB |
Output is correct |
28 |
Correct |
2 ms |
3616 KB |
Output is correct |
29 |
Correct |
6 ms |
3420 KB |
Output is correct |
30 |
Correct |
3 ms |
3420 KB |
Output is correct |
31 |
Correct |
4 ms |
3420 KB |
Output is correct |
32 |
Correct |
3 ms |
3420 KB |
Output is correct |
33 |
Correct |
13 ms |
3656 KB |
Output is correct |
34 |
Correct |
7 ms |
3476 KB |
Output is correct |
35 |
Correct |
6 ms |
3420 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
5 ms |
3932 KB |
Output is correct |
38 |
Correct |
5 ms |
3932 KB |
Output is correct |
39 |
Correct |
5 ms |
3784 KB |
Output is correct |
40 |
Correct |
5 ms |
3932 KB |
Output is correct |
41 |
Correct |
7 ms |
3888 KB |
Output is correct |
42 |
Correct |
4 ms |
3932 KB |
Output is correct |
43 |
Correct |
4 ms |
3868 KB |
Output is correct |
44 |
Correct |
4 ms |
3928 KB |
Output is correct |
45 |
Correct |
69 ms |
4748 KB |
Output is correct |
46 |
Correct |
50 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1180 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1172 KB |
Output is correct |
9 |
Correct |
1 ms |
1176 KB |
Output is correct |
10 |
Correct |
1 ms |
1172 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1956 KB |
Output is correct |
18 |
Correct |
2 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3492 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
2 ms |
3164 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
3268 KB |
Output is correct |
25 |
Correct |
2 ms |
3416 KB |
Output is correct |
26 |
Correct |
2 ms |
3420 KB |
Output is correct |
27 |
Correct |
2 ms |
3420 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
29 |
Correct |
6 ms |
3416 KB |
Output is correct |
30 |
Correct |
2 ms |
3420 KB |
Output is correct |
31 |
Correct |
4 ms |
3476 KB |
Output is correct |
32 |
Correct |
3 ms |
3420 KB |
Output is correct |
33 |
Correct |
12 ms |
3656 KB |
Output is correct |
34 |
Correct |
7 ms |
3656 KB |
Output is correct |
35 |
Correct |
9 ms |
3468 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
5 ms |
4000 KB |
Output is correct |
38 |
Correct |
6 ms |
3932 KB |
Output is correct |
39 |
Correct |
5 ms |
3928 KB |
Output is correct |
40 |
Correct |
5 ms |
3816 KB |
Output is correct |
41 |
Correct |
5 ms |
3932 KB |
Output is correct |
42 |
Correct |
4 ms |
3932 KB |
Output is correct |
43 |
Correct |
4 ms |
4184 KB |
Output is correct |
44 |
Correct |
4 ms |
3928 KB |
Output is correct |
45 |
Correct |
71 ms |
4764 KB |
Output is correct |
46 |
Correct |
52 ms |
4760 KB |
Output is correct |
47 |
Correct |
13 ms |
16668 KB |
Output is correct |
48 |
Correct |
15 ms |
28920 KB |
Output is correct |
49 |
Correct |
16 ms |
31580 KB |
Output is correct |
50 |
Correct |
22 ms |
34392 KB |
Output is correct |
51 |
Correct |
25 ms |
37804 KB |
Output is correct |
52 |
Correct |
27 ms |
37664 KB |
Output is correct |
53 |
Correct |
21 ms |
37208 KB |
Output is correct |
54 |
Correct |
15 ms |
36184 KB |
Output is correct |
55 |
Correct |
15 ms |
36236 KB |
Output is correct |
56 |
Correct |
21 ms |
37464 KB |
Output is correct |
57 |
Correct |
16 ms |
36248 KB |
Output is correct |
58 |
Correct |
21 ms |
36700 KB |
Output is correct |
59 |
Correct |
19 ms |
36700 KB |
Output is correct |
60 |
Correct |
20 ms |
36804 KB |
Output is correct |
61 |
Correct |
20 ms |
36700 KB |
Output is correct |
62 |
Correct |
47 ms |
37440 KB |
Output is correct |
63 |
Correct |
230 ms |
38356 KB |
Output is correct |
64 |
Correct |
391 ms |
38352 KB |
Output is correct |
65 |
Correct |
740 ms |
38360 KB |
Output is correct |
66 |
Correct |
750 ms |
37720 KB |
Output is correct |
67 |
Correct |
458 ms |
37724 KB |
Output is correct |