#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];
bitset<mxn> wei;
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()){
if(dist[T] != inf)break;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
65504 KB |
Output is correct |
2 |
Correct |
11 ms |
65628 KB |
Output is correct |
3 |
Correct |
9 ms |
65372 KB |
Output is correct |
4 |
Correct |
9 ms |
65372 KB |
Output is correct |
5 |
Correct |
11 ms |
65432 KB |
Output is correct |
6 |
Correct |
9 ms |
65372 KB |
Output is correct |
7 |
Correct |
9 ms |
65372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
65368 KB |
Output is correct |
2 |
Correct |
9 ms |
65372 KB |
Output is correct |
3 |
Correct |
11 ms |
65524 KB |
Output is correct |
4 |
Correct |
9 ms |
65368 KB |
Output is correct |
5 |
Correct |
9 ms |
65372 KB |
Output is correct |
6 |
Correct |
9 ms |
65372 KB |
Output is correct |
7 |
Correct |
9 ms |
65476 KB |
Output is correct |
8 |
Correct |
9 ms |
65368 KB |
Output is correct |
9 |
Correct |
9 ms |
65372 KB |
Output is correct |
10 |
Correct |
9 ms |
65544 KB |
Output is correct |
11 |
Correct |
10 ms |
65664 KB |
Output is correct |
12 |
Correct |
9 ms |
65628 KB |
Output is correct |
13 |
Correct |
9 ms |
65372 KB |
Output is correct |
14 |
Correct |
10 ms |
65628 KB |
Output is correct |
15 |
Correct |
10 ms |
65628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
65372 KB |
Output is correct |
2 |
Correct |
9 ms |
65448 KB |
Output is correct |
3 |
Correct |
9 ms |
65372 KB |
Output is correct |
4 |
Correct |
9 ms |
65372 KB |
Output is correct |
5 |
Correct |
9 ms |
65436 KB |
Output is correct |
6 |
Correct |
9 ms |
65368 KB |
Output is correct |
7 |
Correct |
10 ms |
65368 KB |
Output is correct |
8 |
Correct |
9 ms |
65372 KB |
Output is correct |
9 |
Correct |
9 ms |
65372 KB |
Output is correct |
10 |
Correct |
9 ms |
65628 KB |
Output is correct |
11 |
Correct |
10 ms |
65684 KB |
Output is correct |
12 |
Correct |
9 ms |
65628 KB |
Output is correct |
13 |
Correct |
9 ms |
65372 KB |
Output is correct |
14 |
Correct |
10 ms |
65628 KB |
Output is correct |
15 |
Correct |
10 ms |
65628 KB |
Output is correct |
16 |
Correct |
9 ms |
65628 KB |
Output is correct |
17 |
Correct |
10 ms |
65624 KB |
Output is correct |
18 |
Correct |
10 ms |
65652 KB |
Output is correct |
19 |
Correct |
10 ms |
65628 KB |
Output is correct |
20 |
Correct |
9 ms |
65544 KB |
Output is correct |
21 |
Correct |
10 ms |
65628 KB |
Output is correct |
22 |
Correct |
9 ms |
65528 KB |
Output is correct |
23 |
Correct |
10 ms |
65628 KB |
Output is correct |
24 |
Correct |
10 ms |
65884 KB |
Output is correct |
25 |
Correct |
10 ms |
65628 KB |
Output is correct |
26 |
Correct |
10 ms |
65540 KB |
Output is correct |
27 |
Correct |
9 ms |
65524 KB |
Output is correct |
28 |
Correct |
10 ms |
65884 KB |
Output is correct |
29 |
Runtime error |
66 ms |
133232 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
65372 KB |
Output is correct |
2 |
Correct |
9 ms |
65372 KB |
Output is correct |
3 |
Correct |
9 ms |
65372 KB |
Output is correct |
4 |
Correct |
9 ms |
65492 KB |
Output is correct |
5 |
Correct |
11 ms |
65372 KB |
Output is correct |
6 |
Correct |
9 ms |
65372 KB |
Output is correct |
7 |
Correct |
9 ms |
65372 KB |
Output is correct |
8 |
Correct |
9 ms |
65372 KB |
Output is correct |
9 |
Correct |
9 ms |
65372 KB |
Output is correct |
10 |
Correct |
9 ms |
65628 KB |
Output is correct |
11 |
Correct |
10 ms |
65628 KB |
Output is correct |
12 |
Correct |
9 ms |
65628 KB |
Output is correct |
13 |
Correct |
9 ms |
65624 KB |
Output is correct |
14 |
Correct |
10 ms |
65684 KB |
Output is correct |
15 |
Correct |
10 ms |
65624 KB |
Output is correct |
16 |
Correct |
9 ms |
65628 KB |
Output is correct |
17 |
Correct |
10 ms |
65720 KB |
Output is correct |
18 |
Correct |
9 ms |
65644 KB |
Output is correct |
19 |
Correct |
9 ms |
65628 KB |
Output is correct |
20 |
Correct |
9 ms |
65628 KB |
Output is correct |
21 |
Correct |
9 ms |
65628 KB |
Output is correct |
22 |
Correct |
9 ms |
65628 KB |
Output is correct |
23 |
Correct |
9 ms |
65620 KB |
Output is correct |
24 |
Correct |
11 ms |
65628 KB |
Output is correct |
25 |
Correct |
10 ms |
65628 KB |
Output is correct |
26 |
Correct |
9 ms |
65628 KB |
Output is correct |
27 |
Correct |
9 ms |
65572 KB |
Output is correct |
28 |
Correct |
10 ms |
65884 KB |
Output is correct |
29 |
Runtime error |
64 ms |
133184 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
65368 KB |
Output is correct |
2 |
Correct |
9 ms |
65372 KB |
Output is correct |
3 |
Correct |
9 ms |
65372 KB |
Output is correct |
4 |
Correct |
9 ms |
65372 KB |
Output is correct |
5 |
Correct |
9 ms |
65372 KB |
Output is correct |
6 |
Correct |
9 ms |
65372 KB |
Output is correct |
7 |
Correct |
9 ms |
65516 KB |
Output is correct |
8 |
Correct |
9 ms |
65372 KB |
Output is correct |
9 |
Correct |
9 ms |
65372 KB |
Output is correct |
10 |
Correct |
9 ms |
65528 KB |
Output is correct |
11 |
Correct |
9 ms |
65660 KB |
Output is correct |
12 |
Correct |
9 ms |
65536 KB |
Output is correct |
13 |
Correct |
9 ms |
65460 KB |
Output is correct |
14 |
Correct |
10 ms |
65628 KB |
Output is correct |
15 |
Correct |
10 ms |
65624 KB |
Output is correct |
16 |
Correct |
10 ms |
65536 KB |
Output is correct |
17 |
Correct |
10 ms |
65628 KB |
Output is correct |
18 |
Correct |
9 ms |
65600 KB |
Output is correct |
19 |
Correct |
10 ms |
65500 KB |
Output is correct |
20 |
Correct |
9 ms |
65628 KB |
Output is correct |
21 |
Correct |
9 ms |
65628 KB |
Output is correct |
22 |
Correct |
9 ms |
65628 KB |
Output is correct |
23 |
Correct |
9 ms |
65628 KB |
Output is correct |
24 |
Correct |
11 ms |
65628 KB |
Output is correct |
25 |
Correct |
10 ms |
65700 KB |
Output is correct |
26 |
Correct |
9 ms |
65540 KB |
Output is correct |
27 |
Correct |
9 ms |
65628 KB |
Output is correct |
28 |
Correct |
11 ms |
65884 KB |
Output is correct |
29 |
Runtime error |
65 ms |
133248 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |