#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[1]=0;
Q.push(pp(0,1));
int v;
while(!Q.empty()){
do{
v=Q.top().second;Q.pop();
}while(!Q.empty()&&chk[v]);
chk[v]=1;
if(v==0)break;
for(int i=0;i<M;i++){
if(abs(a[v]-a[i])%P[i]==0){
if(dist[i]>dist[v]+abs(a[v]-a[i])/P[i]){
dist[i]=dist[v]+abs(a[v]-a[i])/P[i];
Q.push(pp(dist[i],i));
}
}
}
}
if(dist[0]==INF){
puts("-1");
}else{
printf("%d",dist[0]);
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
23 ms |
1592 KB |
Output is correct |
13 |
Correct |
23 ms |
1592 KB |
Output is correct |
14 |
Correct |
0 ms |
1592 KB |
Output is correct |
15 |
Correct |
5 ms |
1592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
1592 KB |
Output is correct |
11 |
Correct |
0 ms |
1592 KB |
Output is correct |
12 |
Correct |
23 ms |
1592 KB |
Output is correct |
13 |
Correct |
23 ms |
1592 KB |
Output is correct |
14 |
Correct |
0 ms |
1592 KB |
Output is correct |
15 |
Correct |
5 ms |
1592 KB |
Output is correct |
16 |
Correct |
5 ms |
1592 KB |
Output is correct |
17 |
Correct |
18 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 |
23 ms |
1592 KB |
Output is correct |
21 |
Correct |
2 ms |
1592 KB |
Output is correct |
22 |
Correct |
1 ms |
1592 KB |
Output is correct |
23 |
Correct |
0 ms |
1592 KB |
Output is correct |
24 |
Correct |
13 ms |
1592 KB |
Output is correct |
25 |
Correct |
4 ms |
1592 KB |
Output is correct |
26 |
Correct |
24 ms |
1592 KB |
Output is correct |
27 |
Correct |
23 ms |
1592 KB |
Output is correct |
28 |
Correct |
12 ms |
1592 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
1592 KB |
Output is correct |
34 |
Correct |
0 ms |
1592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
1592 KB |
Output is correct |
11 |
Correct |
2 ms |
1592 KB |
Output is correct |
12 |
Correct |
23 ms |
1592 KB |
Output is correct |
13 |
Correct |
23 ms |
1592 KB |
Output is correct |
14 |
Correct |
0 ms |
1592 KB |
Output is correct |
15 |
Correct |
5 ms |
1592 KB |
Output is correct |
16 |
Correct |
5 ms |
1592 KB |
Output is correct |
17 |
Correct |
19 ms |
1592 KB |
Output is correct |
18 |
Correct |
3 ms |
1592 KB |
Output is correct |
19 |
Correct |
0 ms |
1592 KB |
Output is correct |
20 |
Correct |
23 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 |
2 ms |
1592 KB |
Output is correct |
24 |
Correct |
13 ms |
1592 KB |
Output is correct |
25 |
Correct |
4 ms |
1592 KB |
Output is correct |
26 |
Correct |
24 ms |
1592 KB |
Output is correct |
27 |
Correct |
24 ms |
1592 KB |
Output is correct |
28 |
Correct |
13 ms |
1592 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
1592 KB |
Output is correct |
34 |
Correct |
0 ms |
1592 KB |
Output is correct |
35 |
Execution timed out |
1000 ms |
1720 KB |
Program timed out |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
1592 KB |
Output is correct |
12 |
Correct |
23 ms |
1592 KB |
Output is correct |
13 |
Correct |
23 ms |
1592 KB |
Output is correct |
14 |
Correct |
0 ms |
1592 KB |
Output is correct |
15 |
Correct |
2 ms |
1592 KB |
Output is correct |
16 |
Correct |
5 ms |
1592 KB |
Output is correct |
17 |
Correct |
18 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 |
23 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 |
0 ms |
1592 KB |
Output is correct |
24 |
Correct |
13 ms |
1592 KB |
Output is correct |
25 |
Correct |
4 ms |
1592 KB |
Output is correct |
26 |
Correct |
24 ms |
1592 KB |
Output is correct |
27 |
Correct |
23 ms |
1592 KB |
Output is correct |
28 |
Correct |
8 ms |
1592 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
1592 KB |
Output is correct |
34 |
Correct |
0 ms |
1592 KB |
Output is correct |
35 |
Execution timed out |
1000 ms |
1720 KB |
Program timed out |
36 |
Halted |
0 ms |
0 KB |
- |