#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+10;
const int mxn = 3e4+10;
const int mxe = mxn*sqrt(mxn)*3;
const int mxv = mxe/3;
map<pii,vector<int>> mp;
int N,M;
int ecnt,vcnt;
int to[mxe],nid[mxe];
bitset<mxv> wei;
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 |
42 ms |
67972 KB |
Output is correct |
2 |
Correct |
40 ms |
67928 KB |
Output is correct |
3 |
Correct |
40 ms |
67916 KB |
Output is correct |
4 |
Correct |
40 ms |
67932 KB |
Output is correct |
5 |
Correct |
39 ms |
67932 KB |
Output is correct |
6 |
Correct |
40 ms |
67924 KB |
Output is correct |
7 |
Correct |
41 ms |
67920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
67916 KB |
Output is correct |
2 |
Correct |
42 ms |
67876 KB |
Output is correct |
3 |
Correct |
41 ms |
67928 KB |
Output is correct |
4 |
Correct |
42 ms |
67932 KB |
Output is correct |
5 |
Correct |
40 ms |
67916 KB |
Output is correct |
6 |
Correct |
40 ms |
67928 KB |
Output is correct |
7 |
Correct |
40 ms |
67924 KB |
Output is correct |
8 |
Correct |
51 ms |
67984 KB |
Output is correct |
9 |
Correct |
50 ms |
67928 KB |
Output is correct |
10 |
Correct |
41 ms |
67976 KB |
Output is correct |
11 |
Correct |
41 ms |
67928 KB |
Output is correct |
12 |
Correct |
44 ms |
67924 KB |
Output is correct |
13 |
Correct |
40 ms |
67932 KB |
Output is correct |
14 |
Correct |
41 ms |
68176 KB |
Output is correct |
15 |
Correct |
41 ms |
68176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
67928 KB |
Output is correct |
2 |
Correct |
40 ms |
67976 KB |
Output is correct |
3 |
Correct |
40 ms |
67916 KB |
Output is correct |
4 |
Correct |
40 ms |
67928 KB |
Output is correct |
5 |
Correct |
45 ms |
67932 KB |
Output is correct |
6 |
Correct |
41 ms |
67952 KB |
Output is correct |
7 |
Correct |
40 ms |
67932 KB |
Output is correct |
8 |
Correct |
40 ms |
67932 KB |
Output is correct |
9 |
Correct |
39 ms |
67928 KB |
Output is correct |
10 |
Correct |
40 ms |
67924 KB |
Output is correct |
11 |
Correct |
41 ms |
67924 KB |
Output is correct |
12 |
Correct |
47 ms |
67932 KB |
Output is correct |
13 |
Correct |
39 ms |
67928 KB |
Output is correct |
14 |
Correct |
40 ms |
68184 KB |
Output is correct |
15 |
Correct |
40 ms |
68164 KB |
Output is correct |
16 |
Correct |
39 ms |
67920 KB |
Output is correct |
17 |
Correct |
42 ms |
68180 KB |
Output is correct |
18 |
Correct |
40 ms |
67928 KB |
Output is correct |
19 |
Correct |
41 ms |
67924 KB |
Output is correct |
20 |
Correct |
41 ms |
67932 KB |
Output is correct |
21 |
Correct |
41 ms |
67924 KB |
Output is correct |
22 |
Correct |
41 ms |
67924 KB |
Output is correct |
23 |
Correct |
41 ms |
67928 KB |
Output is correct |
24 |
Correct |
41 ms |
68176 KB |
Output is correct |
25 |
Correct |
42 ms |
68176 KB |
Output is correct |
26 |
Correct |
40 ms |
68048 KB |
Output is correct |
27 |
Correct |
40 ms |
67920 KB |
Output is correct |
28 |
Correct |
42 ms |
68180 KB |
Output is correct |
29 |
Correct |
42 ms |
68188 KB |
Output is correct |
30 |
Correct |
40 ms |
67956 KB |
Output is correct |
31 |
Correct |
40 ms |
68188 KB |
Output is correct |
32 |
Correct |
41 ms |
67928 KB |
Output is correct |
33 |
Correct |
45 ms |
70748 KB |
Output is correct |
34 |
Correct |
44 ms |
70748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
67924 KB |
Output is correct |
2 |
Correct |
41 ms |
67920 KB |
Output is correct |
3 |
Correct |
40 ms |
67924 KB |
Output is correct |
4 |
Correct |
40 ms |
67932 KB |
Output is correct |
5 |
Correct |
40 ms |
67924 KB |
Output is correct |
6 |
Correct |
40 ms |
67932 KB |
Output is correct |
7 |
Correct |
40 ms |
67928 KB |
Output is correct |
8 |
Correct |
47 ms |
67920 KB |
Output is correct |
9 |
Correct |
42 ms |
67868 KB |
Output is correct |
10 |
Correct |
40 ms |
67924 KB |
Output is correct |
11 |
Correct |
41 ms |
68172 KB |
Output is correct |
12 |
Correct |
40 ms |
67932 KB |
Output is correct |
13 |
Correct |
41 ms |
67920 KB |
Output is correct |
14 |
Correct |
42 ms |
68184 KB |
Output is correct |
15 |
Correct |
40 ms |
68188 KB |
Output is correct |
16 |
Correct |
40 ms |
67928 KB |
Output is correct |
17 |
Correct |
41 ms |
68188 KB |
Output is correct |
18 |
Correct |
40 ms |
68076 KB |
Output is correct |
19 |
Correct |
39 ms |
67928 KB |
Output is correct |
20 |
Correct |
41 ms |
67932 KB |
Output is correct |
21 |
Correct |
47 ms |
68120 KB |
Output is correct |
22 |
Correct |
41 ms |
67924 KB |
Output is correct |
23 |
Correct |
40 ms |
67932 KB |
Output is correct |
24 |
Correct |
41 ms |
68176 KB |
Output is correct |
25 |
Correct |
43 ms |
68440 KB |
Output is correct |
26 |
Correct |
40 ms |
67932 KB |
Output is correct |
27 |
Correct |
41 ms |
67920 KB |
Output is correct |
28 |
Correct |
41 ms |
68176 KB |
Output is correct |
29 |
Correct |
43 ms |
68184 KB |
Output is correct |
30 |
Correct |
40 ms |
67932 KB |
Output is correct |
31 |
Correct |
41 ms |
68188 KB |
Output is correct |
32 |
Correct |
42 ms |
67932 KB |
Output is correct |
33 |
Correct |
44 ms |
70672 KB |
Output is correct |
34 |
Correct |
45 ms |
70748 KB |
Output is correct |
35 |
Correct |
57 ms |
71156 KB |
Output is correct |
36 |
Correct |
45 ms |
68432 KB |
Output is correct |
37 |
Correct |
58 ms |
74576 KB |
Output is correct |
38 |
Correct |
61 ms |
73812 KB |
Output is correct |
39 |
Correct |
63 ms |
73812 KB |
Output is correct |
40 |
Correct |
63 ms |
73824 KB |
Output is correct |
41 |
Correct |
62 ms |
73832 KB |
Output is correct |
42 |
Correct |
44 ms |
67932 KB |
Output is correct |
43 |
Correct |
45 ms |
68184 KB |
Output is correct |
44 |
Correct |
52 ms |
68176 KB |
Output is correct |
45 |
Correct |
80 ms |
83396 KB |
Output is correct |
46 |
Correct |
89 ms |
83284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
67952 KB |
Output is correct |
2 |
Correct |
41 ms |
67920 KB |
Output is correct |
3 |
Correct |
44 ms |
67924 KB |
Output is correct |
4 |
Correct |
41 ms |
67920 KB |
Output is correct |
5 |
Correct |
41 ms |
67920 KB |
Output is correct |
6 |
Correct |
40 ms |
67924 KB |
Output is correct |
7 |
Correct |
42 ms |
67928 KB |
Output is correct |
8 |
Correct |
40 ms |
67932 KB |
Output is correct |
9 |
Correct |
40 ms |
67932 KB |
Output is correct |
10 |
Correct |
40 ms |
67932 KB |
Output is correct |
11 |
Correct |
43 ms |
68180 KB |
Output is correct |
12 |
Correct |
42 ms |
67932 KB |
Output is correct |
13 |
Correct |
40 ms |
67800 KB |
Output is correct |
14 |
Correct |
41 ms |
68176 KB |
Output is correct |
15 |
Correct |
44 ms |
68476 KB |
Output is correct |
16 |
Correct |
40 ms |
67920 KB |
Output is correct |
17 |
Correct |
42 ms |
68176 KB |
Output is correct |
18 |
Correct |
41 ms |
67920 KB |
Output is correct |
19 |
Correct |
40 ms |
67932 KB |
Output is correct |
20 |
Correct |
41 ms |
67928 KB |
Output is correct |
21 |
Correct |
41 ms |
67932 KB |
Output is correct |
22 |
Correct |
41 ms |
67932 KB |
Output is correct |
23 |
Correct |
41 ms |
68120 KB |
Output is correct |
24 |
Correct |
41 ms |
68180 KB |
Output is correct |
25 |
Correct |
41 ms |
68188 KB |
Output is correct |
26 |
Correct |
41 ms |
67968 KB |
Output is correct |
27 |
Correct |
41 ms |
67924 KB |
Output is correct |
28 |
Correct |
47 ms |
68436 KB |
Output is correct |
29 |
Correct |
44 ms |
68304 KB |
Output is correct |
30 |
Correct |
41 ms |
67928 KB |
Output is correct |
31 |
Correct |
41 ms |
68188 KB |
Output is correct |
32 |
Correct |
44 ms |
67932 KB |
Output is correct |
33 |
Correct |
44 ms |
70568 KB |
Output is correct |
34 |
Correct |
47 ms |
70768 KB |
Output is correct |
35 |
Correct |
56 ms |
70996 KB |
Output is correct |
36 |
Correct |
43 ms |
68444 KB |
Output is correct |
37 |
Correct |
57 ms |
74732 KB |
Output is correct |
38 |
Correct |
63 ms |
74096 KB |
Output is correct |
39 |
Correct |
63 ms |
73816 KB |
Output is correct |
40 |
Correct |
64 ms |
73828 KB |
Output is correct |
41 |
Correct |
61 ms |
73868 KB |
Output is correct |
42 |
Correct |
44 ms |
67932 KB |
Output is correct |
43 |
Correct |
44 ms |
67924 KB |
Output is correct |
44 |
Correct |
44 ms |
67928 KB |
Output is correct |
45 |
Correct |
75 ms |
83476 KB |
Output is correct |
46 |
Correct |
88 ms |
83624 KB |
Output is correct |
47 |
Correct |
106 ms |
89892 KB |
Output is correct |
48 |
Correct |
55 ms |
75404 KB |
Output is correct |
49 |
Correct |
53 ms |
72812 KB |
Output is correct |
50 |
Correct |
50 ms |
72280 KB |
Output is correct |
51 |
Correct |
72 ms |
78616 KB |
Output is correct |
52 |
Correct |
77 ms |
81088 KB |
Output is correct |
53 |
Correct |
58 ms |
71508 KB |
Output is correct |
54 |
Correct |
41 ms |
67932 KB |
Output is correct |
55 |
Correct |
42 ms |
68132 KB |
Output is correct |
56 |
Correct |
45 ms |
68292 KB |
Output is correct |
57 |
Correct |
60 ms |
77908 KB |
Output is correct |
58 |
Correct |
45 ms |
68188 KB |
Output is correct |
59 |
Correct |
46 ms |
68444 KB |
Output is correct |
60 |
Correct |
54 ms |
70740 KB |
Output is correct |
61 |
Correct |
51 ms |
70992 KB |
Output is correct |
62 |
Correct |
70 ms |
83540 KB |
Output is correct |
63 |
Execution timed out |
2503 ms |
262144 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |