#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 = 65535;
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];
bitset<mxv> wei;
int head[mxv];
unsigned short dist[mxv];
int S,T;
int ts;
void add_edge(int a,int b,int w){
ecnt++;
nid[ecnt] = head[a];
to[ecnt] = b;
wei[ecnt] = w;
head[a] = ecnt;
}
bitset<mxv> done;
deque<int> dq;
void BFS(){
dq.resize(mxv*2);
fill(dist,dist+mxv,inf);
dist[S] = 0;
dq.push_back(S);
while(!dq.empty()){
int now = dq.front();
dq.pop_front();
if(done[now])continue;
done[now] = true;
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(nxt);
}
}
else{
if(dist[nxt]>dist[now]+w){
dist[nxt] = dist[now]+w;
dq.push_back(nxt);
}
}
}
}
return;
}
int main(){
ts = clock();
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 |
109 ms |
166624 KB |
Output is correct |
2 |
Correct |
104 ms |
166480 KB |
Output is correct |
3 |
Correct |
109 ms |
166484 KB |
Output is correct |
4 |
Correct |
106 ms |
166480 KB |
Output is correct |
5 |
Correct |
106 ms |
166440 KB |
Output is correct |
6 |
Correct |
106 ms |
166484 KB |
Output is correct |
7 |
Correct |
105 ms |
166480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
166480 KB |
Output is correct |
2 |
Correct |
106 ms |
166428 KB |
Output is correct |
3 |
Correct |
105 ms |
166432 KB |
Output is correct |
4 |
Correct |
114 ms |
166736 KB |
Output is correct |
5 |
Correct |
104 ms |
166480 KB |
Output is correct |
6 |
Correct |
105 ms |
166492 KB |
Output is correct |
7 |
Correct |
108 ms |
166480 KB |
Output is correct |
8 |
Correct |
117 ms |
166736 KB |
Output is correct |
9 |
Correct |
107 ms |
166536 KB |
Output is correct |
10 |
Correct |
105 ms |
166608 KB |
Output is correct |
11 |
Correct |
110 ms |
166864 KB |
Output is correct |
12 |
Correct |
113 ms |
167164 KB |
Output is correct |
13 |
Correct |
106 ms |
166456 KB |
Output is correct |
14 |
Correct |
117 ms |
166672 KB |
Output is correct |
15 |
Correct |
110 ms |
166688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
166432 KB |
Output is correct |
2 |
Correct |
103 ms |
166672 KB |
Output is correct |
3 |
Correct |
106 ms |
166480 KB |
Output is correct |
4 |
Correct |
106 ms |
166480 KB |
Output is correct |
5 |
Correct |
114 ms |
166736 KB |
Output is correct |
6 |
Correct |
108 ms |
166660 KB |
Output is correct |
7 |
Correct |
108 ms |
166676 KB |
Output is correct |
8 |
Correct |
107 ms |
166484 KB |
Output is correct |
9 |
Correct |
108 ms |
166432 KB |
Output is correct |
10 |
Correct |
108 ms |
166484 KB |
Output is correct |
11 |
Correct |
106 ms |
166772 KB |
Output is correct |
12 |
Correct |
104 ms |
166624 KB |
Output is correct |
13 |
Correct |
107 ms |
166484 KB |
Output is correct |
14 |
Correct |
108 ms |
166780 KB |
Output is correct |
15 |
Correct |
107 ms |
166880 KB |
Output is correct |
16 |
Correct |
106 ms |
166780 KB |
Output is correct |
17 |
Correct |
128 ms |
166816 KB |
Output is correct |
18 |
Correct |
111 ms |
166736 KB |
Output is correct |
19 |
Correct |
116 ms |
166736 KB |
Output is correct |
20 |
Correct |
107 ms |
166700 KB |
Output is correct |
21 |
Correct |
106 ms |
166740 KB |
Output is correct |
22 |
Correct |
108 ms |
166564 KB |
Output is correct |
23 |
Correct |
107 ms |
166568 KB |
Output is correct |
24 |
Correct |
106 ms |
166696 KB |
Output is correct |
25 |
Correct |
112 ms |
166940 KB |
Output is correct |
26 |
Correct |
110 ms |
166740 KB |
Output is correct |
27 |
Correct |
106 ms |
166740 KB |
Output is correct |
28 |
Correct |
110 ms |
166884 KB |
Output is correct |
29 |
Correct |
108 ms |
166808 KB |
Output is correct |
30 |
Correct |
108 ms |
166736 KB |
Output is correct |
31 |
Correct |
118 ms |
166776 KB |
Output is correct |
32 |
Correct |
107 ms |
166576 KB |
Output is correct |
33 |
Correct |
116 ms |
169296 KB |
Output is correct |
34 |
Correct |
108 ms |
169228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
166472 KB |
Output is correct |
2 |
Correct |
105 ms |
166736 KB |
Output is correct |
3 |
Correct |
104 ms |
166484 KB |
Output is correct |
4 |
Correct |
105 ms |
166676 KB |
Output is correct |
5 |
Correct |
107 ms |
166612 KB |
Output is correct |
6 |
Correct |
104 ms |
166484 KB |
Output is correct |
7 |
Correct |
115 ms |
166672 KB |
Output is correct |
8 |
Correct |
104 ms |
166608 KB |
Output is correct |
9 |
Correct |
104 ms |
166484 KB |
Output is correct |
10 |
Correct |
108 ms |
166484 KB |
Output is correct |
11 |
Correct |
121 ms |
166616 KB |
Output is correct |
12 |
Correct |
114 ms |
166736 KB |
Output is correct |
13 |
Correct |
108 ms |
166484 KB |
Output is correct |
14 |
Correct |
106 ms |
166740 KB |
Output is correct |
15 |
Correct |
104 ms |
166740 KB |
Output is correct |
16 |
Correct |
107 ms |
166528 KB |
Output is correct |
17 |
Correct |
106 ms |
166736 KB |
Output is correct |
18 |
Correct |
105 ms |
166820 KB |
Output is correct |
19 |
Correct |
119 ms |
166520 KB |
Output is correct |
20 |
Correct |
108 ms |
166472 KB |
Output is correct |
21 |
Correct |
107 ms |
166572 KB |
Output is correct |
22 |
Correct |
105 ms |
166788 KB |
Output is correct |
23 |
Correct |
105 ms |
166816 KB |
Output is correct |
24 |
Correct |
110 ms |
166692 KB |
Output is correct |
25 |
Correct |
113 ms |
166732 KB |
Output is correct |
26 |
Correct |
106 ms |
166740 KB |
Output is correct |
27 |
Correct |
116 ms |
166740 KB |
Output is correct |
28 |
Correct |
108 ms |
166740 KB |
Output is correct |
29 |
Correct |
108 ms |
166860 KB |
Output is correct |
30 |
Correct |
106 ms |
166616 KB |
Output is correct |
31 |
Correct |
106 ms |
166640 KB |
Output is correct |
32 |
Correct |
106 ms |
166736 KB |
Output is correct |
33 |
Correct |
119 ms |
169556 KB |
Output is correct |
34 |
Correct |
135 ms |
169300 KB |
Output is correct |
35 |
Correct |
122 ms |
169812 KB |
Output is correct |
36 |
Correct |
109 ms |
167084 KB |
Output is correct |
37 |
Correct |
128 ms |
173572 KB |
Output is correct |
38 |
Correct |
127 ms |
174420 KB |
Output is correct |
39 |
Correct |
131 ms |
174656 KB |
Output is correct |
40 |
Correct |
128 ms |
174432 KB |
Output is correct |
41 |
Correct |
133 ms |
174632 KB |
Output is correct |
42 |
Correct |
125 ms |
166740 KB |
Output is correct |
43 |
Correct |
111 ms |
166640 KB |
Output is correct |
44 |
Correct |
110 ms |
166600 KB |
Output is correct |
45 |
Correct |
144 ms |
182132 KB |
Output is correct |
46 |
Correct |
142 ms |
181956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
166672 KB |
Output is correct |
2 |
Correct |
116 ms |
166480 KB |
Output is correct |
3 |
Correct |
117 ms |
166468 KB |
Output is correct |
4 |
Correct |
106 ms |
166480 KB |
Output is correct |
5 |
Correct |
105 ms |
166572 KB |
Output is correct |
6 |
Correct |
105 ms |
166536 KB |
Output is correct |
7 |
Correct |
106 ms |
166664 KB |
Output is correct |
8 |
Correct |
105 ms |
166544 KB |
Output is correct |
9 |
Correct |
106 ms |
166480 KB |
Output is correct |
10 |
Correct |
106 ms |
166476 KB |
Output is correct |
11 |
Correct |
117 ms |
166872 KB |
Output is correct |
12 |
Correct |
105 ms |
166480 KB |
Output is correct |
13 |
Correct |
111 ms |
166628 KB |
Output is correct |
14 |
Correct |
108 ms |
166704 KB |
Output is correct |
15 |
Correct |
111 ms |
166740 KB |
Output is correct |
16 |
Correct |
106 ms |
166740 KB |
Output is correct |
17 |
Correct |
106 ms |
166740 KB |
Output is correct |
18 |
Correct |
123 ms |
166812 KB |
Output is correct |
19 |
Correct |
110 ms |
166996 KB |
Output is correct |
20 |
Correct |
106 ms |
166492 KB |
Output is correct |
21 |
Correct |
107 ms |
166740 KB |
Output is correct |
22 |
Correct |
106 ms |
166536 KB |
Output is correct |
23 |
Correct |
106 ms |
166604 KB |
Output is correct |
24 |
Correct |
106 ms |
166696 KB |
Output is correct |
25 |
Correct |
107 ms |
166696 KB |
Output is correct |
26 |
Correct |
107 ms |
166736 KB |
Output is correct |
27 |
Correct |
106 ms |
166736 KB |
Output is correct |
28 |
Correct |
128 ms |
166980 KB |
Output is correct |
29 |
Correct |
106 ms |
166996 KB |
Output is correct |
30 |
Correct |
106 ms |
166524 KB |
Output is correct |
31 |
Correct |
113 ms |
166692 KB |
Output is correct |
32 |
Correct |
124 ms |
166740 KB |
Output is correct |
33 |
Correct |
110 ms |
169292 KB |
Output is correct |
34 |
Correct |
112 ms |
169264 KB |
Output is correct |
35 |
Correct |
134 ms |
170108 KB |
Output is correct |
36 |
Correct |
109 ms |
167044 KB |
Output is correct |
37 |
Correct |
121 ms |
173396 KB |
Output is correct |
38 |
Correct |
133 ms |
174652 KB |
Output is correct |
39 |
Correct |
128 ms |
174416 KB |
Output is correct |
40 |
Correct |
128 ms |
174608 KB |
Output is correct |
41 |
Correct |
130 ms |
174420 KB |
Output is correct |
42 |
Correct |
111 ms |
166712 KB |
Output is correct |
43 |
Correct |
111 ms |
166736 KB |
Output is correct |
44 |
Correct |
108 ms |
166996 KB |
Output is correct |
45 |
Correct |
155 ms |
182100 KB |
Output is correct |
46 |
Correct |
153 ms |
182356 KB |
Output is correct |
47 |
Correct |
163 ms |
188660 KB |
Output is correct |
48 |
Correct |
120 ms |
174048 KB |
Output is correct |
49 |
Correct |
118 ms |
171600 KB |
Output is correct |
50 |
Correct |
114 ms |
170888 KB |
Output is correct |
51 |
Correct |
138 ms |
177380 KB |
Output is correct |
52 |
Correct |
141 ms |
179532 KB |
Output is correct |
53 |
Correct |
131 ms |
170580 KB |
Output is correct |
54 |
Correct |
106 ms |
166740 KB |
Output is correct |
55 |
Correct |
106 ms |
166740 KB |
Output is correct |
56 |
Correct |
112 ms |
166996 KB |
Output is correct |
57 |
Correct |
121 ms |
176724 KB |
Output is correct |
58 |
Correct |
109 ms |
167016 KB |
Output is correct |
59 |
Correct |
111 ms |
169040 KB |
Output is correct |
60 |
Correct |
113 ms |
169300 KB |
Output is correct |
61 |
Correct |
112 ms |
169556 KB |
Output is correct |
62 |
Incorrect |
130 ms |
182272 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |