#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll MOD=1e9+7;
const ll N=3e4+5;
const ll R=2e3+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<30);
#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,b[N],p[N],vis[N],ti,x,y,dis[N],ch[N][R];
vector<ll> v[N];
vector<pll> ed[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;
pq.push(mp(dis[b[0]]=0,b[0]));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(dis[x]!=y)continue;
++ti;
for(ll step:v[x]){
for(ll j=x+step,tt=1;j<n;j+=step,++tt){
if(step<R&&ch[j][step])break;
if(vis[j]==ti)continue;
if(step<R)ch[j][step]=1;
vis[j]=ti;
if(tt+dis[x]<dis[j])pq.push(mp(dis[j]=tt+dis[x],j));
}
for(ll j=x-step,tt=1;j>=0;j-=step,++tt){
if(step<R&&ch[j][step])break;
if(vis[j]==ti)continue;
if(step<R)ch[j][step]=1;
vis[j]=ti;
if(tt+dis[x]<dis[j])pq.push(mp(dis[j]=tt+dis[x],j));
}
}
//if(dis[b[1]]!=INF){cout<<dis[b[1]]<<"\n";return 0;}
}
cout<<(dis[b[1]]==INF?-1:dis[b[1]])<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1912 KB |
Output is correct |
2 |
Correct |
5 ms |
1912 KB |
Output is correct |
3 |
Correct |
6 ms |
1912 KB |
Output is correct |
4 |
Correct |
7 ms |
1912 KB |
Output is correct |
5 |
Correct |
6 ms |
1912 KB |
Output is correct |
6 |
Correct |
6 ms |
1912 KB |
Output is correct |
7 |
Correct |
6 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1912 KB |
Output is correct |
2 |
Correct |
6 ms |
1916 KB |
Output is correct |
3 |
Correct |
5 ms |
1912 KB |
Output is correct |
4 |
Correct |
6 ms |
1912 KB |
Output is correct |
5 |
Correct |
6 ms |
1912 KB |
Output is correct |
6 |
Correct |
6 ms |
1912 KB |
Output is correct |
7 |
Correct |
5 ms |
1912 KB |
Output is correct |
8 |
Correct |
7 ms |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2296 KB |
Output is correct |
11 |
Correct |
6 ms |
2296 KB |
Output is correct |
12 |
Correct |
6 ms |
2296 KB |
Output is correct |
13 |
Correct |
6 ms |
2296 KB |
Output is correct |
14 |
Correct |
6 ms |
2296 KB |
Output is correct |
15 |
Correct |
6 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1912 KB |
Output is correct |
2 |
Correct |
6 ms |
1912 KB |
Output is correct |
3 |
Correct |
6 ms |
1912 KB |
Output is correct |
4 |
Correct |
6 ms |
1912 KB |
Output is correct |
5 |
Correct |
5 ms |
1912 KB |
Output is correct |
6 |
Correct |
6 ms |
1912 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
6 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2296 KB |
Output is correct |
11 |
Correct |
6 ms |
2296 KB |
Output is correct |
12 |
Correct |
6 ms |
2296 KB |
Output is correct |
13 |
Correct |
6 ms |
2296 KB |
Output is correct |
14 |
Correct |
6 ms |
2296 KB |
Output is correct |
15 |
Correct |
7 ms |
2296 KB |
Output is correct |
16 |
Correct |
6 ms |
1912 KB |
Output is correct |
17 |
Correct |
9 ms |
5752 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
1912 KB |
Output is correct |
20 |
Correct |
14 ms |
10104 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
6 ms |
1912 KB |
Output is correct |
23 |
Correct |
10 ms |
9848 KB |
Output is correct |
24 |
Correct |
15 ms |
13176 KB |
Output is correct |
25 |
Correct |
15 ms |
13688 KB |
Output is correct |
26 |
Correct |
12 ms |
10488 KB |
Output is correct |
27 |
Correct |
12 ms |
11000 KB |
Output is correct |
28 |
Correct |
13 ms |
12024 KB |
Output is correct |
29 |
Correct |
13 ms |
10360 KB |
Output is correct |
30 |
Correct |
12 ms |
10104 KB |
Output is correct |
31 |
Correct |
13 ms |
10360 KB |
Output is correct |
32 |
Correct |
12 ms |
10360 KB |
Output is correct |
33 |
Correct |
14 ms |
10488 KB |
Output is correct |
34 |
Correct |
14 ms |
10488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1908 KB |
Output is correct |
2 |
Correct |
6 ms |
1912 KB |
Output is correct |
3 |
Correct |
7 ms |
1912 KB |
Output is correct |
4 |
Correct |
6 ms |
1912 KB |
Output is correct |
5 |
Correct |
7 ms |
1912 KB |
Output is correct |
6 |
Correct |
5 ms |
1912 KB |
Output is correct |
7 |
Correct |
6 ms |
1912 KB |
Output is correct |
8 |
Correct |
6 ms |
2044 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2296 KB |
Output is correct |
11 |
Correct |
6 ms |
2296 KB |
Output is correct |
12 |
Correct |
6 ms |
2296 KB |
Output is correct |
13 |
Correct |
6 ms |
2296 KB |
Output is correct |
14 |
Correct |
6 ms |
2296 KB |
Output is correct |
15 |
Correct |
7 ms |
2296 KB |
Output is correct |
16 |
Correct |
6 ms |
1912 KB |
Output is correct |
17 |
Correct |
9 ms |
5752 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
1912 KB |
Output is correct |
20 |
Correct |
11 ms |
10104 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
6 ms |
1912 KB |
Output is correct |
23 |
Correct |
12 ms |
9848 KB |
Output is correct |
24 |
Correct |
15 ms |
13180 KB |
Output is correct |
25 |
Correct |
15 ms |
13688 KB |
Output is correct |
26 |
Correct |
12 ms |
10488 KB |
Output is correct |
27 |
Correct |
12 ms |
11000 KB |
Output is correct |
28 |
Correct |
14 ms |
12024 KB |
Output is correct |
29 |
Correct |
13 ms |
10320 KB |
Output is correct |
30 |
Correct |
12 ms |
10104 KB |
Output is correct |
31 |
Correct |
12 ms |
10360 KB |
Output is correct |
32 |
Correct |
12 ms |
10360 KB |
Output is correct |
33 |
Correct |
13 ms |
10488 KB |
Output is correct |
34 |
Correct |
14 ms |
10488 KB |
Output is correct |
35 |
Correct |
21 ms |
13048 KB |
Output is correct |
36 |
Correct |
12 ms |
8440 KB |
Output is correct |
37 |
Correct |
25 ms |
16632 KB |
Output is correct |
38 |
Correct |
28 ms |
18040 KB |
Output is correct |
39 |
Correct |
26 ms |
18040 KB |
Output is correct |
40 |
Correct |
28 ms |
18040 KB |
Output is correct |
41 |
Correct |
28 ms |
18040 KB |
Output is correct |
42 |
Correct |
16 ms |
10872 KB |
Output is correct |
43 |
Correct |
17 ms |
11384 KB |
Output is correct |
44 |
Correct |
17 ms |
10616 KB |
Output is correct |
45 |
Correct |
24 ms |
12024 KB |
Output is correct |
46 |
Correct |
24 ms |
12024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1912 KB |
Output is correct |
2 |
Correct |
6 ms |
1912 KB |
Output is correct |
3 |
Correct |
6 ms |
1912 KB |
Output is correct |
4 |
Correct |
5 ms |
1912 KB |
Output is correct |
5 |
Correct |
6 ms |
1912 KB |
Output is correct |
6 |
Correct |
5 ms |
1912 KB |
Output is correct |
7 |
Correct |
5 ms |
1912 KB |
Output is correct |
8 |
Correct |
6 ms |
1964 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2296 KB |
Output is correct |
11 |
Correct |
7 ms |
2296 KB |
Output is correct |
12 |
Correct |
6 ms |
2296 KB |
Output is correct |
13 |
Correct |
6 ms |
2296 KB |
Output is correct |
14 |
Correct |
6 ms |
2296 KB |
Output is correct |
15 |
Correct |
7 ms |
2296 KB |
Output is correct |
16 |
Correct |
6 ms |
1912 KB |
Output is correct |
17 |
Correct |
9 ms |
5752 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
1912 KB |
Output is correct |
20 |
Correct |
15 ms |
9976 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
6 ms |
1912 KB |
Output is correct |
23 |
Correct |
12 ms |
9848 KB |
Output is correct |
24 |
Correct |
15 ms |
13180 KB |
Output is correct |
25 |
Correct |
15 ms |
13688 KB |
Output is correct |
26 |
Correct |
13 ms |
10488 KB |
Output is correct |
27 |
Correct |
12 ms |
11000 KB |
Output is correct |
28 |
Correct |
14 ms |
12152 KB |
Output is correct |
29 |
Correct |
13 ms |
10360 KB |
Output is correct |
30 |
Correct |
12 ms |
10104 KB |
Output is correct |
31 |
Correct |
12 ms |
10360 KB |
Output is correct |
32 |
Correct |
12 ms |
10360 KB |
Output is correct |
33 |
Correct |
15 ms |
10488 KB |
Output is correct |
34 |
Correct |
14 ms |
10488 KB |
Output is correct |
35 |
Correct |
21 ms |
13048 KB |
Output is correct |
36 |
Correct |
13 ms |
8568 KB |
Output is correct |
37 |
Correct |
25 ms |
16632 KB |
Output is correct |
38 |
Correct |
29 ms |
18040 KB |
Output is correct |
39 |
Correct |
27 ms |
18096 KB |
Output is correct |
40 |
Correct |
27 ms |
18040 KB |
Output is correct |
41 |
Correct |
26 ms |
18040 KB |
Output is correct |
42 |
Correct |
17 ms |
10872 KB |
Output is correct |
43 |
Correct |
17 ms |
11384 KB |
Output is correct |
44 |
Correct |
17 ms |
10360 KB |
Output is correct |
45 |
Correct |
22 ms |
12024 KB |
Output is correct |
46 |
Correct |
24 ms |
12024 KB |
Output is correct |
47 |
Correct |
104 ms |
99848 KB |
Output is correct |
48 |
Correct |
13 ms |
2552 KB |
Output is correct |
49 |
Correct |
13 ms |
2552 KB |
Output is correct |
50 |
Correct |
11 ms |
2424 KB |
Output is correct |
51 |
Correct |
176 ms |
203248 KB |
Output is correct |
52 |
Correct |
174 ms |
204016 KB |
Output is correct |
53 |
Correct |
102 ms |
124020 KB |
Output is correct |
54 |
Correct |
45 ms |
62712 KB |
Output is correct |
55 |
Correct |
59 ms |
82680 KB |
Output is correct |
56 |
Correct |
94 ms |
124024 KB |
Output is correct |
57 |
Correct |
132 ms |
130152 KB |
Output is correct |
58 |
Correct |
91 ms |
123896 KB |
Output is correct |
59 |
Correct |
94 ms |
125432 KB |
Output is correct |
60 |
Correct |
103 ms |
138360 KB |
Output is correct |
61 |
Correct |
106 ms |
144396 KB |
Output is correct |
62 |
Correct |
174 ms |
216824 KB |
Output is correct |
63 |
Correct |
216 ms |
188524 KB |
Output is correct |
64 |
Correct |
217 ms |
170324 KB |
Output is correct |
65 |
Correct |
236 ms |
160356 KB |
Output is correct |
66 |
Correct |
285 ms |
152172 KB |
Output is correct |
67 |
Correct |
285 ms |
152300 KB |
Output is correct |