#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll MOD=1e9+7;
const ll N=3e4+5;
const ll INF=(1LL<<60);
const ll R=1e3+5;
const ld pi=3.14159265359;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()
ll n,m,vis[N][R],dis[N],b[N],p[N],ti,x,y;
vector<ll> v[N];
priority_queue<pll,vector<pll>,greater<pll> > pq;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
REP(i,m){
cin>>b[i]>>p[i];
v[b[i]].pb(p[i]);
}
REP(i,n)sort(v[i].rbegin(),v[i].rend()),v[i].resize(unique(v[i].begin(),v[i].end())-v[i].begin());
REP(i,N)dis[i]=INF;
dis[b[0]]=0;
pq.push(mp(0,b[0]));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(dis[x]!=y)continue;
for(ll step:v[x]){
for(ll i=x+step,tt=1;i<n;i+=step,++tt){
if(step<R&&vis[i][step])break;
if(dis[i]>dis[x]+tt){
pq.push(mp(dis[i]=dis[x]+tt,i));
if(step<R)vis[i][step]=1;
}
}
for(ll i=x-step,tt=1;i>=0;i-=step,++tt){
if(step<R&&vis[i][step])break;
if(dis[i]>dis[x]+tt){
pq.push(mp(dis[i]=dis[x]+tt,i));
if(step<R)vis[i][step]=1;
}
}
}
}
cout<<(dis[b[1]]>=INF?-1:dis[b[1]])<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1272 KB |
Output is correct |
2 |
Correct |
5 ms |
1272 KB |
Output is correct |
3 |
Correct |
5 ms |
1276 KB |
Output is correct |
4 |
Correct |
5 ms |
1272 KB |
Output is correct |
5 |
Correct |
5 ms |
1272 KB |
Output is correct |
6 |
Correct |
5 ms |
1272 KB |
Output is correct |
7 |
Correct |
5 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1272 KB |
Output is correct |
2 |
Correct |
5 ms |
1272 KB |
Output is correct |
3 |
Correct |
5 ms |
1272 KB |
Output is correct |
4 |
Correct |
5 ms |
1272 KB |
Output is correct |
5 |
Correct |
5 ms |
1272 KB |
Output is correct |
6 |
Correct |
5 ms |
1272 KB |
Output is correct |
7 |
Correct |
5 ms |
1272 KB |
Output is correct |
8 |
Correct |
5 ms |
1400 KB |
Output is correct |
9 |
Correct |
5 ms |
1528 KB |
Output is correct |
10 |
Correct |
6 ms |
1832 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1784 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1276 KB |
Output is correct |
2 |
Correct |
5 ms |
1272 KB |
Output is correct |
3 |
Correct |
5 ms |
1272 KB |
Output is correct |
4 |
Correct |
5 ms |
1272 KB |
Output is correct |
5 |
Correct |
5 ms |
1272 KB |
Output is correct |
6 |
Correct |
5 ms |
1272 KB |
Output is correct |
7 |
Correct |
5 ms |
1272 KB |
Output is correct |
8 |
Correct |
5 ms |
1400 KB |
Output is correct |
9 |
Correct |
5 ms |
1528 KB |
Output is correct |
10 |
Correct |
6 ms |
1656 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1784 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
16 |
Correct |
5 ms |
1272 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
1400 KB |
Output is correct |
19 |
Correct |
6 ms |
1400 KB |
Output is correct |
20 |
Correct |
11 ms |
9464 KB |
Output is correct |
21 |
Correct |
6 ms |
1400 KB |
Output is correct |
22 |
Correct |
5 ms |
1272 KB |
Output is correct |
23 |
Correct |
12 ms |
7928 KB |
Output is correct |
24 |
Correct |
13 ms |
9848 KB |
Output is correct |
25 |
Correct |
13 ms |
10488 KB |
Output is correct |
26 |
Correct |
12 ms |
9464 KB |
Output is correct |
27 |
Correct |
12 ms |
9464 KB |
Output is correct |
28 |
Correct |
12 ms |
9464 KB |
Output is correct |
29 |
Correct |
13 ms |
9976 KB |
Output is correct |
30 |
Correct |
12 ms |
9724 KB |
Output is correct |
31 |
Correct |
12 ms |
9976 KB |
Output is correct |
32 |
Correct |
12 ms |
10104 KB |
Output is correct |
33 |
Correct |
13 ms |
10232 KB |
Output is correct |
34 |
Correct |
15 ms |
10232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1272 KB |
Output is correct |
2 |
Correct |
5 ms |
1272 KB |
Output is correct |
3 |
Correct |
5 ms |
1400 KB |
Output is correct |
4 |
Correct |
5 ms |
1272 KB |
Output is correct |
5 |
Correct |
5 ms |
1272 KB |
Output is correct |
6 |
Correct |
5 ms |
1400 KB |
Output is correct |
7 |
Correct |
5 ms |
1272 KB |
Output is correct |
8 |
Correct |
5 ms |
1404 KB |
Output is correct |
9 |
Correct |
5 ms |
1532 KB |
Output is correct |
10 |
Correct |
6 ms |
1656 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1744 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
7 ms |
1784 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
16 |
Correct |
5 ms |
1272 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
1272 KB |
Output is correct |
19 |
Correct |
6 ms |
1400 KB |
Output is correct |
20 |
Correct |
11 ms |
9464 KB |
Output is correct |
21 |
Correct |
5 ms |
1272 KB |
Output is correct |
22 |
Correct |
5 ms |
1272 KB |
Output is correct |
23 |
Correct |
11 ms |
7928 KB |
Output is correct |
24 |
Correct |
13 ms |
9848 KB |
Output is correct |
25 |
Correct |
13 ms |
10488 KB |
Output is correct |
26 |
Correct |
12 ms |
9464 KB |
Output is correct |
27 |
Correct |
11 ms |
9464 KB |
Output is correct |
28 |
Correct |
12 ms |
9464 KB |
Output is correct |
29 |
Correct |
13 ms |
9976 KB |
Output is correct |
30 |
Correct |
12 ms |
9752 KB |
Output is correct |
31 |
Correct |
13 ms |
10004 KB |
Output is correct |
32 |
Correct |
12 ms |
10104 KB |
Output is correct |
33 |
Correct |
14 ms |
10232 KB |
Output is correct |
34 |
Correct |
13 ms |
10232 KB |
Output is correct |
35 |
Correct |
20 ms |
9976 KB |
Output is correct |
36 |
Correct |
13 ms |
7160 KB |
Output is correct |
37 |
Correct |
23 ms |
14072 KB |
Output is correct |
38 |
Correct |
25 ms |
14200 KB |
Output is correct |
39 |
Correct |
24 ms |
13048 KB |
Output is correct |
40 |
Correct |
25 ms |
14328 KB |
Output is correct |
41 |
Correct |
24 ms |
13944 KB |
Output is correct |
42 |
Correct |
16 ms |
10488 KB |
Output is correct |
43 |
Correct |
16 ms |
10360 KB |
Output is correct |
44 |
Correct |
16 ms |
10396 KB |
Output is correct |
45 |
Correct |
23 ms |
12280 KB |
Output is correct |
46 |
Correct |
23 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1272 KB |
Output is correct |
2 |
Correct |
5 ms |
1272 KB |
Output is correct |
3 |
Correct |
5 ms |
1276 KB |
Output is correct |
4 |
Correct |
5 ms |
1272 KB |
Output is correct |
5 |
Correct |
5 ms |
1400 KB |
Output is correct |
6 |
Correct |
5 ms |
1400 KB |
Output is correct |
7 |
Correct |
5 ms |
1272 KB |
Output is correct |
8 |
Correct |
5 ms |
1400 KB |
Output is correct |
9 |
Correct |
5 ms |
1528 KB |
Output is correct |
10 |
Correct |
6 ms |
1656 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1788 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
16 |
Correct |
6 ms |
1272 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
1400 KB |
Output is correct |
19 |
Correct |
5 ms |
1400 KB |
Output is correct |
20 |
Correct |
12 ms |
9464 KB |
Output is correct |
21 |
Correct |
5 ms |
1272 KB |
Output is correct |
22 |
Correct |
5 ms |
1272 KB |
Output is correct |
23 |
Correct |
10 ms |
7928 KB |
Output is correct |
24 |
Correct |
13 ms |
9848 KB |
Output is correct |
25 |
Correct |
12 ms |
10488 KB |
Output is correct |
26 |
Correct |
11 ms |
9464 KB |
Output is correct |
27 |
Correct |
12 ms |
9464 KB |
Output is correct |
28 |
Correct |
12 ms |
9464 KB |
Output is correct |
29 |
Correct |
12 ms |
9976 KB |
Output is correct |
30 |
Correct |
12 ms |
9848 KB |
Output is correct |
31 |
Correct |
12 ms |
9976 KB |
Output is correct |
32 |
Correct |
12 ms |
10104 KB |
Output is correct |
33 |
Correct |
14 ms |
10232 KB |
Output is correct |
34 |
Correct |
14 ms |
10232 KB |
Output is correct |
35 |
Correct |
20 ms |
9976 KB |
Output is correct |
36 |
Correct |
11 ms |
7160 KB |
Output is correct |
37 |
Correct |
22 ms |
13948 KB |
Output is correct |
38 |
Correct |
25 ms |
14200 KB |
Output is correct |
39 |
Correct |
23 ms |
13048 KB |
Output is correct |
40 |
Correct |
24 ms |
14200 KB |
Output is correct |
41 |
Correct |
24 ms |
13944 KB |
Output is correct |
42 |
Correct |
17 ms |
10488 KB |
Output is correct |
43 |
Correct |
16 ms |
10360 KB |
Output is correct |
44 |
Correct |
17 ms |
10396 KB |
Output is correct |
45 |
Correct |
24 ms |
12280 KB |
Output is correct |
46 |
Correct |
23 ms |
12280 KB |
Output is correct |
47 |
Correct |
86 ms |
77168 KB |
Output is correct |
48 |
Correct |
12 ms |
2296 KB |
Output is correct |
49 |
Correct |
12 ms |
2296 KB |
Output is correct |
50 |
Correct |
10 ms |
2040 KB |
Output is correct |
51 |
Correct |
148 ms |
157808 KB |
Output is correct |
52 |
Correct |
147 ms |
143728 KB |
Output is correct |
53 |
Correct |
106 ms |
120564 KB |
Output is correct |
54 |
Correct |
44 ms |
62072 KB |
Output is correct |
55 |
Correct |
58 ms |
82040 KB |
Output is correct |
56 |
Correct |
96 ms |
124020 KB |
Output is correct |
57 |
Correct |
141 ms |
135012 KB |
Output is correct |
58 |
Correct |
90 ms |
122868 KB |
Output is correct |
59 |
Correct |
95 ms |
123380 KB |
Output is correct |
60 |
Correct |
101 ms |
124916 KB |
Output is correct |
61 |
Correct |
107 ms |
123756 KB |
Output is correct |
62 |
Correct |
63 ms |
20216 KB |
Output is correct |
63 |
Correct |
224 ms |
225356 KB |
Output is correct |
64 |
Correct |
233 ms |
208232 KB |
Output is correct |
65 |
Correct |
243 ms |
187884 KB |
Output is correct |
66 |
Correct |
296 ms |
163556 KB |
Output is correct |
67 |
Correct |
298 ms |
163536 KB |
Output is correct |