#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 INF=(1LL<<30);
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,b[N],p[N],x,y,dis[N];
vector<ll> P[N],tmp;
vector<pll> v[N];
vector<vector<ll> > lis;
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];
P[p[i]].pb(b[i]);
}
for(ll step=1;step<N;++step){
if(!SZ(P[step]))continue;
lis.clear();lis.resize(step);
tmp.clear();
sort(P[step].begin(),P[step].end());
for(auto i:P[step]){
lis[i%step].pb(i);
tmp.pb(i%step);
}
sort(tmp.begin(),tmp.end());
tmp.resize(unique(tmp.begin(),tmp.end())-tmp.begin());
for(auto i:tmp){
//sort(lis[i].begin(),lis[i].end());
for(ll j=1;j<SZ(lis[i]);++j){
x=lis[i][j-1],y=lis[i][j];
for(ll ii=x;ii<=y;ii+=step){
if(ii!=x)v[x].pb(mp(ii,(ii-x)/step));
if(ii!=y)v[y].pb(mp(ii,(y-ii)/step));
}
}
for(ll ii=lis[i].back()+step;ii<N;ii+=step)x=lis[i].back(),v[x].pb(mp(ii,(ii-x)/step));
for(ll ii=lis[i][0]-step;ii>=0;ii-=step)x=lis[i][0],v[x].pb(mp(ii,(x-ii)/step));
}
}
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;
for(auto j:v[x])if(dis[j.X]>dis[x]+j.Y)pq.push(mp(dis[j.X]=dis[x]+j.Y,j.X));
}
cout<<(dis[b[1]]==INF?-1:dis[b[1]])<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2552 KB |
Output is correct |
2 |
Correct |
9 ms |
2424 KB |
Output is correct |
3 |
Correct |
7 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
12 ms |
2968 KB |
Output is correct |
6 |
Correct |
11 ms |
2928 KB |
Output is correct |
7 |
Correct |
9 ms |
2424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
2556 KB |
Output is correct |
2 |
Correct |
9 ms |
2428 KB |
Output is correct |
3 |
Correct |
7 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
12 ms |
2840 KB |
Output is correct |
6 |
Correct |
11 ms |
2932 KB |
Output is correct |
7 |
Correct |
9 ms |
2552 KB |
Output is correct |
8 |
Correct |
12 ms |
2940 KB |
Output is correct |
9 |
Correct |
18 ms |
3572 KB |
Output is correct |
10 |
Correct |
35 ms |
7176 KB |
Output is correct |
11 |
Correct |
54 ms |
15332 KB |
Output is correct |
12 |
Correct |
17 ms |
3332 KB |
Output is correct |
13 |
Correct |
9 ms |
2560 KB |
Output is correct |
14 |
Correct |
98 ms |
22876 KB |
Output is correct |
15 |
Correct |
101 ms |
23008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
2808 KB |
Output is correct |
2 |
Correct |
9 ms |
2424 KB |
Output is correct |
3 |
Correct |
7 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
12 ms |
2844 KB |
Output is correct |
6 |
Correct |
11 ms |
2932 KB |
Output is correct |
7 |
Correct |
9 ms |
2424 KB |
Output is correct |
8 |
Correct |
12 ms |
2940 KB |
Output is correct |
9 |
Correct |
18 ms |
3568 KB |
Output is correct |
10 |
Correct |
37 ms |
7180 KB |
Output is correct |
11 |
Correct |
54 ms |
15332 KB |
Output is correct |
12 |
Correct |
17 ms |
3324 KB |
Output is correct |
13 |
Correct |
9 ms |
2560 KB |
Output is correct |
14 |
Correct |
96 ms |
22876 KB |
Output is correct |
15 |
Correct |
98 ms |
23004 KB |
Output is correct |
16 |
Correct |
16 ms |
7156 KB |
Output is correct |
17 |
Correct |
39 ms |
8960 KB |
Output is correct |
18 |
Correct |
11 ms |
3192 KB |
Output is correct |
19 |
Correct |
9 ms |
2808 KB |
Output is correct |
20 |
Correct |
11 ms |
2548 KB |
Output is correct |
21 |
Correct |
13 ms |
3448 KB |
Output is correct |
22 |
Correct |
10 ms |
3064 KB |
Output is correct |
23 |
Correct |
21 ms |
3896 KB |
Output is correct |
24 |
Correct |
27 ms |
4760 KB |
Output is correct |
25 |
Correct |
21 ms |
3572 KB |
Output is correct |
26 |
Correct |
19 ms |
3580 KB |
Output is correct |
27 |
Correct |
18 ms |
3716 KB |
Output is correct |
28 |
Correct |
18 ms |
5496 KB |
Output is correct |
29 |
Correct |
58 ms |
12164 KB |
Output is correct |
30 |
Correct |
24 ms |
5756 KB |
Output is correct |
31 |
Correct |
32 ms |
7568 KB |
Output is correct |
32 |
Correct |
25 ms |
6136 KB |
Output is correct |
33 |
Correct |
96 ms |
22740 KB |
Output is correct |
34 |
Correct |
99 ms |
22740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2552 KB |
Output is correct |
2 |
Correct |
9 ms |
2428 KB |
Output is correct |
3 |
Correct |
7 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
13 ms |
2840 KB |
Output is correct |
6 |
Correct |
11 ms |
2932 KB |
Output is correct |
7 |
Correct |
9 ms |
2552 KB |
Output is correct |
8 |
Correct |
13 ms |
2940 KB |
Output is correct |
9 |
Correct |
18 ms |
3568 KB |
Output is correct |
10 |
Correct |
37 ms |
7180 KB |
Output is correct |
11 |
Correct |
54 ms |
15332 KB |
Output is correct |
12 |
Correct |
18 ms |
3460 KB |
Output is correct |
13 |
Correct |
9 ms |
2560 KB |
Output is correct |
14 |
Correct |
103 ms |
22876 KB |
Output is correct |
15 |
Correct |
97 ms |
23004 KB |
Output is correct |
16 |
Correct |
16 ms |
7156 KB |
Output is correct |
17 |
Correct |
38 ms |
8960 KB |
Output is correct |
18 |
Correct |
10 ms |
3192 KB |
Output is correct |
19 |
Correct |
10 ms |
2808 KB |
Output is correct |
20 |
Correct |
10 ms |
2548 KB |
Output is correct |
21 |
Correct |
11 ms |
3448 KB |
Output is correct |
22 |
Correct |
10 ms |
3064 KB |
Output is correct |
23 |
Correct |
20 ms |
4024 KB |
Output is correct |
24 |
Correct |
26 ms |
4760 KB |
Output is correct |
25 |
Correct |
22 ms |
3700 KB |
Output is correct |
26 |
Correct |
20 ms |
3580 KB |
Output is correct |
27 |
Correct |
18 ms |
3716 KB |
Output is correct |
28 |
Correct |
17 ms |
5496 KB |
Output is correct |
29 |
Correct |
57 ms |
12036 KB |
Output is correct |
30 |
Correct |
25 ms |
5756 KB |
Output is correct |
31 |
Correct |
32 ms |
7576 KB |
Output is correct |
32 |
Correct |
24 ms |
6144 KB |
Output is correct |
33 |
Correct |
96 ms |
22740 KB |
Output is correct |
34 |
Correct |
96 ms |
22740 KB |
Output is correct |
35 |
Correct |
86 ms |
26680 KB |
Output is correct |
36 |
Correct |
39 ms |
8112 KB |
Output is correct |
37 |
Correct |
91 ms |
31852 KB |
Output is correct |
38 |
Correct |
94 ms |
30388 KB |
Output is correct |
39 |
Correct |
93 ms |
30448 KB |
Output is correct |
40 |
Correct |
94 ms |
29980 KB |
Output is correct |
41 |
Correct |
96 ms |
30104 KB |
Output is correct |
42 |
Correct |
26 ms |
4116 KB |
Output is correct |
43 |
Correct |
24 ms |
4156 KB |
Output is correct |
44 |
Correct |
16 ms |
3188 KB |
Output is correct |
45 |
Correct |
219 ms |
71144 KB |
Output is correct |
46 |
Correct |
231 ms |
71148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
2680 KB |
Output is correct |
2 |
Correct |
9 ms |
2424 KB |
Output is correct |
3 |
Correct |
6 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
12 ms |
2840 KB |
Output is correct |
6 |
Correct |
11 ms |
2932 KB |
Output is correct |
7 |
Correct |
9 ms |
2552 KB |
Output is correct |
8 |
Correct |
13 ms |
2940 KB |
Output is correct |
9 |
Correct |
17 ms |
3572 KB |
Output is correct |
10 |
Correct |
37 ms |
7176 KB |
Output is correct |
11 |
Correct |
54 ms |
15332 KB |
Output is correct |
12 |
Correct |
17 ms |
3332 KB |
Output is correct |
13 |
Correct |
9 ms |
2560 KB |
Output is correct |
14 |
Correct |
94 ms |
22876 KB |
Output is correct |
15 |
Correct |
95 ms |
23004 KB |
Output is correct |
16 |
Correct |
16 ms |
7156 KB |
Output is correct |
17 |
Correct |
38 ms |
8960 KB |
Output is correct |
18 |
Correct |
11 ms |
3192 KB |
Output is correct |
19 |
Correct |
12 ms |
2808 KB |
Output is correct |
20 |
Correct |
10 ms |
2548 KB |
Output is correct |
21 |
Correct |
11 ms |
3444 KB |
Output is correct |
22 |
Correct |
10 ms |
3064 KB |
Output is correct |
23 |
Correct |
20 ms |
3896 KB |
Output is correct |
24 |
Correct |
27 ms |
4760 KB |
Output is correct |
25 |
Correct |
21 ms |
3572 KB |
Output is correct |
26 |
Correct |
19 ms |
3580 KB |
Output is correct |
27 |
Correct |
18 ms |
3712 KB |
Output is correct |
28 |
Correct |
18 ms |
5492 KB |
Output is correct |
29 |
Correct |
57 ms |
12172 KB |
Output is correct |
30 |
Correct |
24 ms |
5756 KB |
Output is correct |
31 |
Correct |
32 ms |
7576 KB |
Output is correct |
32 |
Correct |
24 ms |
6136 KB |
Output is correct |
33 |
Correct |
97 ms |
22740 KB |
Output is correct |
34 |
Correct |
96 ms |
22740 KB |
Output is correct |
35 |
Correct |
83 ms |
26684 KB |
Output is correct |
36 |
Correct |
40 ms |
8112 KB |
Output is correct |
37 |
Correct |
90 ms |
31732 KB |
Output is correct |
38 |
Correct |
94 ms |
30388 KB |
Output is correct |
39 |
Correct |
93 ms |
30324 KB |
Output is correct |
40 |
Correct |
94 ms |
29844 KB |
Output is correct |
41 |
Correct |
98 ms |
30108 KB |
Output is correct |
42 |
Correct |
25 ms |
4116 KB |
Output is correct |
43 |
Correct |
23 ms |
4156 KB |
Output is correct |
44 |
Correct |
16 ms |
3188 KB |
Output is correct |
45 |
Correct |
221 ms |
71148 KB |
Output is correct |
46 |
Correct |
225 ms |
71396 KB |
Output is correct |
47 |
Correct |
89 ms |
23188 KB |
Output is correct |
48 |
Execution timed out |
1091 ms |
7660 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |