#include<stdio.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
bool ch[30010][200];
vector<int>con[30010];
typedef pair<int,int> ppair;
struct data{
int val,x,y;
bool operator<(const data&r)const{
return val>r.val;
}
}b;
priority_queue<data>q;
int sw,n,m,root,s,pos;
struct data2{
int x,p;
}a[30010];
int main()
{
int i,j,val,x,y;
scanf("%d %d",&n,&m);
root=sqrt(n);
for(i=0;i<m;i++){scanf("%d %d",&a[i].x,&a[i].p); con[a[i].x].push_back(a[i].p);}
b.val=0, b.x=a[0].x, b.y=a[0].p, q.push(b);
while(1){
while(q.size())
{
if(q.top().y<=root)
{
if(ch[q.top().x][q.top().y]){q.pop(); continue;}
else break;
}
x=q.top().x; val=q.top().val; y=q.top().y;
if(x==a[1].x){printf("%d",val); return 0;}
q.pop();
for(i=x+y;i<n;i+=y){
if(ch[i][0]) continue;
b.val=val+(i-x)/y; b.x=i; b.y=n; q.push(b);
}
for(i=x-y;i>=0;i-=y){
if(ch[i][0]) continue;
b.val=val+(x-i)/y; b.x=i; b.y=n; q.push(b);
}
if(ch[x][0]) continue;
for(i=0;i<con[x].size();i++){
b.val=val; b.x=x; b.y=con[x][i];
if(b.y<=root && ch[b.x][b.y]) continue;
q.push(b);
}
ch[x][0]=1;
}
if(q.size()==0){sw=1; break;}
val=q.top().val; x=q.top().x; y=q.top().y; ch[x][y]=1; q.pop();
if(x==a[1].x) break;
if(ch[x][0]==0)
{
for(i=0;i<con[x].size();i++){
b.val=val; b.x=x; b.y=con[x][i];
if(b.y<=root && ch[b.x][b.y]) continue;
q.push(b);
}
}
ch[x][0]=1;
if(x+y<n) b.val=val+1, b.x=x+y, b.y=y, q.push(b);
if(x-y>=0) b.val=val+1, b.x=x-y, b.y=y, q.push(b);
}
if(sw) printf("-1");
else printf("%d",val);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8408 KB |
Output is correct |
2 |
Correct |
0 ms |
8408 KB |
Output is correct |
3 |
Correct |
0 ms |
8408 KB |
Output is correct |
4 |
Correct |
0 ms |
8408 KB |
Output is correct |
5 |
Correct |
0 ms |
8408 KB |
Output is correct |
6 |
Correct |
0 ms |
8408 KB |
Output is correct |
7 |
Correct |
0 ms |
8408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8408 KB |
Output is correct |
2 |
Correct |
0 ms |
8408 KB |
Output is correct |
3 |
Correct |
0 ms |
8408 KB |
Output is correct |
4 |
Correct |
0 ms |
8408 KB |
Output is correct |
5 |
Correct |
0 ms |
8408 KB |
Output is correct |
6 |
Correct |
0 ms |
8408 KB |
Output is correct |
7 |
Correct |
0 ms |
8408 KB |
Output is correct |
8 |
Correct |
0 ms |
8408 KB |
Output is correct |
9 |
Correct |
0 ms |
8408 KB |
Output is correct |
10 |
Correct |
0 ms |
8408 KB |
Output is correct |
11 |
Correct |
0 ms |
8408 KB |
Output is correct |
12 |
Correct |
0 ms |
8408 KB |
Output is correct |
13 |
Correct |
0 ms |
8408 KB |
Output is correct |
14 |
Correct |
0 ms |
8408 KB |
Output is correct |
15 |
Correct |
0 ms |
8408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8408 KB |
Output is correct |
2 |
Correct |
0 ms |
8408 KB |
Output is correct |
3 |
Correct |
1 ms |
8408 KB |
Output is correct |
4 |
Correct |
0 ms |
8408 KB |
Output is correct |
5 |
Correct |
0 ms |
8408 KB |
Output is correct |
6 |
Correct |
0 ms |
8408 KB |
Output is correct |
7 |
Correct |
0 ms |
8408 KB |
Output is correct |
8 |
Correct |
0 ms |
8408 KB |
Output is correct |
9 |
Correct |
0 ms |
8408 KB |
Output is correct |
10 |
Correct |
0 ms |
8408 KB |
Output is correct |
11 |
Correct |
0 ms |
8408 KB |
Output is correct |
12 |
Correct |
0 ms |
8408 KB |
Output is correct |
13 |
Correct |
1 ms |
8408 KB |
Output is correct |
14 |
Correct |
0 ms |
8408 KB |
Output is correct |
15 |
Correct |
0 ms |
8408 KB |
Output is correct |
16 |
Correct |
0 ms |
8408 KB |
Output is correct |
17 |
Correct |
0 ms |
8408 KB |
Output is correct |
18 |
Correct |
0 ms |
8408 KB |
Output is correct |
19 |
Correct |
0 ms |
8408 KB |
Output is correct |
20 |
Correct |
0 ms |
8408 KB |
Output is correct |
21 |
Correct |
0 ms |
8408 KB |
Output is correct |
22 |
Correct |
0 ms |
8408 KB |
Output is correct |
23 |
Correct |
2 ms |
8408 KB |
Output is correct |
24 |
Correct |
0 ms |
8408 KB |
Output is correct |
25 |
Correct |
0 ms |
8408 KB |
Output is correct |
26 |
Correct |
0 ms |
8408 KB |
Output is correct |
27 |
Correct |
0 ms |
8408 KB |
Output is correct |
28 |
Correct |
0 ms |
8408 KB |
Output is correct |
29 |
Correct |
13 ms |
8408 KB |
Output is correct |
30 |
Correct |
4 ms |
8408 KB |
Output is correct |
31 |
Correct |
7 ms |
8408 KB |
Output is correct |
32 |
Correct |
0 ms |
8608 KB |
Output is correct |
33 |
Correct |
27 ms |
9580 KB |
Output is correct |
34 |
Correct |
17 ms |
9580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8408 KB |
Output is correct |
2 |
Correct |
0 ms |
8408 KB |
Output is correct |
3 |
Correct |
0 ms |
8408 KB |
Output is correct |
4 |
Correct |
0 ms |
8408 KB |
Output is correct |
5 |
Correct |
0 ms |
8408 KB |
Output is correct |
6 |
Correct |
0 ms |
8408 KB |
Output is correct |
7 |
Correct |
0 ms |
8408 KB |
Output is correct |
8 |
Correct |
0 ms |
8408 KB |
Output is correct |
9 |
Correct |
0 ms |
8408 KB |
Output is correct |
10 |
Correct |
0 ms |
8408 KB |
Output is correct |
11 |
Correct |
1 ms |
8408 KB |
Output is correct |
12 |
Correct |
0 ms |
8408 KB |
Output is correct |
13 |
Correct |
0 ms |
8408 KB |
Output is correct |
14 |
Correct |
0 ms |
8408 KB |
Output is correct |
15 |
Correct |
0 ms |
8408 KB |
Output is correct |
16 |
Correct |
0 ms |
8408 KB |
Output is correct |
17 |
Correct |
0 ms |
8408 KB |
Output is correct |
18 |
Correct |
1 ms |
8408 KB |
Output is correct |
19 |
Correct |
0 ms |
8408 KB |
Output is correct |
20 |
Correct |
1 ms |
8408 KB |
Output is correct |
21 |
Correct |
0 ms |
8408 KB |
Output is correct |
22 |
Correct |
0 ms |
8408 KB |
Output is correct |
23 |
Correct |
1 ms |
8408 KB |
Output is correct |
24 |
Correct |
2 ms |
8408 KB |
Output is correct |
25 |
Correct |
0 ms |
8408 KB |
Output is correct |
26 |
Correct |
0 ms |
8408 KB |
Output is correct |
27 |
Correct |
1 ms |
8408 KB |
Output is correct |
28 |
Correct |
1 ms |
8408 KB |
Output is correct |
29 |
Correct |
8 ms |
8408 KB |
Output is correct |
30 |
Correct |
4 ms |
8408 KB |
Output is correct |
31 |
Correct |
8 ms |
8408 KB |
Output is correct |
32 |
Correct |
4 ms |
8608 KB |
Output is correct |
33 |
Correct |
27 ms |
9580 KB |
Output is correct |
34 |
Correct |
16 ms |
9580 KB |
Output is correct |
35 |
Correct |
8 ms |
9220 KB |
Output is correct |
36 |
Correct |
0 ms |
8408 KB |
Output is correct |
37 |
Correct |
12 ms |
9736 KB |
Output is correct |
38 |
Correct |
14 ms |
9248 KB |
Output is correct |
39 |
Correct |
0 ms |
8540 KB |
Output is correct |
40 |
Correct |
0 ms |
8672 KB |
Output is correct |
41 |
Correct |
17 ms |
9248 KB |
Output is correct |
42 |
Correct |
3 ms |
8540 KB |
Output is correct |
43 |
Correct |
0 ms |
8540 KB |
Output is correct |
44 |
Correct |
0 ms |
8540 KB |
Output is correct |
45 |
Correct |
95 ms |
17808 KB |
Output is correct |
46 |
Correct |
53 ms |
17808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8408 KB |
Output is correct |
2 |
Correct |
0 ms |
8408 KB |
Output is correct |
3 |
Correct |
0 ms |
8408 KB |
Output is correct |
4 |
Correct |
0 ms |
8408 KB |
Output is correct |
5 |
Correct |
0 ms |
8408 KB |
Output is correct |
6 |
Correct |
0 ms |
8408 KB |
Output is correct |
7 |
Correct |
0 ms |
8408 KB |
Output is correct |
8 |
Correct |
0 ms |
8408 KB |
Output is correct |
9 |
Correct |
0 ms |
8408 KB |
Output is correct |
10 |
Correct |
0 ms |
8408 KB |
Output is correct |
11 |
Correct |
0 ms |
8408 KB |
Output is correct |
12 |
Correct |
0 ms |
8408 KB |
Output is correct |
13 |
Correct |
1 ms |
8408 KB |
Output is correct |
14 |
Correct |
2 ms |
8408 KB |
Output is correct |
15 |
Correct |
0 ms |
8408 KB |
Output is correct |
16 |
Correct |
0 ms |
8408 KB |
Output is correct |
17 |
Correct |
1 ms |
8408 KB |
Output is correct |
18 |
Correct |
0 ms |
8408 KB |
Output is correct |
19 |
Correct |
1 ms |
8408 KB |
Output is correct |
20 |
Correct |
0 ms |
8408 KB |
Output is correct |
21 |
Correct |
0 ms |
8408 KB |
Output is correct |
22 |
Correct |
0 ms |
8408 KB |
Output is correct |
23 |
Correct |
2 ms |
8408 KB |
Output is correct |
24 |
Correct |
0 ms |
8408 KB |
Output is correct |
25 |
Correct |
0 ms |
8408 KB |
Output is correct |
26 |
Correct |
0 ms |
8408 KB |
Output is correct |
27 |
Correct |
0 ms |
8408 KB |
Output is correct |
28 |
Correct |
0 ms |
8408 KB |
Output is correct |
29 |
Correct |
13 ms |
8408 KB |
Output is correct |
30 |
Correct |
4 ms |
8408 KB |
Output is correct |
31 |
Correct |
8 ms |
8408 KB |
Output is correct |
32 |
Correct |
4 ms |
8608 KB |
Output is correct |
33 |
Correct |
23 ms |
9580 KB |
Output is correct |
34 |
Correct |
13 ms |
9580 KB |
Output is correct |
35 |
Correct |
15 ms |
9220 KB |
Output is correct |
36 |
Correct |
0 ms |
8408 KB |
Output is correct |
37 |
Correct |
8 ms |
9736 KB |
Output is correct |
38 |
Correct |
14 ms |
9248 KB |
Output is correct |
39 |
Correct |
7 ms |
8540 KB |
Output is correct |
40 |
Correct |
0 ms |
8672 KB |
Output is correct |
41 |
Correct |
18 ms |
9248 KB |
Output is correct |
42 |
Correct |
8 ms |
8540 KB |
Output is correct |
43 |
Correct |
3 ms |
8540 KB |
Output is correct |
44 |
Correct |
6 ms |
8540 KB |
Output is correct |
45 |
Correct |
95 ms |
17808 KB |
Output is correct |
46 |
Correct |
60 ms |
17808 KB |
Output is correct |
47 |
Correct |
13 ms |
11060 KB |
Output is correct |
48 |
Correct |
0 ms |
8804 KB |
Output is correct |
49 |
Correct |
5 ms |
8804 KB |
Output is correct |
50 |
Correct |
7 ms |
8672 KB |
Output is correct |
51 |
Correct |
27 ms |
9580 KB |
Output is correct |
52 |
Correct |
25 ms |
10160 KB |
Output is correct |
53 |
Correct |
13 ms |
8936 KB |
Output is correct |
54 |
Correct |
0 ms |
8408 KB |
Output is correct |
55 |
Correct |
4 ms |
8408 KB |
Output is correct |
56 |
Correct |
12 ms |
9332 KB |
Output is correct |
57 |
Correct |
0 ms |
8408 KB |
Output is correct |
58 |
Correct |
12 ms |
9120 KB |
Output is correct |
59 |
Correct |
9 ms |
8540 KB |
Output is correct |
60 |
Correct |
9 ms |
8544 KB |
Output is correct |
61 |
Correct |
7 ms |
8556 KB |
Output is correct |
62 |
Correct |
28 ms |
9332 KB |
Output is correct |
63 |
Correct |
270 ms |
27084 KB |
Output is correct |
64 |
Correct |
328 ms |
27044 KB |
Output is correct |
65 |
Execution timed out |
1000 ms |
45488 KB |
Program timed out |
66 |
Halted |
0 ms |
0 KB |
- |