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>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
const int inf = 1e8;
const int MAX_N = 30030;
int d[MAX_N];
int n, m;
set<int> doges[MAX_N];
int main() { FAST
cin >> n >> m;
int st = -1, en = -1;
M00(i, m) {
int b, p; cin >> b >> p;
if(i == 0) st = b;
else if(i == 1) en = b;
doges[b].insert(p);
}
priority_queue<pi, vector<pi>, greater<pi>> pq;
M00(i, n) d[i] = inf;
pq.push(mp(0, st));
while(!pq.empty()) {
pi t = pq.top(); pq.pop();
if(d[t.s] != inf) continue;
d[t.s] = t.f;
for(int j: doges[t.s]) {
int i = t.s;
for(int x = i+j; x < n; x += j) {
if(d[x] == inf) pq.push(mp(d[t.s] + (x-i)/j, x));
if(doges[x].count(j)) break;
}
for(int x = i-j; x >= 0; x -= j) {
if(d[x] == inf) pq.push(mp(d[t.s] + (i-x)/j, x));
if(doges[x].count(j)) break;
}
}
}
int res = d[en];
if(res == 1e8) {
finish(-1);
}
cout << res << "\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... |