This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const int inf = 1e9, N = 30000;
int B[N], P[N];
vector<int> residents[N];
bitset<N> bl;
int dis[N];
vector<int> Q[N+1];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> B[i] >> P[i];
residents[B[i]].pb(i);
}
fill(dis, dis+m, inf);
Q[0].pb(0);
for(int d = 0; d <= N; d++) for(int nd: Q[d]){
if(nd == 1){
cout << d << "\n";
exit(0);
}
if(bl[nd]) continue;
bl[nd] = 1;
for(int i = B[nd], dd = d; i < n; i+=P[nd], dd++) for(int x: residents[i]) if(dd < dis[x]){
dis[x] = dd;
Q[dd].pb(x);
}
for(int i = B[nd]-P[nd], dd = d+1; i >= 0; i-=P[nd], dd++) for(int x: residents[i]) if(dd < dis[x]){
dis[x] = dd;
Q[dd].pb(x);
}
}
cout << "-1\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |