#include<stdio.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int 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;
q.pop();
for(i=x+y;i<n;i+=y){
b.val=val+(i-x)/y; b.x=i; b.y=n; q.push(b);
}
for(i=x-y;i>=0;i-=y){
b.val=val+(x-i)/y; b.x=i; b.y=n; q.push(b);
}
if(ch[q.top().x][0]) continue;
for(i=0;i<con[x].size();i++){
b.val=val; b.x=x; b.y=con[x][i];
q.push(b);
}
ch[q.top().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; ch[x][0]=1; q.pop();
if(x==a[1].x) break;
for(i=0;i<con[x].size();i++){
b.val=val; b.x=x; b.y=con[x][i];
q.push(b);
}
if(y<=root){
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);
}
else{
for(i=x+y;i<n;i+=y){
b.val=val+(i-x)/y; b.x=i; b.y=n; q.push(b);
}
for(i=x-y;i>=0;i-=y){
b.val=val+(x-i)/y; b.x=i; b.y=n; q.push(b);
}
}
}
if(sw) printf("-1");
else printf("%d",val);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25992 KB |
Output is correct |
2 |
Correct |
0 ms |
25992 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
173452 KB |
Program timed out |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25992 KB |
Output is correct |
2 |
Correct |
0 ms |
25992 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
173452 KB |
Program timed out |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25992 KB |
Output is correct |
2 |
Correct |
0 ms |
25992 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
173452 KB |
Program timed out |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25992 KB |
Output is correct |
2 |
Correct |
0 ms |
25992 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
173452 KB |
Program timed out |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25992 KB |
Output is correct |
2 |
Correct |
0 ms |
25992 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
173452 KB |
Program timed out |
4 |
Halted |
0 ms |
0 KB |
- |