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 mk make_pair
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e18 + 50;
const int N = 30050, SQ = 210;
ll n, m, b[N], p[N], dis[N];
bool mark[SQ][N];
vector<pll> adj[N];
set<pll> st;
void solve() {
cin >> n >> m;
for (int x = 0; x < n; x++) {
dis[x] = inf;
}
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
if (p[i] < SQ) mark[p[i]][b[i]] = 1;
else for (int x = b[i]%p[i]; x < n; x += p[i]) {
adj[b[i]].push_back({x, abs(b[i]-x)/p[i]});
}
}
for (int d = 1; d < SQ; d++) {
for (int x = 0; x < n; x++) {
if (!mark[d][x]) continue;
for (int y = x + d; y < n; y += d) {
adj[x].push_back({y, abs(x-y)/d});
if (mark[d][y]) break;
}
for (int y = x - d; y >= 0; y -= d) {
adj[x].push_back({y, abs(x-y)/d});
if (mark[d][y]) break;
}
}
}
dis[b[0]] = 0;
st.insert({dis[b[0]], b[0]});
while (st.size()) {
ll u = st.begin()->S;
st.erase(st.begin());;
for (pll x: adj[u]) {
ll v = x.F, w = x.S;
if (dis[u] + w < dis[v]) {
st.erase({dis[v], v});
dis[v] = dis[u] + w;
st.insert({dis[v], v});
}
}
}
cout << (dis[b[1]] >= inf? -1: dis[b[1]]) << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# | 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... |