#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2,popcnt,sse4")
#pragma GCC optimize("O3,unroll-loops")
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int inf = 1e9;
const int mxn = 3e4+10;
const int mxe = mxn*sqrt(mxn)*3;
const int mxv = mxe;
map<pii,vector<int>> mp;
int N,M;
int ecnt,vcnt;
int to[mxe],nid[mxe];
bool wei[mxe];
int head[mxv];
int dist[mxv];
int S,T;
void add_edge(int a,int b,int w){
ecnt++;
nid[ecnt] = head[a];
to[ecnt] = b;
wei[ecnt] = w;
head[a] = ecnt;
}
deque<pii> dq;
void BFS(){
fill(dist,dist+mxv,inf);
dist[S] = 0;
dq.push_back(pii(0,S));
while(!dq.empty()){
auto [d,now] = dq.front();
dq.pop_front();
if(d != dist[now])continue;
for(int eid = head[now];eid;eid = nid[eid]){
int nxt = to[eid],w = wei[eid];
//cout<<now<<' '<<nxt<<":"<<dist[now]+w<<' '<<dist[nxt]<<endl;
if(!w){
if(dist[nxt]>dist[now]){
dist[nxt] = dist[now];
dq.push_front(pii(dist[nxt],nxt));
}
}
else{
if(dist[nxt]>dist[now]+w){
dist[nxt] = dist[now]+w;
dq.push_back(pii(dist[nxt],nxt));
}
}
}
}
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M;
for(int i = 0;i<M;i++){
pii tmp;
cin>>tmp.fs>>tmp.sc;
mp[pii(tmp.sc,tmp.fs%tmp.sc)].push_back(tmp.fs);
if(!i)S = tmp.fs;
if(i == 1)T = tmp.fs;
}
vcnt = N-1;
for(auto &i:mp){
sort(i.sc.rbegin(),i.sc.rend());
for(int j = i.fs.sc;j<N;j+=i.fs.fs){
vcnt++;
add_edge(vcnt,j,0);
while(!i.sc.empty()&&i.sc.back() == j){
add_edge(i.sc.back(),vcnt,0);
i.sc.pop_back();
}
if(j != i.fs.sc){
add_edge(vcnt-1,vcnt,1);
add_edge(vcnt,vcnt-1,1);
}
}
}
BFS();
cout<<(dist[T]>=inf?-1:dist[T])<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
67416 KB |
Output is correct |
2 |
Correct |
10 ms |
67420 KB |
Output is correct |
3 |
Correct |
9 ms |
67420 KB |
Output is correct |
4 |
Correct |
9 ms |
67520 KB |
Output is correct |
5 |
Correct |
11 ms |
67420 KB |
Output is correct |
6 |
Correct |
9 ms |
67568 KB |
Output is correct |
7 |
Correct |
9 ms |
67608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
67416 KB |
Output is correct |
2 |
Correct |
9 ms |
67420 KB |
Output is correct |
3 |
Correct |
9 ms |
67420 KB |
Output is correct |
4 |
Correct |
9 ms |
67420 KB |
Output is correct |
5 |
Correct |
9 ms |
67420 KB |
Output is correct |
6 |
Correct |
9 ms |
67420 KB |
Output is correct |
7 |
Correct |
9 ms |
67420 KB |
Output is correct |
8 |
Correct |
9 ms |
67420 KB |
Output is correct |
9 |
Correct |
9 ms |
67416 KB |
Output is correct |
10 |
Correct |
10 ms |
67676 KB |
Output is correct |
11 |
Correct |
10 ms |
67676 KB |
Output is correct |
12 |
Correct |
9 ms |
67504 KB |
Output is correct |
13 |
Correct |
12 ms |
67672 KB |
Output is correct |
14 |
Correct |
11 ms |
67676 KB |
Output is correct |
15 |
Correct |
10 ms |
67776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
67544 KB |
Output is correct |
2 |
Correct |
9 ms |
67420 KB |
Output is correct |
3 |
Correct |
9 ms |
67420 KB |
Output is correct |
4 |
Correct |
9 ms |
67480 KB |
Output is correct |
5 |
Correct |
9 ms |
67420 KB |
Output is correct |
6 |
Correct |
10 ms |
67476 KB |
Output is correct |
7 |
Correct |
10 ms |
67416 KB |
Output is correct |
8 |
Correct |
9 ms |
67416 KB |
Output is correct |
9 |
Correct |
9 ms |
67416 KB |
Output is correct |
10 |
Correct |
9 ms |
67672 KB |
Output is correct |
11 |
Correct |
10 ms |
67676 KB |
Output is correct |
12 |
Correct |
9 ms |
67672 KB |
Output is correct |
13 |
Correct |
9 ms |
67676 KB |
Output is correct |
14 |
Correct |
11 ms |
67932 KB |
Output is correct |
15 |
Correct |
10 ms |
67676 KB |
Output is correct |
16 |
Correct |
11 ms |
67676 KB |
Output is correct |
17 |
Correct |
11 ms |
67764 KB |
Output is correct |
18 |
Correct |
9 ms |
67676 KB |
Output is correct |
19 |
Correct |
9 ms |
67632 KB |
Output is correct |
20 |
Correct |
9 ms |
67676 KB |
Output is correct |
21 |
Correct |
10 ms |
67676 KB |
Output is correct |
22 |
Correct |
10 ms |
67676 KB |
Output is correct |
23 |
Correct |
10 ms |
67748 KB |
Output is correct |
24 |
Correct |
11 ms |
67932 KB |
Output is correct |
25 |
Correct |
11 ms |
67844 KB |
Output is correct |
26 |
Correct |
9 ms |
67588 KB |
Output is correct |
27 |
Correct |
9 ms |
67676 KB |
Output is correct |
28 |
Correct |
11 ms |
67820 KB |
Output is correct |
29 |
Correct |
12 ms |
69976 KB |
Output is correct |
30 |
Correct |
10 ms |
67676 KB |
Output is correct |
31 |
Correct |
11 ms |
69724 KB |
Output is correct |
32 |
Correct |
10 ms |
67676 KB |
Output is correct |
33 |
Correct |
15 ms |
72284 KB |
Output is correct |
34 |
Correct |
14 ms |
72284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
67416 KB |
Output is correct |
2 |
Correct |
9 ms |
67420 KB |
Output is correct |
3 |
Correct |
9 ms |
67532 KB |
Output is correct |
4 |
Correct |
9 ms |
67420 KB |
Output is correct |
5 |
Correct |
9 ms |
67504 KB |
Output is correct |
6 |
Correct |
12 ms |
67420 KB |
Output is correct |
7 |
Correct |
11 ms |
67416 KB |
Output is correct |
8 |
Correct |
9 ms |
67416 KB |
Output is correct |
9 |
Correct |
9 ms |
67416 KB |
Output is correct |
10 |
Correct |
10 ms |
67676 KB |
Output is correct |
11 |
Correct |
10 ms |
67676 KB |
Output is correct |
12 |
Correct |
9 ms |
67540 KB |
Output is correct |
13 |
Correct |
9 ms |
67676 KB |
Output is correct |
14 |
Correct |
10 ms |
67764 KB |
Output is correct |
15 |
Correct |
10 ms |
67676 KB |
Output is correct |
16 |
Correct |
11 ms |
67676 KB |
Output is correct |
17 |
Correct |
10 ms |
67832 KB |
Output is correct |
18 |
Correct |
10 ms |
67644 KB |
Output is correct |
19 |
Correct |
9 ms |
67632 KB |
Output is correct |
20 |
Correct |
10 ms |
67672 KB |
Output is correct |
21 |
Correct |
9 ms |
67676 KB |
Output is correct |
22 |
Correct |
10 ms |
67672 KB |
Output is correct |
23 |
Correct |
10 ms |
67676 KB |
Output is correct |
24 |
Correct |
10 ms |
67784 KB |
Output is correct |
25 |
Correct |
10 ms |
67672 KB |
Output is correct |
26 |
Correct |
9 ms |
67676 KB |
Output is correct |
27 |
Correct |
10 ms |
67624 KB |
Output is correct |
28 |
Correct |
10 ms |
67840 KB |
Output is correct |
29 |
Correct |
12 ms |
69976 KB |
Output is correct |
30 |
Correct |
10 ms |
67676 KB |
Output is correct |
31 |
Correct |
10 ms |
69724 KB |
Output is correct |
32 |
Correct |
10 ms |
67676 KB |
Output is correct |
33 |
Correct |
14 ms |
72284 KB |
Output is correct |
34 |
Correct |
15 ms |
72284 KB |
Output is correct |
35 |
Correct |
27 ms |
74844 KB |
Output is correct |
36 |
Correct |
12 ms |
68188 KB |
Output is correct |
37 |
Correct |
27 ms |
74588 KB |
Output is correct |
38 |
Correct |
32 ms |
75792 KB |
Output is correct |
39 |
Correct |
32 ms |
75736 KB |
Output is correct |
40 |
Correct |
32 ms |
75612 KB |
Output is correct |
41 |
Correct |
33 ms |
75860 KB |
Output is correct |
42 |
Correct |
13 ms |
67932 KB |
Output is correct |
43 |
Correct |
12 ms |
67872 KB |
Output is correct |
44 |
Correct |
13 ms |
67808 KB |
Output is correct |
45 |
Correct |
47 ms |
87400 KB |
Output is correct |
46 |
Correct |
45 ms |
87376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
67420 KB |
Output is correct |
2 |
Correct |
9 ms |
67488 KB |
Output is correct |
3 |
Correct |
11 ms |
67512 KB |
Output is correct |
4 |
Correct |
9 ms |
67416 KB |
Output is correct |
5 |
Correct |
10 ms |
67420 KB |
Output is correct |
6 |
Correct |
9 ms |
67508 KB |
Output is correct |
7 |
Correct |
9 ms |
67420 KB |
Output is correct |
8 |
Correct |
9 ms |
67480 KB |
Output is correct |
9 |
Correct |
9 ms |
67676 KB |
Output is correct |
10 |
Correct |
9 ms |
67540 KB |
Output is correct |
11 |
Correct |
10 ms |
67676 KB |
Output is correct |
12 |
Correct |
10 ms |
67580 KB |
Output is correct |
13 |
Correct |
9 ms |
67676 KB |
Output is correct |
14 |
Correct |
11 ms |
67784 KB |
Output is correct |
15 |
Correct |
10 ms |
67776 KB |
Output is correct |
16 |
Correct |
10 ms |
67676 KB |
Output is correct |
17 |
Correct |
11 ms |
67932 KB |
Output is correct |
18 |
Correct |
10 ms |
67676 KB |
Output is correct |
19 |
Correct |
10 ms |
67672 KB |
Output is correct |
20 |
Correct |
9 ms |
67676 KB |
Output is correct |
21 |
Correct |
11 ms |
67676 KB |
Output is correct |
22 |
Correct |
9 ms |
67676 KB |
Output is correct |
23 |
Correct |
10 ms |
67676 KB |
Output is correct |
24 |
Correct |
11 ms |
67824 KB |
Output is correct |
25 |
Correct |
10 ms |
67676 KB |
Output is correct |
26 |
Correct |
10 ms |
67676 KB |
Output is correct |
27 |
Correct |
10 ms |
67676 KB |
Output is correct |
28 |
Correct |
11 ms |
67932 KB |
Output is correct |
29 |
Correct |
11 ms |
69980 KB |
Output is correct |
30 |
Correct |
10 ms |
67628 KB |
Output is correct |
31 |
Correct |
11 ms |
69724 KB |
Output is correct |
32 |
Correct |
10 ms |
67676 KB |
Output is correct |
33 |
Correct |
16 ms |
72476 KB |
Output is correct |
34 |
Correct |
14 ms |
72284 KB |
Output is correct |
35 |
Correct |
28 ms |
74836 KB |
Output is correct |
36 |
Correct |
12 ms |
68188 KB |
Output is correct |
37 |
Correct |
26 ms |
74588 KB |
Output is correct |
38 |
Correct |
34 ms |
75724 KB |
Output is correct |
39 |
Correct |
32 ms |
75612 KB |
Output is correct |
40 |
Correct |
32 ms |
75784 KB |
Output is correct |
41 |
Correct |
31 ms |
75744 KB |
Output is correct |
42 |
Correct |
14 ms |
67928 KB |
Output is correct |
43 |
Correct |
12 ms |
67932 KB |
Output is correct |
44 |
Correct |
12 ms |
67796 KB |
Output is correct |
45 |
Correct |
47 ms |
87388 KB |
Output is correct |
46 |
Correct |
48 ms |
87432 KB |
Output is correct |
47 |
Correct |
60 ms |
94032 KB |
Output is correct |
48 |
Correct |
24 ms |
77144 KB |
Output is correct |
49 |
Correct |
22 ms |
74588 KB |
Output is correct |
50 |
Correct |
19 ms |
73820 KB |
Output is correct |
51 |
Correct |
41 ms |
80468 KB |
Output is correct |
52 |
Correct |
41 ms |
80476 KB |
Output is correct |
53 |
Correct |
31 ms |
75356 KB |
Output is correct |
54 |
Correct |
10 ms |
69920 KB |
Output is correct |
55 |
Correct |
10 ms |
69724 KB |
Output is correct |
56 |
Correct |
14 ms |
69976 KB |
Output is correct |
57 |
Correct |
28 ms |
83804 KB |
Output is correct |
58 |
Correct |
14 ms |
72164 KB |
Output is correct |
59 |
Correct |
16 ms |
72212 KB |
Output is correct |
60 |
Correct |
17 ms |
72536 KB |
Output is correct |
61 |
Correct |
16 ms |
72792 KB |
Output is correct |
62 |
Correct |
41 ms |
87300 KB |
Output is correct |
63 |
Correct |
155 ms |
139520 KB |
Output is correct |
64 |
Correct |
165 ms |
155976 KB |
Output is correct |
65 |
Correct |
339 ms |
193556 KB |
Output is correct |
66 |
Execution timed out |
2355 ms |
262144 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |