#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;
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 |
108 ms |
196948 KB |
Output is correct |
2 |
Correct |
108 ms |
196944 KB |
Output is correct |
3 |
Correct |
133 ms |
197204 KB |
Output is correct |
4 |
Correct |
119 ms |
197204 KB |
Output is correct |
5 |
Correct |
109 ms |
197056 KB |
Output is correct |
6 |
Correct |
108 ms |
196944 KB |
Output is correct |
7 |
Correct |
109 ms |
197132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
197124 KB |
Output is correct |
2 |
Correct |
110 ms |
196944 KB |
Output is correct |
3 |
Correct |
108 ms |
197204 KB |
Output is correct |
4 |
Correct |
107 ms |
197200 KB |
Output is correct |
5 |
Correct |
107 ms |
196948 KB |
Output is correct |
6 |
Correct |
106 ms |
197196 KB |
Output is correct |
7 |
Correct |
107 ms |
197196 KB |
Output is correct |
8 |
Correct |
107 ms |
197028 KB |
Output is correct |
9 |
Correct |
107 ms |
197160 KB |
Output is correct |
10 |
Correct |
106 ms |
197204 KB |
Output is correct |
11 |
Correct |
108 ms |
197204 KB |
Output is correct |
12 |
Correct |
107 ms |
197196 KB |
Output is correct |
13 |
Correct |
106 ms |
197000 KB |
Output is correct |
14 |
Correct |
112 ms |
197412 KB |
Output is correct |
15 |
Correct |
108 ms |
197360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
197124 KB |
Output is correct |
2 |
Correct |
110 ms |
197144 KB |
Output is correct |
3 |
Correct |
108 ms |
197204 KB |
Output is correct |
4 |
Correct |
132 ms |
196948 KB |
Output is correct |
5 |
Correct |
123 ms |
197200 KB |
Output is correct |
6 |
Correct |
110 ms |
196968 KB |
Output is correct |
7 |
Correct |
107 ms |
197156 KB |
Output is correct |
8 |
Correct |
108 ms |
197200 KB |
Output is correct |
9 |
Correct |
110 ms |
197200 KB |
Output is correct |
10 |
Correct |
132 ms |
197200 KB |
Output is correct |
11 |
Correct |
120 ms |
197168 KB |
Output is correct |
12 |
Correct |
109 ms |
197200 KB |
Output is correct |
13 |
Correct |
117 ms |
197204 KB |
Output is correct |
14 |
Correct |
114 ms |
197208 KB |
Output is correct |
15 |
Correct |
114 ms |
197460 KB |
Output is correct |
16 |
Correct |
108 ms |
197204 KB |
Output is correct |
17 |
Correct |
110 ms |
197204 KB |
Output is correct |
18 |
Correct |
108 ms |
197268 KB |
Output is correct |
19 |
Correct |
112 ms |
197204 KB |
Output is correct |
20 |
Correct |
107 ms |
197224 KB |
Output is correct |
21 |
Correct |
107 ms |
197200 KB |
Output is correct |
22 |
Correct |
107 ms |
197204 KB |
Output is correct |
23 |
Correct |
107 ms |
197328 KB |
Output is correct |
24 |
Correct |
109 ms |
197460 KB |
Output is correct |
25 |
Correct |
108 ms |
197456 KB |
Output is correct |
26 |
Correct |
108 ms |
197204 KB |
Output is correct |
27 |
Correct |
109 ms |
197200 KB |
Output is correct |
28 |
Correct |
107 ms |
197284 KB |
Output is correct |
29 |
Correct |
109 ms |
199504 KB |
Output is correct |
30 |
Correct |
108 ms |
197236 KB |
Output is correct |
31 |
Correct |
110 ms |
197204 KB |
Output is correct |
32 |
Correct |
106 ms |
197204 KB |
Output is correct |
33 |
Correct |
112 ms |
201808 KB |
Output is correct |
34 |
Correct |
112 ms |
201804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
196948 KB |
Output is correct |
2 |
Correct |
108 ms |
196940 KB |
Output is correct |
3 |
Correct |
106 ms |
197200 KB |
Output is correct |
4 |
Correct |
108 ms |
196952 KB |
Output is correct |
5 |
Correct |
107 ms |
197204 KB |
Output is correct |
6 |
Correct |
112 ms |
197196 KB |
Output is correct |
7 |
Correct |
106 ms |
197144 KB |
Output is correct |
8 |
Correct |
109 ms |
197204 KB |
Output is correct |
9 |
Correct |
108 ms |
197156 KB |
Output is correct |
10 |
Correct |
112 ms |
197200 KB |
Output is correct |
11 |
Correct |
109 ms |
197200 KB |
Output is correct |
12 |
Correct |
109 ms |
197012 KB |
Output is correct |
13 |
Correct |
108 ms |
197164 KB |
Output is correct |
14 |
Correct |
114 ms |
197204 KB |
Output is correct |
15 |
Correct |
108 ms |
197200 KB |
Output is correct |
16 |
Correct |
108 ms |
197300 KB |
Output is correct |
17 |
Correct |
110 ms |
197304 KB |
Output is correct |
18 |
Correct |
111 ms |
197460 KB |
Output is correct |
19 |
Correct |
109 ms |
197200 KB |
Output is correct |
20 |
Correct |
107 ms |
197200 KB |
Output is correct |
21 |
Correct |
110 ms |
197204 KB |
Output is correct |
22 |
Correct |
114 ms |
197584 KB |
Output is correct |
23 |
Correct |
110 ms |
197288 KB |
Output is correct |
24 |
Correct |
109 ms |
197460 KB |
Output is correct |
25 |
Correct |
109 ms |
197328 KB |
Output is correct |
26 |
Correct |
106 ms |
197204 KB |
Output is correct |
27 |
Correct |
108 ms |
197204 KB |
Output is correct |
28 |
Correct |
109 ms |
197516 KB |
Output is correct |
29 |
Correct |
111 ms |
199512 KB |
Output is correct |
30 |
Correct |
110 ms |
197168 KB |
Output is correct |
31 |
Correct |
108 ms |
197196 KB |
Output is correct |
32 |
Correct |
107 ms |
197204 KB |
Output is correct |
33 |
Correct |
114 ms |
201808 KB |
Output is correct |
34 |
Correct |
111 ms |
201812 KB |
Output is correct |
35 |
Correct |
125 ms |
204432 KB |
Output is correct |
36 |
Correct |
108 ms |
197716 KB |
Output is correct |
37 |
Correct |
123 ms |
203856 KB |
Output is correct |
38 |
Correct |
134 ms |
205364 KB |
Output is correct |
39 |
Correct |
130 ms |
205140 KB |
Output is correct |
40 |
Correct |
130 ms |
205140 KB |
Output is correct |
41 |
Correct |
131 ms |
205140 KB |
Output is correct |
42 |
Correct |
116 ms |
197700 KB |
Output is correct |
43 |
Correct |
111 ms |
197200 KB |
Output is correct |
44 |
Correct |
117 ms |
197336 KB |
Output is correct |
45 |
Correct |
147 ms |
216660 KB |
Output is correct |
46 |
Correct |
148 ms |
216908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
197200 KB |
Output is correct |
2 |
Correct |
108 ms |
196948 KB |
Output is correct |
3 |
Correct |
107 ms |
196948 KB |
Output is correct |
4 |
Correct |
108 ms |
197168 KB |
Output is correct |
5 |
Correct |
111 ms |
197168 KB |
Output is correct |
6 |
Correct |
106 ms |
197200 KB |
Output is correct |
7 |
Correct |
110 ms |
197148 KB |
Output is correct |
8 |
Correct |
110 ms |
197208 KB |
Output is correct |
9 |
Correct |
109 ms |
197200 KB |
Output is correct |
10 |
Correct |
106 ms |
197200 KB |
Output is correct |
11 |
Correct |
110 ms |
197204 KB |
Output is correct |
12 |
Correct |
108 ms |
197200 KB |
Output is correct |
13 |
Correct |
107 ms |
197204 KB |
Output is correct |
14 |
Correct |
107 ms |
197204 KB |
Output is correct |
15 |
Correct |
110 ms |
197396 KB |
Output is correct |
16 |
Correct |
113 ms |
197484 KB |
Output is correct |
17 |
Correct |
113 ms |
197376 KB |
Output is correct |
18 |
Correct |
109 ms |
197340 KB |
Output is correct |
19 |
Correct |
108 ms |
197200 KB |
Output is correct |
20 |
Correct |
114 ms |
197204 KB |
Output is correct |
21 |
Correct |
109 ms |
197096 KB |
Output is correct |
22 |
Correct |
106 ms |
197260 KB |
Output is correct |
23 |
Correct |
106 ms |
197204 KB |
Output is correct |
24 |
Correct |
108 ms |
197244 KB |
Output is correct |
25 |
Correct |
107 ms |
197460 KB |
Output is correct |
26 |
Correct |
107 ms |
197204 KB |
Output is correct |
27 |
Correct |
109 ms |
197272 KB |
Output is correct |
28 |
Correct |
108 ms |
197320 KB |
Output is correct |
29 |
Correct |
108 ms |
199528 KB |
Output is correct |
30 |
Correct |
107 ms |
197200 KB |
Output is correct |
31 |
Correct |
108 ms |
197344 KB |
Output is correct |
32 |
Correct |
108 ms |
197200 KB |
Output is correct |
33 |
Correct |
113 ms |
201808 KB |
Output is correct |
34 |
Correct |
111 ms |
201820 KB |
Output is correct |
35 |
Correct |
124 ms |
204372 KB |
Output is correct |
36 |
Correct |
111 ms |
197716 KB |
Output is correct |
37 |
Correct |
124 ms |
203856 KB |
Output is correct |
38 |
Correct |
129 ms |
205140 KB |
Output is correct |
39 |
Correct |
129 ms |
205136 KB |
Output is correct |
40 |
Correct |
135 ms |
205396 KB |
Output is correct |
41 |
Correct |
132 ms |
205136 KB |
Output is correct |
42 |
Correct |
112 ms |
197204 KB |
Output is correct |
43 |
Correct |
110 ms |
197144 KB |
Output is correct |
44 |
Correct |
109 ms |
197348 KB |
Output is correct |
45 |
Correct |
142 ms |
216596 KB |
Output is correct |
46 |
Correct |
140 ms |
216740 KB |
Output is correct |
47 |
Correct |
154 ms |
221264 KB |
Output is correct |
48 |
Correct |
125 ms |
206932 KB |
Output is correct |
49 |
Correct |
120 ms |
204256 KB |
Output is correct |
50 |
Correct |
115 ms |
203544 KB |
Output is correct |
51 |
Correct |
136 ms |
210000 KB |
Output is correct |
52 |
Correct |
146 ms |
210236 KB |
Output is correct |
53 |
Correct |
126 ms |
202840 KB |
Output is correct |
54 |
Correct |
107 ms |
197288 KB |
Output is correct |
55 |
Correct |
106 ms |
199256 KB |
Output is correct |
56 |
Correct |
113 ms |
199504 KB |
Output is correct |
57 |
Correct |
121 ms |
211280 KB |
Output is correct |
58 |
Correct |
111 ms |
199588 KB |
Output is correct |
59 |
Correct |
112 ms |
201672 KB |
Output is correct |
60 |
Correct |
115 ms |
201812 KB |
Output is correct |
61 |
Correct |
114 ms |
202264 KB |
Output is correct |
62 |
Correct |
135 ms |
216916 KB |
Output is correct |
63 |
Runtime error |
122 ms |
262144 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |