#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int,int> pp;
int N,M;
int a[30010],P[30010];
int dist[30010];
bool chk[30010];
priority_queue<pp,vector<pp>,greater<pp> > Q;
int main(){
scanf("%d%d",&N,&M);
for(int i=0;i<M;i++){
scanf("%d%d",&a[i],&P[i]);
}
memset(dist,0x3f,sizeof(dist));
int INF=dist[0];
dist[0]=0;
Q.push(pp(0,0));
int v;
while(!Q.empty()){
do{
v=Q.top().second;Q.pop();
}while(!Q.empty()&&chk[v]);
chk[v]=1;
if(v==1)break;
for(int i=0;i<M;i++){
if(abs(a[v]-a[i])%P[v]==0){
if(dist[i]>dist[v]+abs(a[v]-a[i])/P[v]){
dist[i]=dist[v]+abs(a[v]-a[i])/P[v];
Q.push(pp(dist[i],i));
}
}
}
}
if(dist[1]==INF){
puts("-1");
}else{
printf("%d",dist[1]);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1592 KB |
Output is correct |
2 |
Correct |
0 ms |
1592 KB |
Output is correct |
3 |
Correct |
0 ms |
1592 KB |
Output is correct |
4 |
Correct |
0 ms |
1592 KB |
Output is correct |
5 |
Correct |
0 ms |
1592 KB |
Output is correct |
6 |
Correct |
0 ms |
1592 KB |
Output is correct |
7 |
Correct |
0 ms |
1592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1592 KB |
Output is correct |
2 |
Correct |
0 ms |
1592 KB |
Output is correct |
3 |
Correct |
0 ms |
1592 KB |
Output is correct |
4 |
Correct |
0 ms |
1592 KB |
Output is correct |
5 |
Correct |
0 ms |
1592 KB |
Output is correct |
6 |
Correct |
0 ms |
1592 KB |
Output is correct |
7 |
Correct |
0 ms |
1592 KB |
Output is correct |
8 |
Correct |
0 ms |
1592 KB |
Output is correct |
9 |
Correct |
0 ms |
1592 KB |
Output is correct |
10 |
Correct |
0 ms |
1592 KB |
Output is correct |
11 |
Correct |
1 ms |
1592 KB |
Output is correct |
12 |
Correct |
0 ms |
1592 KB |
Output is correct |
13 |
Correct |
25 ms |
1592 KB |
Output is correct |
14 |
Correct |
26 ms |
1592 KB |
Output is correct |
15 |
Correct |
26 ms |
1592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1592 KB |
Output is correct |
2 |
Correct |
0 ms |
1592 KB |
Output is correct |
3 |
Correct |
0 ms |
1592 KB |
Output is correct |
4 |
Correct |
0 ms |
1592 KB |
Output is correct |
5 |
Correct |
0 ms |
1592 KB |
Output is correct |
6 |
Correct |
0 ms |
1592 KB |
Output is correct |
7 |
Correct |
0 ms |
1592 KB |
Output is correct |
8 |
Correct |
0 ms |
1592 KB |
Output is correct |
9 |
Correct |
0 ms |
1592 KB |
Output is correct |
10 |
Correct |
0 ms |
1592 KB |
Output is correct |
11 |
Correct |
0 ms |
1592 KB |
Output is correct |
12 |
Correct |
0 ms |
1592 KB |
Output is correct |
13 |
Correct |
25 ms |
1592 KB |
Output is correct |
14 |
Correct |
26 ms |
1592 KB |
Output is correct |
15 |
Correct |
26 ms |
1592 KB |
Output is correct |
16 |
Correct |
0 ms |
1592 KB |
Output is correct |
17 |
Correct |
13 ms |
1592 KB |
Output is correct |
18 |
Correct |
0 ms |
1592 KB |
Output is correct |
19 |
Correct |
0 ms |
1592 KB |
Output is correct |
20 |
Correct |
25 ms |
1592 KB |
Output is correct |
21 |
Correct |
0 ms |
1592 KB |
Output is correct |
22 |
Correct |
0 ms |
1592 KB |
Output is correct |
23 |
Correct |
5 ms |
1592 KB |
Output is correct |
24 |
Correct |
20 ms |
1592 KB |
Output is correct |
25 |
Correct |
4 ms |
1592 KB |
Output is correct |
26 |
Correct |
0 ms |
1592 KB |
Output is correct |
27 |
Correct |
2 ms |
1592 KB |
Output is correct |
28 |
Correct |
25 ms |
1592 KB |
Output is correct |
29 |
Correct |
4 ms |
1592 KB |
Output is correct |
30 |
Correct |
0 ms |
1592 KB |
Output is correct |
31 |
Correct |
0 ms |
1592 KB |
Output is correct |
32 |
Correct |
0 ms |
1592 KB |
Output is correct |
33 |
Correct |
26 ms |
1592 KB |
Output is correct |
34 |
Correct |
26 ms |
1592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1592 KB |
Output is correct |
2 |
Correct |
0 ms |
1592 KB |
Output is correct |
3 |
Correct |
0 ms |
1592 KB |
Output is correct |
4 |
Correct |
0 ms |
1592 KB |
Output is correct |
5 |
Correct |
0 ms |
1592 KB |
Output is correct |
6 |
Correct |
0 ms |
1592 KB |
Output is correct |
7 |
Correct |
0 ms |
1592 KB |
Output is correct |
8 |
Correct |
0 ms |
1592 KB |
Output is correct |
9 |
Correct |
0 ms |
1592 KB |
Output is correct |
10 |
Correct |
0 ms |
1592 KB |
Output is correct |
11 |
Correct |
0 ms |
1592 KB |
Output is correct |
12 |
Correct |
0 ms |
1592 KB |
Output is correct |
13 |
Correct |
25 ms |
1592 KB |
Output is correct |
14 |
Correct |
26 ms |
1592 KB |
Output is correct |
15 |
Correct |
26 ms |
1592 KB |
Output is correct |
16 |
Correct |
0 ms |
1592 KB |
Output is correct |
17 |
Correct |
13 ms |
1592 KB |
Output is correct |
18 |
Correct |
0 ms |
1592 KB |
Output is correct |
19 |
Correct |
0 ms |
1592 KB |
Output is correct |
20 |
Correct |
25 ms |
1592 KB |
Output is correct |
21 |
Correct |
0 ms |
1592 KB |
Output is correct |
22 |
Correct |
0 ms |
1592 KB |
Output is correct |
23 |
Correct |
5 ms |
1592 KB |
Output is correct |
24 |
Correct |
20 ms |
1592 KB |
Output is correct |
25 |
Correct |
5 ms |
1592 KB |
Output is correct |
26 |
Correct |
0 ms |
1592 KB |
Output is correct |
27 |
Correct |
0 ms |
1592 KB |
Output is correct |
28 |
Correct |
25 ms |
1592 KB |
Output is correct |
29 |
Correct |
4 ms |
1592 KB |
Output is correct |
30 |
Correct |
0 ms |
1592 KB |
Output is correct |
31 |
Correct |
0 ms |
1592 KB |
Output is correct |
32 |
Correct |
0 ms |
1592 KB |
Output is correct |
33 |
Correct |
26 ms |
1592 KB |
Output is correct |
34 |
Correct |
26 ms |
1592 KB |
Output is correct |
35 |
Execution timed out |
1000 ms |
2364 KB |
Program timed out |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1592 KB |
Output is correct |
2 |
Correct |
0 ms |
1592 KB |
Output is correct |
3 |
Correct |
0 ms |
1592 KB |
Output is correct |
4 |
Correct |
0 ms |
1592 KB |
Output is correct |
5 |
Correct |
0 ms |
1592 KB |
Output is correct |
6 |
Correct |
0 ms |
1592 KB |
Output is correct |
7 |
Correct |
0 ms |
1592 KB |
Output is correct |
8 |
Correct |
0 ms |
1592 KB |
Output is correct |
9 |
Correct |
0 ms |
1592 KB |
Output is correct |
10 |
Correct |
0 ms |
1592 KB |
Output is correct |
11 |
Correct |
0 ms |
1592 KB |
Output is correct |
12 |
Correct |
0 ms |
1592 KB |
Output is correct |
13 |
Correct |
25 ms |
1592 KB |
Output is correct |
14 |
Correct |
26 ms |
1592 KB |
Output is correct |
15 |
Correct |
26 ms |
1592 KB |
Output is correct |
16 |
Correct |
0 ms |
1592 KB |
Output is correct |
17 |
Correct |
12 ms |
1592 KB |
Output is correct |
18 |
Correct |
0 ms |
1592 KB |
Output is correct |
19 |
Correct |
0 ms |
1592 KB |
Output is correct |
20 |
Correct |
25 ms |
1592 KB |
Output is correct |
21 |
Correct |
0 ms |
1592 KB |
Output is correct |
22 |
Correct |
0 ms |
1592 KB |
Output is correct |
23 |
Correct |
3 ms |
1592 KB |
Output is correct |
24 |
Correct |
20 ms |
1592 KB |
Output is correct |
25 |
Correct |
4 ms |
1592 KB |
Output is correct |
26 |
Correct |
0 ms |
1592 KB |
Output is correct |
27 |
Correct |
0 ms |
1592 KB |
Output is correct |
28 |
Correct |
21 ms |
1592 KB |
Output is correct |
29 |
Correct |
4 ms |
1592 KB |
Output is correct |
30 |
Correct |
0 ms |
1592 KB |
Output is correct |
31 |
Correct |
0 ms |
1592 KB |
Output is correct |
32 |
Correct |
2 ms |
1592 KB |
Output is correct |
33 |
Correct |
26 ms |
1592 KB |
Output is correct |
34 |
Correct |
26 ms |
1592 KB |
Output is correct |
35 |
Execution timed out |
1000 ms |
2364 KB |
Program timed out |
36 |
Halted |
0 ms |
0 KB |
- |