This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define ll long long
const int nmax = 1e5 + 5, N = 5e5 * 2;
const ll oo = 1e18;
const int lg = 19, M = 4e3;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;
int n, m;
pii a[nmax];
vector<int> adj[nmax];
ll dp[nmax];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >> m;
for(int i = 0; i < m; ++i){
cin >> a[i].fi >> a[i].se;
adj[a[i].fi].push_back(a[i].se);
}
priority_queue<pii, vector<pii>, greater<>> q;
memset(dp, 0x3f, sizeof dp);
dp[a[0].fi] = 0;
q.push({0, a[0].fi});
while(q.size()){
pii tmp = q.top();q.pop();
int u = tmp.se;
if(dp[u] != tmp.fi) continue;
for(auto &pw : adj[u]){
int w = tmp.fi;
for(int v = u + pw; v < n; v += pw){
if (dp[v] > ++w){
dp[v] = w;
q.push({dp[v], v});
}
}
w = tmp.fi;
for(int v = u - pw; v >= 0; v -= pw){
if (dp[v] > ++w){
dp[v] = w;
q.push({dp[v], v});
}
}
}
}
// debug(dp, n);
if(dp[a[1].fi] < dp[n]) cout << dp[a[1].fi];
else cout << -1;
}
/*
*/
# | 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... |