#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]);
}
queue<pair<int,int>>q;
q.push({0,b[0]});
vector<int>dist(n+5,1e9);
dist[b[0]]=0;
vector<bool>vis(n+5);
map<pair<int,int>,bool>v;
while(q.size()){
pair<int,int>x=q.front();
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 |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
2004 KB |
Output is correct |
11 |
Correct |
16 ms |
2260 KB |
Output is correct |
12 |
Correct |
6 ms |
2772 KB |
Output is correct |
13 |
Correct |
3 ms |
2772 KB |
Output is correct |
14 |
Incorrect |
10 ms |
2268 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
16 ms |
2264 KB |
Output is correct |
12 |
Correct |
7 ms |
2720 KB |
Output is correct |
13 |
Correct |
4 ms |
2772 KB |
Output is correct |
14 |
Incorrect |
10 ms |
2264 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
16 ms |
2260 KB |
Output is correct |
12 |
Correct |
6 ms |
2804 KB |
Output is correct |
13 |
Correct |
4 ms |
2720 KB |
Output is correct |
14 |
Incorrect |
10 ms |
2260 KB |
Output isn't correct |
15 |
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 |
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 |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
16 ms |
2268 KB |
Output is correct |
12 |
Correct |
7 ms |
2804 KB |
Output is correct |
13 |
Correct |
4 ms |
2772 KB |
Output is correct |
14 |
Incorrect |
10 ms |
2260 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |