#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<short,short>
#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)*2.33;
const int mxv = mxn*sqrt(mxn)*2.1;
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++;
assert(ecnt<mxe);
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);
int tmp = inf;
while(!dq.empty()){
int now = dq.front();
dq.pop_front();
if(done[now])continue;
if(now == T)tmp = dist[now];
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);
}
}
}
}
assert(dist[T] == tmp);
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++;
assert(vcnt<mxv);
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 |
77 ms |
137812 KB |
Output is correct |
2 |
Correct |
79 ms |
137920 KB |
Output is correct |
3 |
Correct |
80 ms |
137880 KB |
Output is correct |
4 |
Correct |
76 ms |
137812 KB |
Output is correct |
5 |
Correct |
76 ms |
137920 KB |
Output is correct |
6 |
Correct |
77 ms |
137812 KB |
Output is correct |
7 |
Correct |
77 ms |
137924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
137768 KB |
Output is correct |
2 |
Correct |
77 ms |
137824 KB |
Output is correct |
3 |
Correct |
81 ms |
137888 KB |
Output is correct |
4 |
Correct |
78 ms |
137812 KB |
Output is correct |
5 |
Correct |
79 ms |
138068 KB |
Output is correct |
6 |
Correct |
77 ms |
137812 KB |
Output is correct |
7 |
Correct |
77 ms |
137920 KB |
Output is correct |
8 |
Correct |
78 ms |
137920 KB |
Output is correct |
9 |
Correct |
79 ms |
137812 KB |
Output is correct |
10 |
Correct |
78 ms |
137808 KB |
Output is correct |
11 |
Correct |
78 ms |
138068 KB |
Output is correct |
12 |
Correct |
77 ms |
137804 KB |
Output is correct |
13 |
Correct |
78 ms |
137836 KB |
Output is correct |
14 |
Correct |
82 ms |
138060 KB |
Output is correct |
15 |
Correct |
78 ms |
138092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
137808 KB |
Output is correct |
2 |
Correct |
81 ms |
137808 KB |
Output is correct |
3 |
Correct |
77 ms |
137812 KB |
Output is correct |
4 |
Correct |
77 ms |
137820 KB |
Output is correct |
5 |
Correct |
78 ms |
137812 KB |
Output is correct |
6 |
Correct |
80 ms |
137812 KB |
Output is correct |
7 |
Correct |
77 ms |
137836 KB |
Output is correct |
8 |
Correct |
77 ms |
138060 KB |
Output is correct |
9 |
Correct |
76 ms |
137808 KB |
Output is correct |
10 |
Correct |
79 ms |
137808 KB |
Output is correct |
11 |
Correct |
78 ms |
138068 KB |
Output is correct |
12 |
Correct |
77 ms |
137808 KB |
Output is correct |
13 |
Correct |
83 ms |
138324 KB |
Output is correct |
14 |
Correct |
78 ms |
138068 KB |
Output is correct |
15 |
Correct |
77 ms |
138064 KB |
Output is correct |
16 |
Correct |
77 ms |
137960 KB |
Output is correct |
17 |
Correct |
79 ms |
140112 KB |
Output is correct |
18 |
Correct |
77 ms |
140112 KB |
Output is correct |
19 |
Correct |
79 ms |
137812 KB |
Output is correct |
20 |
Correct |
78 ms |
137928 KB |
Output is correct |
21 |
Correct |
78 ms |
137852 KB |
Output is correct |
22 |
Correct |
78 ms |
137940 KB |
Output is correct |
23 |
Correct |
90 ms |
140000 KB |
Output is correct |
24 |
Correct |
79 ms |
140116 KB |
Output is correct |
25 |
Correct |
92 ms |
140112 KB |
Output is correct |
26 |
Correct |
78 ms |
140036 KB |
Output is correct |
27 |
Correct |
80 ms |
139852 KB |
Output is correct |
28 |
Execution timed out |
2398 ms |
262144 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
137812 KB |
Output is correct |
2 |
Correct |
77 ms |
137808 KB |
Output is correct |
3 |
Correct |
76 ms |
137920 KB |
Output is correct |
4 |
Correct |
76 ms |
137768 KB |
Output is correct |
5 |
Correct |
77 ms |
137808 KB |
Output is correct |
6 |
Correct |
77 ms |
137808 KB |
Output is correct |
7 |
Correct |
76 ms |
137924 KB |
Output is correct |
8 |
Correct |
78 ms |
137808 KB |
Output is correct |
9 |
Correct |
79 ms |
137844 KB |
Output is correct |
10 |
Correct |
78 ms |
137820 KB |
Output is correct |
11 |
Correct |
78 ms |
138064 KB |
Output is correct |
12 |
Correct |
77 ms |
137812 KB |
Output is correct |
13 |
Correct |
78 ms |
137824 KB |
Output is correct |
14 |
Correct |
78 ms |
137920 KB |
Output is correct |
15 |
Correct |
96 ms |
138052 KB |
Output is correct |
16 |
Correct |
84 ms |
138020 KB |
Output is correct |
17 |
Correct |
85 ms |
140112 KB |
Output is correct |
18 |
Correct |
78 ms |
140112 KB |
Output is correct |
19 |
Correct |
78 ms |
137928 KB |
Output is correct |
20 |
Correct |
78 ms |
137812 KB |
Output is correct |
21 |
Correct |
77 ms |
137900 KB |
Output is correct |
22 |
Correct |
83 ms |
137980 KB |
Output is correct |
23 |
Correct |
80 ms |
139988 KB |
Output is correct |
24 |
Correct |
86 ms |
140116 KB |
Output is correct |
25 |
Correct |
86 ms |
140220 KB |
Output is correct |
26 |
Correct |
80 ms |
140040 KB |
Output is correct |
27 |
Correct |
84 ms |
139960 KB |
Output is correct |
28 |
Execution timed out |
2486 ms |
262144 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
137808 KB |
Output is correct |
2 |
Correct |
95 ms |
137916 KB |
Output is correct |
3 |
Correct |
82 ms |
137920 KB |
Output is correct |
4 |
Correct |
79 ms |
137856 KB |
Output is correct |
5 |
Correct |
79 ms |
137920 KB |
Output is correct |
6 |
Correct |
91 ms |
137804 KB |
Output is correct |
7 |
Correct |
86 ms |
137916 KB |
Output is correct |
8 |
Correct |
82 ms |
137928 KB |
Output is correct |
9 |
Correct |
85 ms |
137832 KB |
Output is correct |
10 |
Correct |
90 ms |
137872 KB |
Output is correct |
11 |
Correct |
81 ms |
138068 KB |
Output is correct |
12 |
Correct |
84 ms |
137936 KB |
Output is correct |
13 |
Correct |
88 ms |
137936 KB |
Output is correct |
14 |
Correct |
85 ms |
137924 KB |
Output is correct |
15 |
Correct |
78 ms |
138068 KB |
Output is correct |
16 |
Correct |
82 ms |
137856 KB |
Output is correct |
17 |
Correct |
96 ms |
140212 KB |
Output is correct |
18 |
Correct |
88 ms |
140136 KB |
Output is correct |
19 |
Correct |
84 ms |
137956 KB |
Output is correct |
20 |
Correct |
81 ms |
137892 KB |
Output is correct |
21 |
Correct |
97 ms |
137904 KB |
Output is correct |
22 |
Correct |
90 ms |
137916 KB |
Output is correct |
23 |
Correct |
82 ms |
140108 KB |
Output is correct |
24 |
Correct |
86 ms |
140040 KB |
Output is correct |
25 |
Correct |
80 ms |
140076 KB |
Output is correct |
26 |
Correct |
82 ms |
139856 KB |
Output is correct |
27 |
Correct |
79 ms |
139860 KB |
Output is correct |
28 |
Execution timed out |
2364 ms |
262144 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |