#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)*2;
const int mxv = mxe;
map<pii,vector<int>> mp;
int N,M;
vector<pii> people;
int ecnt,vcnt;
int to[mxe],nid[mxe],wei[mxe];
int head[mxv];
int dist[mxv];
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[0] = 0;
dq.push_back(pii(0,0));
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);
}
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);
if(!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[1]>=inf?-1:dist[1])<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
47196 KB |
Output is correct |
2 |
Correct |
9 ms |
47196 KB |
Output is correct |
3 |
Incorrect |
10 ms |
47196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
47196 KB |
Output is correct |
2 |
Correct |
9 ms |
47192 KB |
Output is correct |
3 |
Incorrect |
10 ms |
47196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
47196 KB |
Output is correct |
2 |
Correct |
13 ms |
47196 KB |
Output is correct |
3 |
Incorrect |
10 ms |
47196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
47192 KB |
Output is correct |
2 |
Correct |
9 ms |
47196 KB |
Output is correct |
3 |
Incorrect |
9 ms |
47196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
47192 KB |
Output is correct |
2 |
Correct |
9 ms |
47192 KB |
Output is correct |
3 |
Incorrect |
9 ms |
47196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |