#include <bits/stdc++.h>
//#include "game.h"
using namespace std;
vector<long long> ind[30001];
long long len[30001];
long long n,m;
long long nah,z;
long long d(){
long long cost[n+1];
for(int i = 0;i<=n;i++)cost[i] = 1e18;
priority_queue<pair<long long,long long>> q1;
q1.push({0,z});
cost[z] = 0;
while(!q1.empty()){
long long co = q1.top().first*-1;
long long no = q1.top().second;
//cout<<lin<<" "<<indx<<"\n";
q1.pop();
if(co>cost[no]) continue ;
for(auto i:ind[no]){
long long c = 1;
for(long long j = no+len[i];j<n;j+=len[i]){
if(cost[j]>cost[no]+c){
cost[j] = cost[no]+c;
q1.push({cost[j]*-1,j});
}
c++;
}
c = 1;
for(long long j = no-len[i];j>=0;j-=len[i]){
if(cost[j]>cost[no]+c){
cost[j] = cost[no]+c;
q1.push({cost[j]*-1,j});
}
c++;
}
}
}
return cost[nah];
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i = 0;i<m;i++){
long long x,b;
cin>>x>>b;
if(i==1)nah = x;
if(i==0)z = x;
len[i] = b;
ind[x].push_back(i);
}
//cout<<z<<" "<<nah<<"\n";
long long val = d();
if(val==1e18)cout<<-1<<"\n";
else cout<<val<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1072 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1040 KB |
Output is correct |
10 |
Correct |
1 ms |
1052 KB |
Output is correct |
11 |
Correct |
1 ms |
1044 KB |
Output is correct |
12 |
Correct |
2 ms |
1044 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1044 KB |
Output is correct |
19 |
Correct |
1 ms |
1044 KB |
Output is correct |
20 |
Correct |
7 ms |
1172 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
1040 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1220 KB |
Output is correct |
26 |
Correct |
4 ms |
1108 KB |
Output is correct |
27 |
Correct |
4 ms |
1108 KB |
Output is correct |
28 |
Correct |
1 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1164 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
1236 KB |
Output is correct |
33 |
Correct |
2 ms |
1236 KB |
Output is correct |
34 |
Correct |
2 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1044 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1036 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1048 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
4 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1048 KB |
Output is correct |
22 |
Correct |
1 ms |
1108 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1180 KB |
Output is correct |
25 |
Correct |
2 ms |
1236 KB |
Output is correct |
26 |
Correct |
4 ms |
1044 KB |
Output is correct |
27 |
Correct |
4 ms |
1048 KB |
Output is correct |
28 |
Correct |
1 ms |
1044 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1164 KB |
Output is correct |
31 |
Correct |
2 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
1236 KB |
Output is correct |
33 |
Correct |
2 ms |
1236 KB |
Output is correct |
34 |
Correct |
3 ms |
1492 KB |
Output is correct |
35 |
Correct |
5 ms |
1928 KB |
Output is correct |
36 |
Correct |
2 ms |
1236 KB |
Output is correct |
37 |
Correct |
5 ms |
1748 KB |
Output is correct |
38 |
Correct |
6 ms |
2132 KB |
Output is correct |
39 |
Correct |
6 ms |
2132 KB |
Output is correct |
40 |
Correct |
6 ms |
2132 KB |
Output is correct |
41 |
Correct |
7 ms |
2080 KB |
Output is correct |
42 |
Correct |
50 ms |
1748 KB |
Output is correct |
43 |
Correct |
49 ms |
1748 KB |
Output is correct |
44 |
Correct |
50 ms |
1788 KB |
Output is correct |
45 |
Correct |
5 ms |
2004 KB |
Output is correct |
46 |
Correct |
5 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1036 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1036 KB |
Output is correct |
17 |
Correct |
1 ms |
1052 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
4 ms |
1140 KB |
Output is correct |
21 |
Correct |
1 ms |
1048 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
1060 KB |
Output is correct |
24 |
Correct |
2 ms |
1088 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
4 ms |
1132 KB |
Output is correct |
27 |
Correct |
4 ms |
1044 KB |
Output is correct |
28 |
Correct |
1 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
2 ms |
1164 KB |
Output is correct |
32 |
Correct |
1 ms |
1236 KB |
Output is correct |
33 |
Correct |
2 ms |
1236 KB |
Output is correct |
34 |
Correct |
2 ms |
1492 KB |
Output is correct |
35 |
Correct |
5 ms |
1944 KB |
Output is correct |
36 |
Correct |
2 ms |
1236 KB |
Output is correct |
37 |
Correct |
5 ms |
1796 KB |
Output is correct |
38 |
Correct |
6 ms |
2132 KB |
Output is correct |
39 |
Correct |
6 ms |
2076 KB |
Output is correct |
40 |
Correct |
6 ms |
2204 KB |
Output is correct |
41 |
Correct |
7 ms |
2196 KB |
Output is correct |
42 |
Correct |
50 ms |
1748 KB |
Output is correct |
43 |
Correct |
51 ms |
1796 KB |
Output is correct |
44 |
Correct |
50 ms |
1788 KB |
Output is correct |
45 |
Correct |
6 ms |
2004 KB |
Output is correct |
46 |
Correct |
5 ms |
2004 KB |
Output is correct |
47 |
Correct |
13 ms |
3156 KB |
Output is correct |
48 |
Correct |
5 ms |
2072 KB |
Output is correct |
49 |
Correct |
5 ms |
2004 KB |
Output is correct |
50 |
Correct |
4 ms |
1876 KB |
Output is correct |
51 |
Correct |
21 ms |
4544 KB |
Output is correct |
52 |
Correct |
21 ms |
3552 KB |
Output is correct |
53 |
Correct |
13 ms |
3536 KB |
Output is correct |
54 |
Correct |
2 ms |
1624 KB |
Output is correct |
55 |
Correct |
2 ms |
1624 KB |
Output is correct |
56 |
Correct |
715 ms |
3288 KB |
Output is correct |
57 |
Correct |
38 ms |
5572 KB |
Output is correct |
58 |
Correct |
703 ms |
2136 KB |
Output is correct |
59 |
Correct |
686 ms |
2264 KB |
Output is correct |
60 |
Correct |
692 ms |
2264 KB |
Output is correct |
61 |
Correct |
617 ms |
2388 KB |
Output is correct |
62 |
Correct |
8 ms |
2644 KB |
Output is correct |
63 |
Correct |
20 ms |
4296 KB |
Output is correct |
64 |
Correct |
19 ms |
4304 KB |
Output is correct |
65 |
Correct |
22 ms |
4296 KB |
Output is correct |
66 |
Correct |
30 ms |
6280 KB |
Output is correct |
67 |
Correct |
30 ms |
6264 KB |
Output is correct |