#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=1505;
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],vv[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]<=tt&&vv[j][step]!=0)break;
if(vis[j]==ti)continue;
if(step<R)ch[j][step]=(vv[j][step]?min(ch[j][step],tt):tt);
vv[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]<=tt&&vv[j][step]!=0)break;
if(vis[j]==ti)continue;
if(step<R)ch[j][step]=(vv[j][step]?min(ch[j][step],tt):tt);
vv[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 |
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 |
6 ms |
2040 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 |
5 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 |
5 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
1912 KB |
Output is correct |
8 |
Correct |
6 ms |
2168 KB |
Output is correct |
9 |
Correct |
5 ms |
2424 KB |
Output is correct |
10 |
Correct |
6 ms |
2808 KB |
Output is correct |
11 |
Correct |
7 ms |
2812 KB |
Output is correct |
12 |
Correct |
7 ms |
2680 KB |
Output is correct |
13 |
Correct |
6 ms |
2680 KB |
Output is correct |
14 |
Correct |
6 ms |
2808 KB |
Output is correct |
15 |
Correct |
6 ms |
2680 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 |
5 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
1912 KB |
Output is correct |
7 |
Correct |
6 ms |
1916 KB |
Output is correct |
8 |
Correct |
6 ms |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2296 KB |
Output is correct |
10 |
Correct |
6 ms |
2680 KB |
Output is correct |
11 |
Correct |
7 ms |
2808 KB |
Output is correct |
12 |
Correct |
6 ms |
2680 KB |
Output is correct |
13 |
Correct |
7 ms |
2680 KB |
Output is correct |
14 |
Correct |
6 ms |
2680 KB |
Output is correct |
15 |
Correct |
6 ms |
2680 KB |
Output is correct |
16 |
Correct |
6 ms |
1912 KB |
Output is correct |
17 |
Correct |
12 ms |
9720 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
2040 KB |
Output is correct |
20 |
Correct |
69 ms |
18088 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
6 ms |
1912 KB |
Output is correct |
23 |
Correct |
17 ms |
16376 KB |
Output is correct |
24 |
Correct |
21 ms |
22136 KB |
Output is correct |
25 |
Correct |
19 ms |
18936 KB |
Output is correct |
26 |
Correct |
19 ms |
18884 KB |
Output is correct |
27 |
Correct |
19 ms |
19448 KB |
Output is correct |
28 |
Correct |
21 ms |
22136 KB |
Output is correct |
29 |
Correct |
19 ms |
18680 KB |
Output is correct |
30 |
Correct |
18 ms |
18424 KB |
Output is correct |
31 |
Correct |
19 ms |
18680 KB |
Output is correct |
32 |
Correct |
18 ms |
18680 KB |
Output is correct |
33 |
Correct |
21 ms |
18940 KB |
Output is correct |
34 |
Correct |
21 ms |
18936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1916 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 |
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 |
6 ms |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2296 KB |
Output is correct |
10 |
Correct |
7 ms |
2680 KB |
Output is correct |
11 |
Correct |
7 ms |
2808 KB |
Output is correct |
12 |
Correct |
7 ms |
2684 KB |
Output is correct |
13 |
Correct |
8 ms |
2808 KB |
Output is correct |
14 |
Correct |
6 ms |
2808 KB |
Output is correct |
15 |
Correct |
7 ms |
2680 KB |
Output is correct |
16 |
Correct |
6 ms |
1912 KB |
Output is correct |
17 |
Correct |
12 ms |
9720 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
7 ms |
2040 KB |
Output is correct |
20 |
Correct |
56 ms |
18040 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
6 ms |
1912 KB |
Output is correct |
23 |
Correct |
16 ms |
16376 KB |
Output is correct |
24 |
Correct |
21 ms |
22136 KB |
Output is correct |
25 |
Correct |
17 ms |
18936 KB |
Output is correct |
26 |
Correct |
18 ms |
18936 KB |
Output is correct |
27 |
Correct |
17 ms |
19448 KB |
Output is correct |
28 |
Correct |
21 ms |
22136 KB |
Output is correct |
29 |
Correct |
20 ms |
18680 KB |
Output is correct |
30 |
Correct |
18 ms |
18424 KB |
Output is correct |
31 |
Correct |
22 ms |
18680 KB |
Output is correct |
32 |
Correct |
19 ms |
18748 KB |
Output is correct |
33 |
Correct |
21 ms |
18936 KB |
Output is correct |
34 |
Correct |
21 ms |
18936 KB |
Output is correct |
35 |
Correct |
27 ms |
19320 KB |
Output is correct |
36 |
Correct |
19 ms |
14176 KB |
Output is correct |
37 |
Correct |
33 ms |
25208 KB |
Output is correct |
38 |
Correct |
35 ms |
25952 KB |
Output is correct |
39 |
Correct |
36 ms |
25976 KB |
Output is correct |
40 |
Correct |
37 ms |
26084 KB |
Output is correct |
41 |
Correct |
34 ms |
26080 KB |
Output is correct |
42 |
Correct |
23 ms |
19320 KB |
Output is correct |
43 |
Correct |
22 ms |
19960 KB |
Output is correct |
44 |
Correct |
61 ms |
18456 KB |
Output is correct |
45 |
Correct |
36 ms |
21880 KB |
Output is correct |
46 |
Correct |
40 ms |
21752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1916 KB |
Output is correct |
2 |
Correct |
7 ms |
1912 KB |
Output is correct |
3 |
Correct |
5 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 |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
5 ms |
1912 KB |
Output is correct |
8 |
Correct |
6 ms |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2424 KB |
Output is correct |
10 |
Correct |
7 ms |
2680 KB |
Output is correct |
11 |
Correct |
7 ms |
2936 KB |
Output is correct |
12 |
Correct |
7 ms |
2808 KB |
Output is correct |
13 |
Correct |
8 ms |
2680 KB |
Output is correct |
14 |
Correct |
7 ms |
2808 KB |
Output is correct |
15 |
Correct |
6 ms |
2808 KB |
Output is correct |
16 |
Correct |
6 ms |
1912 KB |
Output is correct |
17 |
Correct |
12 ms |
9720 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
2040 KB |
Output is correct |
20 |
Correct |
65 ms |
18040 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
7 ms |
1912 KB |
Output is correct |
23 |
Correct |
16 ms |
16376 KB |
Output is correct |
24 |
Correct |
20 ms |
22136 KB |
Output is correct |
25 |
Correct |
19 ms |
18936 KB |
Output is correct |
26 |
Correct |
18 ms |
18936 KB |
Output is correct |
27 |
Correct |
19 ms |
19448 KB |
Output is correct |
28 |
Correct |
21 ms |
22136 KB |
Output is correct |
29 |
Correct |
20 ms |
18680 KB |
Output is correct |
30 |
Correct |
19 ms |
18424 KB |
Output is correct |
31 |
Correct |
24 ms |
18680 KB |
Output is correct |
32 |
Correct |
18 ms |
18680 KB |
Output is correct |
33 |
Correct |
22 ms |
18936 KB |
Output is correct |
34 |
Correct |
22 ms |
19064 KB |
Output is correct |
35 |
Correct |
27 ms |
19320 KB |
Output is correct |
36 |
Correct |
16 ms |
14200 KB |
Output is correct |
37 |
Correct |
40 ms |
25208 KB |
Output is correct |
38 |
Correct |
43 ms |
25976 KB |
Output is correct |
39 |
Correct |
34 ms |
25976 KB |
Output is correct |
40 |
Correct |
37 ms |
26104 KB |
Output is correct |
41 |
Correct |
35 ms |
26104 KB |
Output is correct |
42 |
Correct |
22 ms |
19320 KB |
Output is correct |
43 |
Correct |
22 ms |
19840 KB |
Output is correct |
44 |
Correct |
64 ms |
18424 KB |
Output is correct |
45 |
Correct |
38 ms |
21752 KB |
Output is correct |
46 |
Correct |
37 ms |
21752 KB |
Output is correct |
47 |
Correct |
151 ms |
148336 KB |
Output is correct |
48 |
Correct |
13 ms |
2552 KB |
Output is correct |
49 |
Correct |
13 ms |
2556 KB |
Output is correct |
50 |
Correct |
10 ms |
2424 KB |
Output is correct |
51 |
Runtime error |
192 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
52 |
Halted |
0 ms |
0 KB |
- |