#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#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=2e3+5;
vector<int> adj[nax];
int dis[nax][nax];
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)
{
if(i==fir.fi) continue;
for (int j = 0; j < sqrt(n); ++j)
{
dis[i][j]=1e9;
}
}
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<sqrt(n)){
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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2460 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
3 ms |
11860 KB |
Output is correct |
20 |
Correct |
3 ms |
10844 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
3 ms |
10844 KB |
Output is correct |
23 |
Correct |
4 ms |
9052 KB |
Output is correct |
24 |
Correct |
5 ms |
9240 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
9716 KB |
Output is correct |
28 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
3 ms |
8540 KB |
Output is correct |
19 |
Correct |
3 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
9820 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
9676 KB |
Output is correct |
23 |
Correct |
3 ms |
9052 KB |
Output is correct |
24 |
Correct |
3 ms |
10408 KB |
Output is correct |
25 |
Correct |
4 ms |
10772 KB |
Output is correct |
26 |
Correct |
3 ms |
10712 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2568 KB |
Output is correct |
11 |
Correct |
1 ms |
2656 KB |
Output is correct |
12 |
Correct |
1 ms |
2664 KB |
Output is correct |
13 |
Correct |
1 ms |
2656 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2664 KB |
Output is correct |
16 |
Correct |
1 ms |
2660 KB |
Output is correct |
17 |
Correct |
1 ms |
4448 KB |
Output is correct |
18 |
Correct |
3 ms |
8768 KB |
Output is correct |
19 |
Correct |
3 ms |
10628 KB |
Output is correct |
20 |
Correct |
4 ms |
9884 KB |
Output is correct |
21 |
Correct |
1 ms |
4704 KB |
Output is correct |
22 |
Correct |
3 ms |
8888 KB |
Output is correct |
23 |
Correct |
3 ms |
9052 KB |
Output is correct |
24 |
Correct |
3 ms |
9316 KB |
Output is correct |
25 |
Correct |
3 ms |
9688 KB |
Output is correct |
26 |
Correct |
3 ms |
9820 KB |
Output is correct |
27 |
Correct |
3 ms |
10600 KB |
Output is correct |
28 |
Incorrect |
3 ms |
10852 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |