#include<bits/stdc++.h>
using namespace std;
vector<int>adj[30000],g[30000],b(30000),p(30000);
int main(){
int n,m;cin>>n>>m;
for(int i=0;i<m;i++){
cin>>b[i]>>p[i];
g[b[i]].push_back(p[i]);
if(b[i]==b[0]){
int u=b[i];
for(int j=b[i]-p[i];j>=0;j-=p[i]){
adj[u].push_back(j);
u=j;
}
u=b[i];
for(int j=b[i]+p[i];j<n;j+=p[i]){
adj[u].push_back(j);
u=j;
}
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;
q.push({0,b[0]});
vector<int>dist(n+5,1e9);
dist[b[0]]=0;
vector<bool>vis(n+5);
vis[b[0]]=1;
map<pair<int,int>,bool>v;
while(q.size()){
pair<int,int>x=q.top();
q.pop();
dist[x.second]=min(dist[x.second],x.first);
if(!vis[x.second]){
vis[x.second]=1;
for(auto i:g[x.second]){
int s=x.second;
int u=x.second+i;
while(u<n)adj[s].push_back(u),s=u,u+=i;
s=x.second;
u=x.second-i;
while(u>=0)adj[s].push_back(u),s=u,u-=i;
}
}
for(auto i:adj[x.second]){
if(!v[{x.second,i}]){
v[{x.second,i}]=1;
q.push({x.first+1,i});
}
}
}
cout<<(dist[b[1]]==1e9?-1:dist[b[1]])<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1888 KB |
Output is correct |
2 |
Correct |
1 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Incorrect |
2 ms |
1876 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1876 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Incorrect |
2 ms |
1876 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1880 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1896 KB |
Output is correct |
9 |
Incorrect |
2 ms |
1876 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |