#include <bits/stdc++.h>
using namespace std;
#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)*4;
const int mxv = mxe;
map<pii,vector<int>> mp;
int N,M;
int ecnt,vcnt;
int to[mxe],nid[mxe],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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
87892 KB |
Output is correct |
2 |
Correct |
23 ms |
87892 KB |
Output is correct |
3 |
Correct |
23 ms |
87892 KB |
Output is correct |
4 |
Correct |
23 ms |
87900 KB |
Output is correct |
5 |
Correct |
23 ms |
87812 KB |
Output is correct |
6 |
Correct |
23 ms |
87896 KB |
Output is correct |
7 |
Correct |
23 ms |
87888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
87888 KB |
Output is correct |
2 |
Correct |
25 ms |
87768 KB |
Output is correct |
3 |
Correct |
23 ms |
87892 KB |
Output is correct |
4 |
Correct |
23 ms |
87740 KB |
Output is correct |
5 |
Correct |
27 ms |
87808 KB |
Output is correct |
6 |
Correct |
23 ms |
87820 KB |
Output is correct |
7 |
Correct |
23 ms |
87776 KB |
Output is correct |
8 |
Correct |
23 ms |
87900 KB |
Output is correct |
9 |
Correct |
30 ms |
87804 KB |
Output is correct |
10 |
Correct |
23 ms |
87896 KB |
Output is correct |
11 |
Correct |
24 ms |
88144 KB |
Output is correct |
12 |
Correct |
27 ms |
87900 KB |
Output is correct |
13 |
Correct |
27 ms |
88020 KB |
Output is correct |
14 |
Correct |
24 ms |
88244 KB |
Output is correct |
15 |
Correct |
24 ms |
88156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
87836 KB |
Output is correct |
2 |
Correct |
23 ms |
87824 KB |
Output is correct |
3 |
Correct |
25 ms |
87976 KB |
Output is correct |
4 |
Correct |
23 ms |
87896 KB |
Output is correct |
5 |
Correct |
23 ms |
87896 KB |
Output is correct |
6 |
Correct |
23 ms |
87900 KB |
Output is correct |
7 |
Correct |
23 ms |
87784 KB |
Output is correct |
8 |
Correct |
23 ms |
87824 KB |
Output is correct |
9 |
Correct |
23 ms |
87900 KB |
Output is correct |
10 |
Correct |
23 ms |
87900 KB |
Output is correct |
11 |
Correct |
27 ms |
88152 KB |
Output is correct |
12 |
Correct |
24 ms |
87808 KB |
Output is correct |
13 |
Correct |
23 ms |
88008 KB |
Output is correct |
14 |
Correct |
24 ms |
88156 KB |
Output is correct |
15 |
Correct |
28 ms |
88228 KB |
Output is correct |
16 |
Correct |
23 ms |
87900 KB |
Output is correct |
17 |
Correct |
25 ms |
88148 KB |
Output is correct |
18 |
Correct |
24 ms |
88060 KB |
Output is correct |
19 |
Correct |
23 ms |
87888 KB |
Output is correct |
20 |
Correct |
23 ms |
87768 KB |
Output is correct |
21 |
Correct |
24 ms |
87900 KB |
Output is correct |
22 |
Correct |
24 ms |
87896 KB |
Output is correct |
23 |
Correct |
23 ms |
87892 KB |
Output is correct |
24 |
Correct |
24 ms |
88148 KB |
Output is correct |
25 |
Correct |
25 ms |
88308 KB |
Output is correct |
26 |
Correct |
24 ms |
87896 KB |
Output is correct |
27 |
Correct |
24 ms |
87884 KB |
Output is correct |
28 |
Correct |
25 ms |
90204 KB |
Output is correct |
29 |
Correct |
25 ms |
90368 KB |
Output is correct |
30 |
Correct |
24 ms |
89948 KB |
Output is correct |
31 |
Correct |
28 ms |
90220 KB |
Output is correct |
32 |
Correct |
27 ms |
90196 KB |
Output is correct |
33 |
Correct |
28 ms |
92796 KB |
Output is correct |
34 |
Correct |
30 ms |
92612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
87892 KB |
Output is correct |
2 |
Correct |
23 ms |
87848 KB |
Output is correct |
3 |
Correct |
23 ms |
87888 KB |
Output is correct |
4 |
Correct |
30 ms |
88004 KB |
Output is correct |
5 |
Correct |
28 ms |
87744 KB |
Output is correct |
6 |
Correct |
23 ms |
87892 KB |
Output is correct |
7 |
Correct |
23 ms |
87836 KB |
Output is correct |
8 |
Correct |
24 ms |
87808 KB |
Output is correct |
9 |
Correct |
23 ms |
87900 KB |
Output is correct |
10 |
Correct |
23 ms |
87876 KB |
Output is correct |
11 |
Correct |
24 ms |
88152 KB |
Output is correct |
12 |
Correct |
23 ms |
87896 KB |
Output is correct |
13 |
Correct |
23 ms |
87764 KB |
Output is correct |
14 |
Correct |
31 ms |
88156 KB |
Output is correct |
15 |
Correct |
24 ms |
88148 KB |
Output is correct |
16 |
Correct |
24 ms |
88072 KB |
Output is correct |
17 |
Correct |
25 ms |
88028 KB |
Output is correct |
18 |
Correct |
23 ms |
87900 KB |
Output is correct |
19 |
Correct |
25 ms |
87900 KB |
Output is correct |
20 |
Correct |
23 ms |
87900 KB |
Output is correct |
21 |
Correct |
23 ms |
87892 KB |
Output is correct |
22 |
Correct |
23 ms |
88148 KB |
Output is correct |
23 |
Correct |
23 ms |
87880 KB |
Output is correct |
24 |
Correct |
25 ms |
88156 KB |
Output is correct |
25 |
Correct |
24 ms |
88156 KB |
Output is correct |
26 |
Correct |
23 ms |
87892 KB |
Output is correct |
27 |
Correct |
23 ms |
87900 KB |
Output is correct |
28 |
Correct |
25 ms |
90200 KB |
Output is correct |
29 |
Correct |
26 ms |
90388 KB |
Output is correct |
30 |
Correct |
24 ms |
89944 KB |
Output is correct |
31 |
Correct |
25 ms |
90068 KB |
Output is correct |
32 |
Correct |
24 ms |
90204 KB |
Output is correct |
33 |
Correct |
29 ms |
92760 KB |
Output is correct |
34 |
Correct |
29 ms |
93008 KB |
Output is correct |
35 |
Correct |
51 ms |
95220 KB |
Output is correct |
36 |
Correct |
30 ms |
90640 KB |
Output is correct |
37 |
Correct |
40 ms |
96848 KB |
Output is correct |
38 |
Correct |
46 ms |
98132 KB |
Output is correct |
39 |
Correct |
47 ms |
98132 KB |
Output is correct |
40 |
Correct |
46 ms |
98388 KB |
Output is correct |
41 |
Correct |
46 ms |
98132 KB |
Output is correct |
42 |
Correct |
31 ms |
90200 KB |
Output is correct |
43 |
Correct |
30 ms |
90204 KB |
Output is correct |
44 |
Correct |
26 ms |
88156 KB |
Output is correct |
45 |
Correct |
67 ms |
109652 KB |
Output is correct |
46 |
Correct |
66 ms |
109836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
87900 KB |
Output is correct |
2 |
Correct |
26 ms |
87760 KB |
Output is correct |
3 |
Correct |
26 ms |
87892 KB |
Output is correct |
4 |
Correct |
23 ms |
87832 KB |
Output is correct |
5 |
Correct |
23 ms |
87900 KB |
Output is correct |
6 |
Correct |
23 ms |
87816 KB |
Output is correct |
7 |
Correct |
24 ms |
87896 KB |
Output is correct |
8 |
Correct |
23 ms |
87896 KB |
Output is correct |
9 |
Correct |
23 ms |
87980 KB |
Output is correct |
10 |
Correct |
27 ms |
88032 KB |
Output is correct |
11 |
Correct |
26 ms |
87984 KB |
Output is correct |
12 |
Correct |
28 ms |
87812 KB |
Output is correct |
13 |
Correct |
24 ms |
87996 KB |
Output is correct |
14 |
Correct |
24 ms |
88268 KB |
Output is correct |
15 |
Correct |
24 ms |
88268 KB |
Output is correct |
16 |
Correct |
24 ms |
87900 KB |
Output is correct |
17 |
Correct |
24 ms |
88156 KB |
Output is correct |
18 |
Correct |
23 ms |
87920 KB |
Output is correct |
19 |
Correct |
23 ms |
87900 KB |
Output is correct |
20 |
Correct |
23 ms |
87804 KB |
Output is correct |
21 |
Correct |
24 ms |
87900 KB |
Output is correct |
22 |
Correct |
23 ms |
87900 KB |
Output is correct |
23 |
Correct |
24 ms |
87900 KB |
Output is correct |
24 |
Correct |
24 ms |
88276 KB |
Output is correct |
25 |
Correct |
26 ms |
88156 KB |
Output is correct |
26 |
Correct |
25 ms |
87896 KB |
Output is correct |
27 |
Correct |
24 ms |
87892 KB |
Output is correct |
28 |
Correct |
25 ms |
90376 KB |
Output is correct |
29 |
Correct |
25 ms |
90204 KB |
Output is correct |
30 |
Correct |
31 ms |
90108 KB |
Output is correct |
31 |
Correct |
26 ms |
90060 KB |
Output is correct |
32 |
Correct |
25 ms |
90192 KB |
Output is correct |
33 |
Correct |
29 ms |
92756 KB |
Output is correct |
34 |
Correct |
28 ms |
92752 KB |
Output is correct |
35 |
Correct |
41 ms |
95340 KB |
Output is correct |
36 |
Correct |
26 ms |
90460 KB |
Output is correct |
37 |
Correct |
40 ms |
96872 KB |
Output is correct |
38 |
Correct |
46 ms |
98132 KB |
Output is correct |
39 |
Correct |
59 ms |
98132 KB |
Output is correct |
40 |
Correct |
46 ms |
97988 KB |
Output is correct |
41 |
Correct |
45 ms |
98132 KB |
Output is correct |
42 |
Correct |
27 ms |
90196 KB |
Output is correct |
43 |
Correct |
29 ms |
90200 KB |
Output is correct |
44 |
Correct |
27 ms |
88144 KB |
Output is correct |
45 |
Correct |
73 ms |
109652 KB |
Output is correct |
46 |
Correct |
77 ms |
109812 KB |
Output is correct |
47 |
Correct |
81 ms |
120400 KB |
Output is correct |
48 |
Correct |
37 ms |
99656 KB |
Output is correct |
49 |
Correct |
41 ms |
97196 KB |
Output is correct |
50 |
Correct |
32 ms |
96348 KB |
Output is correct |
51 |
Correct |
55 ms |
102740 KB |
Output is correct |
52 |
Correct |
57 ms |
105044 KB |
Output is correct |
53 |
Correct |
43 ms |
95828 KB |
Output is correct |
54 |
Correct |
23 ms |
89936 KB |
Output is correct |
55 |
Correct |
24 ms |
90204 KB |
Output is correct |
56 |
Correct |
28 ms |
90452 KB |
Output is correct |
57 |
Correct |
42 ms |
106516 KB |
Output is correct |
58 |
Correct |
28 ms |
92620 KB |
Output is correct |
59 |
Correct |
32 ms |
92500 KB |
Output is correct |
60 |
Correct |
30 ms |
95012 KB |
Output is correct |
61 |
Correct |
31 ms |
93212 KB |
Output is correct |
62 |
Correct |
56 ms |
111600 KB |
Output is correct |
63 |
Correct |
189 ms |
180820 KB |
Output is correct |
64 |
Correct |
188 ms |
200820 KB |
Output is correct |
65 |
Correct |
455 ms |
248656 KB |
Output is correct |
66 |
Runtime error |
104 ms |
262144 KB |
Execution killed with signal 9 |
67 |
Halted |
0 ms |
0 KB |
- |