#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
using namespace std;
typedef pair<int,int> ppair;
vector<int>v[30010];
priority_queue<ppair, vector<ppair>, greater<ppair> >q;
int ch[30010],val,x,y,pos,n,m,t,en,arr[200][30010],d[30010];
int main()
{
int i,j;
scanf("%d %d",&n,&m);
t=sqrt(n);
for(i=1;i<=m;i++){
scanf("%d %d",&x,&y); v[++x].push_back(y);
if(i==1) pos=x;
if(i==2) en=x;
}
for(i=1;i<=n;i++) d[i]=1e9; d[pos]=0; ch[pos]=1;
while(1)
{
for(i=0;i<v[pos].size();i++)
{
val=v[pos][i];
if(val<=t)
{
if(arr[val][pos]) continue;
arr[val][pos]=1;
for(j=pos+val;j<=n;j+=val){
if(d[j]>d[pos]+(j-pos)/val){d[j]=d[pos]+(j-pos)/val; q.push(make_pair(d[pos]+(j-pos)/val,j));}
arr[val][j]=1;
}
for(j=pos-val;j>=1;j-=val){
if(d[j]>d[pos]+(j-pos)/val){d[j]=d[pos]-(j-pos)/val; q.push(make_pair(d[pos]-(j-pos)/val,j));}
arr[val][j]=1;
}
}
else
{
for(j=pos+val;j<=n;j+=val){
if(d[j]>d[pos]+(j-pos)/val){d[j]=d[pos]+(j-pos)/val; q.push(make_pair(d[pos]+(j-pos)/val,j));}
}
for(j=pos-val;j>=1;j-=val){
if(d[j]>d[pos]+(j-pos)/val){d[j]=d[pos]-(j-pos)/val; q.push(make_pair(d[pos]-(j-pos)/val,j));}
}
}
}
while(q.size() && ch[q.top().second]) q.pop();
if(q.size()==0) break;
pos=q.top().second; d[pos]=q.top().first; ch[pos]=1;
if(q.top().second==en) break;
}
if(d[en]==1e9) printf("-1");
else printf("%d",d[en]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
25988 KB |
Output is correct |
2 |
Correct |
0 ms |
25988 KB |
Output is correct |
3 |
Correct |
0 ms |
25988 KB |
Output is correct |
4 |
Correct |
0 ms |
25988 KB |
Output is correct |
5 |
Correct |
0 ms |
25988 KB |
Output is correct |
6 |
Correct |
0 ms |
25988 KB |
Output is correct |
7 |
Correct |
0 ms |
25988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
25988 KB |
Output is correct |
2 |
Correct |
0 ms |
25988 KB |
Output is correct |
3 |
Correct |
0 ms |
25988 KB |
Output is correct |
4 |
Correct |
0 ms |
25988 KB |
Output is correct |
5 |
Correct |
0 ms |
25988 KB |
Output is correct |
6 |
Correct |
0 ms |
25988 KB |
Output is correct |
7 |
Correct |
0 ms |
25988 KB |
Output is correct |
8 |
Correct |
0 ms |
25988 KB |
Output is correct |
9 |
Correct |
0 ms |
25988 KB |
Output is correct |
10 |
Correct |
0 ms |
25988 KB |
Output is correct |
11 |
Correct |
0 ms |
25988 KB |
Output is correct |
12 |
Correct |
0 ms |
25988 KB |
Output is correct |
13 |
Correct |
0 ms |
25988 KB |
Output is correct |
14 |
Correct |
1 ms |
25988 KB |
Output is correct |
15 |
Correct |
0 ms |
25988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
25988 KB |
Output is correct |
2 |
Correct |
0 ms |
25988 KB |
Output is correct |
3 |
Correct |
0 ms |
25988 KB |
Output is correct |
4 |
Correct |
0 ms |
25988 KB |
Output is correct |
5 |
Correct |
0 ms |
25988 KB |
Output is correct |
6 |
Correct |
0 ms |
25988 KB |
Output is correct |
7 |
Correct |
0 ms |
25988 KB |
Output is correct |
8 |
Correct |
0 ms |
25988 KB |
Output is correct |
9 |
Correct |
0 ms |
25988 KB |
Output is correct |
10 |
Correct |
0 ms |
25988 KB |
Output is correct |
11 |
Correct |
0 ms |
25988 KB |
Output is correct |
12 |
Correct |
0 ms |
25988 KB |
Output is correct |
13 |
Correct |
0 ms |
25988 KB |
Output is correct |
14 |
Correct |
0 ms |
25988 KB |
Output is correct |
15 |
Correct |
0 ms |
25988 KB |
Output is correct |
16 |
Correct |
0 ms |
25988 KB |
Output is correct |
17 |
Correct |
2 ms |
26140 KB |
Output is correct |
18 |
Correct |
0 ms |
25988 KB |
Output is correct |
19 |
Correct |
0 ms |
25988 KB |
Output is correct |
20 |
Correct |
0 ms |
25988 KB |
Output is correct |
21 |
Correct |
0 ms |
25988 KB |
Output is correct |
22 |
Correct |
0 ms |
25988 KB |
Output is correct |
23 |
Correct |
0 ms |
25988 KB |
Output is correct |
24 |
Correct |
0 ms |
26156 KB |
Output is correct |
25 |
Correct |
0 ms |
25988 KB |
Output is correct |
26 |
Correct |
0 ms |
25988 KB |
Output is correct |
27 |
Correct |
0 ms |
25988 KB |
Output is correct |
28 |
Correct |
0 ms |
25988 KB |
Output is correct |
29 |
Correct |
4 ms |
26124 KB |
Output is correct |
30 |
Correct |
0 ms |
26120 KB |
Output is correct |
31 |
Correct |
0 ms |
26124 KB |
Output is correct |
32 |
Correct |
0 ms |
26124 KB |
Output is correct |
33 |
Correct |
2 ms |
26132 KB |
Output is correct |
34 |
Correct |
2 ms |
26132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
25988 KB |
Output is correct |
2 |
Correct |
0 ms |
25988 KB |
Output is correct |
3 |
Correct |
0 ms |
25988 KB |
Output is correct |
4 |
Correct |
0 ms |
25988 KB |
Output is correct |
5 |
Correct |
0 ms |
25988 KB |
Output is correct |
6 |
Correct |
0 ms |
25988 KB |
Output is correct |
7 |
Correct |
0 ms |
25988 KB |
Output is correct |
8 |
Correct |
0 ms |
25988 KB |
Output is correct |
9 |
Correct |
0 ms |
25988 KB |
Output is correct |
10 |
Correct |
0 ms |
25988 KB |
Output is correct |
11 |
Correct |
0 ms |
25988 KB |
Output is correct |
12 |
Correct |
0 ms |
25988 KB |
Output is correct |
13 |
Correct |
0 ms |
25988 KB |
Output is correct |
14 |
Correct |
0 ms |
25988 KB |
Output is correct |
15 |
Correct |
0 ms |
25988 KB |
Output is correct |
16 |
Correct |
0 ms |
25988 KB |
Output is correct |
17 |
Correct |
0 ms |
26140 KB |
Output is correct |
18 |
Correct |
1 ms |
25988 KB |
Output is correct |
19 |
Correct |
0 ms |
25988 KB |
Output is correct |
20 |
Correct |
0 ms |
25988 KB |
Output is correct |
21 |
Correct |
0 ms |
25988 KB |
Output is correct |
22 |
Correct |
0 ms |
25988 KB |
Output is correct |
23 |
Correct |
0 ms |
25988 KB |
Output is correct |
24 |
Correct |
0 ms |
26156 KB |
Output is correct |
25 |
Correct |
0 ms |
25988 KB |
Output is correct |
26 |
Correct |
0 ms |
25988 KB |
Output is correct |
27 |
Correct |
0 ms |
25988 KB |
Output is correct |
28 |
Correct |
0 ms |
25988 KB |
Output is correct |
29 |
Correct |
3 ms |
26124 KB |
Output is correct |
30 |
Correct |
0 ms |
26120 KB |
Output is correct |
31 |
Correct |
3 ms |
26124 KB |
Output is correct |
32 |
Correct |
2 ms |
26124 KB |
Output is correct |
33 |
Correct |
2 ms |
26132 KB |
Output is correct |
34 |
Correct |
4 ms |
26132 KB |
Output is correct |
35 |
Correct |
11 ms |
26608 KB |
Output is correct |
36 |
Correct |
2 ms |
25988 KB |
Output is correct |
37 |
Correct |
9 ms |
26932 KB |
Output is correct |
38 |
Correct |
11 ms |
26636 KB |
Output is correct |
39 |
Correct |
0 ms |
26252 KB |
Output is correct |
40 |
Correct |
0 ms |
26252 KB |
Output is correct |
41 |
Correct |
0 ms |
26380 KB |
Output is correct |
42 |
Correct |
0 ms |
26120 KB |
Output is correct |
43 |
Correct |
9 ms |
26120 KB |
Output is correct |
44 |
Correct |
2 ms |
26120 KB |
Output is correct |
45 |
Correct |
10 ms |
26296 KB |
Output is correct |
46 |
Correct |
8 ms |
26292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
25988 KB |
Output is correct |
2 |
Correct |
0 ms |
25988 KB |
Output is correct |
3 |
Correct |
0 ms |
25988 KB |
Output is correct |
4 |
Correct |
0 ms |
25988 KB |
Output is correct |
5 |
Correct |
0 ms |
25988 KB |
Output is correct |
6 |
Correct |
0 ms |
25988 KB |
Output is correct |
7 |
Correct |
0 ms |
25988 KB |
Output is correct |
8 |
Correct |
0 ms |
25988 KB |
Output is correct |
9 |
Correct |
0 ms |
25988 KB |
Output is correct |
10 |
Correct |
0 ms |
25988 KB |
Output is correct |
11 |
Correct |
0 ms |
25988 KB |
Output is correct |
12 |
Correct |
0 ms |
25988 KB |
Output is correct |
13 |
Correct |
0 ms |
25988 KB |
Output is correct |
14 |
Correct |
0 ms |
25988 KB |
Output is correct |
15 |
Correct |
0 ms |
25988 KB |
Output is correct |
16 |
Correct |
0 ms |
25988 KB |
Output is correct |
17 |
Correct |
0 ms |
26140 KB |
Output is correct |
18 |
Correct |
1 ms |
25988 KB |
Output is correct |
19 |
Correct |
0 ms |
25988 KB |
Output is correct |
20 |
Correct |
0 ms |
25988 KB |
Output is correct |
21 |
Correct |
0 ms |
25988 KB |
Output is correct |
22 |
Correct |
0 ms |
25988 KB |
Output is correct |
23 |
Correct |
2 ms |
25988 KB |
Output is correct |
24 |
Correct |
0 ms |
26156 KB |
Output is correct |
25 |
Correct |
0 ms |
25988 KB |
Output is correct |
26 |
Correct |
0 ms |
25988 KB |
Output is correct |
27 |
Correct |
2 ms |
25988 KB |
Output is correct |
28 |
Correct |
0 ms |
25988 KB |
Output is correct |
29 |
Correct |
4 ms |
26124 KB |
Output is correct |
30 |
Correct |
0 ms |
26120 KB |
Output is correct |
31 |
Correct |
3 ms |
26124 KB |
Output is correct |
32 |
Correct |
0 ms |
26124 KB |
Output is correct |
33 |
Correct |
0 ms |
26132 KB |
Output is correct |
34 |
Correct |
0 ms |
26132 KB |
Output is correct |
35 |
Correct |
0 ms |
26608 KB |
Output is correct |
36 |
Correct |
0 ms |
25988 KB |
Output is correct |
37 |
Correct |
10 ms |
26932 KB |
Output is correct |
38 |
Correct |
0 ms |
26636 KB |
Output is correct |
39 |
Correct |
0 ms |
26252 KB |
Output is correct |
40 |
Correct |
6 ms |
26252 KB |
Output is correct |
41 |
Correct |
8 ms |
26380 KB |
Output is correct |
42 |
Correct |
5 ms |
26120 KB |
Output is correct |
43 |
Correct |
0 ms |
26120 KB |
Output is correct |
44 |
Correct |
0 ms |
26120 KB |
Output is correct |
45 |
Correct |
7 ms |
26296 KB |
Output is correct |
46 |
Correct |
7 ms |
26292 KB |
Output is correct |
47 |
Correct |
23 ms |
29408 KB |
Output is correct |
48 |
Correct |
0 ms |
26384 KB |
Output is correct |
49 |
Correct |
0 ms |
26384 KB |
Output is correct |
50 |
Correct |
0 ms |
26252 KB |
Output is correct |
51 |
Correct |
24 ms |
28120 KB |
Output is correct |
52 |
Correct |
25 ms |
28124 KB |
Output is correct |
53 |
Correct |
0 ms |
26776 KB |
Output is correct |
54 |
Correct |
3 ms |
26120 KB |
Output is correct |
55 |
Correct |
3 ms |
26120 KB |
Output is correct |
56 |
Correct |
15 ms |
27312 KB |
Output is correct |
57 |
Correct |
2 ms |
26832 KB |
Output is correct |
58 |
Correct |
11 ms |
26252 KB |
Output is correct |
59 |
Correct |
6 ms |
26252 KB |
Output is correct |
60 |
Correct |
12 ms |
26256 KB |
Output is correct |
61 |
Correct |
4 ms |
26268 KB |
Output is correct |
62 |
Correct |
18 ms |
26912 KB |
Output is correct |
63 |
Correct |
49 ms |
27768 KB |
Output is correct |
64 |
Correct |
56 ms |
29264 KB |
Output is correct |
65 |
Correct |
66 ms |
29276 KB |
Output is correct |
66 |
Correct |
104 ms |
29240 KB |
Output is correct |
67 |
Correct |
84 ms |
29244 KB |
Output is correct |