#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(0,b[0]));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(dis[x]<=y)continue;
dis[x]=y;
++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(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(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;
}
# |
Verdict |
Execution time |
Memory |
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 |
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 |
# |
Verdict |
Execution time |
Memory |
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 |
6 ms |
1916 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2300 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 |
7 ms |
2296 KB |
Output is correct |
15 |
Correct |
6 ms |
2300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1912 KB |
Output is correct |
2 |
Correct |
6 ms |
1912 KB |
Output is correct |
3 |
Correct |
6 ms |
1916 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 |
1912 KB |
Output is correct |
7 |
Correct |
6 ms |
1912 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 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 |
2424 KB |
Output is correct |
15 |
Correct |
7 ms |
2424 KB |
Output is correct |
16 |
Correct |
6 ms |
1912 KB |
Output is correct |
17 |
Correct |
10 ms |
5880 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
1912 KB |
Output is correct |
20 |
Correct |
12 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 |
9720 KB |
Output is correct |
24 |
Correct |
17 ms |
12920 KB |
Output is correct |
25 |
Correct |
12 ms |
10872 KB |
Output is correct |
26 |
Correct |
12 ms |
9976 KB |
Output is correct |
27 |
Correct |
12 ms |
11000 KB |
Output is correct |
28 |
Correct |
14 ms |
12024 KB |
Output is correct |
29 |
Correct |
17 ms |
10616 KB |
Output is correct |
30 |
Correct |
13 ms |
10232 KB |
Output is correct |
31 |
Correct |
15 ms |
10360 KB |
Output is correct |
32 |
Correct |
14 ms |
10488 KB |
Output is correct |
33 |
Correct |
21 ms |
11000 KB |
Output is correct |
34 |
Correct |
24 ms |
10996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
1912 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
6 ms |
2044 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 |
2424 KB |
Output is correct |
15 |
Correct |
7 ms |
2296 KB |
Output is correct |
16 |
Correct |
6 ms |
2040 KB |
Output is correct |
17 |
Correct |
9 ms |
5752 KB |
Output is correct |
18 |
Correct |
6 ms |
1916 KB |
Output is correct |
19 |
Correct |
6 ms |
2040 KB |
Output is correct |
20 |
Correct |
12 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 |
13 ms |
9720 KB |
Output is correct |
24 |
Correct |
15 ms |
12920 KB |
Output is correct |
25 |
Correct |
13 ms |
10872 KB |
Output is correct |
26 |
Correct |
12 ms |
9976 KB |
Output is correct |
27 |
Correct |
12 ms |
11000 KB |
Output is correct |
28 |
Correct |
13 ms |
12028 KB |
Output is correct |
29 |
Correct |
17 ms |
10616 KB |
Output is correct |
30 |
Correct |
15 ms |
10232 KB |
Output is correct |
31 |
Correct |
15 ms |
10376 KB |
Output is correct |
32 |
Correct |
14 ms |
10488 KB |
Output is correct |
33 |
Correct |
21 ms |
11000 KB |
Output is correct |
34 |
Correct |
19 ms |
11000 KB |
Output is correct |
35 |
Correct |
24 ms |
13376 KB |
Output is correct |
36 |
Correct |
11 ms |
7288 KB |
Output is correct |
37 |
Correct |
26 ms |
17004 KB |
Output is correct |
38 |
Correct |
25 ms |
17648 KB |
Output is correct |
39 |
Correct |
13 ms |
2936 KB |
Output is correct |
40 |
Correct |
19 ms |
11260 KB |
Output is correct |
41 |
Correct |
24 ms |
15988 KB |
Output is correct |
42 |
Correct |
16 ms |
10488 KB |
Output is correct |
43 |
Correct |
16 ms |
11384 KB |
Output is correct |
44 |
Correct |
16 ms |
10360 KB |
Output is correct |
45 |
Correct |
63 ms |
14056 KB |
Output is correct |
46 |
Correct |
58 ms |
14060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1908 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 |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
6 ms |
2044 KB |
Output is correct |
10 |
Correct |
7 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 |
2424 KB |
Output is correct |
14 |
Correct |
7 ms |
2424 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 |
12 ms |
10108 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 |
9720 KB |
Output is correct |
24 |
Correct |
15 ms |
12920 KB |
Output is correct |
25 |
Correct |
12 ms |
10872 KB |
Output is correct |
26 |
Correct |
12 ms |
9976 KB |
Output is correct |
27 |
Correct |
13 ms |
11000 KB |
Output is correct |
28 |
Correct |
15 ms |
12024 KB |
Output is correct |
29 |
Correct |
16 ms |
10492 KB |
Output is correct |
30 |
Correct |
13 ms |
10232 KB |
Output is correct |
31 |
Correct |
14 ms |
10376 KB |
Output is correct |
32 |
Correct |
14 ms |
10488 KB |
Output is correct |
33 |
Correct |
22 ms |
11000 KB |
Output is correct |
34 |
Correct |
19 ms |
11000 KB |
Output is correct |
35 |
Correct |
23 ms |
13424 KB |
Output is correct |
36 |
Correct |
11 ms |
7288 KB |
Output is correct |
37 |
Correct |
25 ms |
17008 KB |
Output is correct |
38 |
Correct |
26 ms |
17648 KB |
Output is correct |
39 |
Correct |
13 ms |
3064 KB |
Output is correct |
40 |
Correct |
22 ms |
11260 KB |
Output is correct |
41 |
Correct |
24 ms |
15860 KB |
Output is correct |
42 |
Correct |
15 ms |
10488 KB |
Output is correct |
43 |
Correct |
17 ms |
11384 KB |
Output is correct |
44 |
Correct |
16 ms |
10360 KB |
Output is correct |
45 |
Correct |
61 ms |
14060 KB |
Output is correct |
46 |
Correct |
56 ms |
14056 KB |
Output is correct |
47 |
Correct |
96 ms |
100196 KB |
Output is correct |
48 |
Correct |
12 ms |
2552 KB |
Output is correct |
49 |
Correct |
12 ms |
2424 KB |
Output is correct |
50 |
Correct |
11 ms |
2424 KB |
Output is correct |
51 |
Correct |
150 ms |
186216 KB |
Output is correct |
52 |
Correct |
155 ms |
185580 KB |
Output is correct |
53 |
Correct |
94 ms |
123760 KB |
Output is correct |
54 |
Correct |
49 ms |
62712 KB |
Output is correct |
55 |
Correct |
59 ms |
82796 KB |
Output is correct |
56 |
Correct |
94 ms |
124020 KB |
Output is correct |
57 |
Correct |
85 ms |
120040 KB |
Output is correct |
58 |
Correct |
89 ms |
123896 KB |
Output is correct |
59 |
Correct |
90 ms |
124408 KB |
Output is correct |
60 |
Correct |
93 ms |
124488 KB |
Output is correct |
61 |
Correct |
99 ms |
134904 KB |
Output is correct |
62 |
Correct |
165 ms |
216824 KB |
Output is correct |
63 |
Correct |
481 ms |
194000 KB |
Output is correct |
64 |
Correct |
462 ms |
175696 KB |
Output is correct |
65 |
Correct |
746 ms |
174424 KB |
Output is correct |
66 |
Execution timed out |
1052 ms |
183100 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |