#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = a; i<int(b);++i)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define trav(a,c) for(auto a: c)
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pii;
int main(){
cin.sync_with_stdio(false);
ll n,m;
cin>>n>>m;
vi b(m);
vi p(m);
rep(i,0,m) cin>>b[i]>>p[i];
queue<tuple<ll,ll> > q; //(doge,jumps);
q.emplace(0,0);
vector<bool> seen(m);
while(q.size()){
ll doge,jumps;
tie(doge,jumps) = q.front();
q.pop();
if(seen[doge]) continue;
seen[doge] = true;
if(doge==1){
cout<<jumps<<endl;
return 0;
}
rep(i,0,m){
if((b[i]-b[doge])%p[doge]==0)
q.emplace(i,jumps+abs(b[i]-b[doge])/p[doge]);
}
}
cout<<-1<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
496 KB |
Output is correct |
3 |
Correct |
4 ms |
496 KB |
Output is correct |
4 |
Correct |
2 ms |
496 KB |
Output is correct |
5 |
Correct |
2 ms |
496 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
652 KB |
Output is correct |
2 |
Correct |
3 ms |
652 KB |
Output is correct |
3 |
Correct |
3 ms |
652 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
3 ms |
760 KB |
Output is correct |
6 |
Correct |
4 ms |
760 KB |
Output is correct |
7 |
Correct |
2 ms |
836 KB |
Output is correct |
8 |
Correct |
3 ms |
836 KB |
Output is correct |
9 |
Incorrect |
2 ms |
836 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
836 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
856 KB |
Output is correct |
4 |
Correct |
3 ms |
856 KB |
Output is correct |
5 |
Correct |
2 ms |
856 KB |
Output is correct |
6 |
Correct |
3 ms |
856 KB |
Output is correct |
7 |
Correct |
3 ms |
856 KB |
Output is correct |
8 |
Correct |
3 ms |
856 KB |
Output is correct |
9 |
Incorrect |
3 ms |
856 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
856 KB |
Output is correct |
4 |
Correct |
3 ms |
856 KB |
Output is correct |
5 |
Correct |
3 ms |
856 KB |
Output is correct |
6 |
Correct |
2 ms |
856 KB |
Output is correct |
7 |
Correct |
3 ms |
856 KB |
Output is correct |
8 |
Correct |
3 ms |
972 KB |
Output is correct |
9 |
Incorrect |
3 ms |
996 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
996 KB |
Output is correct |
2 |
Correct |
2 ms |
996 KB |
Output is correct |
3 |
Correct |
3 ms |
1008 KB |
Output is correct |
4 |
Correct |
3 ms |
1008 KB |
Output is correct |
5 |
Correct |
3 ms |
1008 KB |
Output is correct |
6 |
Correct |
4 ms |
1008 KB |
Output is correct |
7 |
Correct |
2 ms |
1008 KB |
Output is correct |
8 |
Correct |
3 ms |
1008 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1008 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |