This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
///* __ __ __ */
///* ====== _ /| /| __ _ / | | /| | * | | | | / /| |\ | / | | * | / */
///* \- || |_| |_ / |/ | | | |_ |- | |--| /-| | | \ \ |==| |- /=| | \ | | |--| | |- */
///* || | | |_ / | |__| _| |_ \__ | | / | |__ | __| | | | \ / | | \| \__ | | | | \ */
///*
// autor :: Rick Prime
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define Mp make_pair
#define double double long
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef vector<pii> vii;
const int N = 1e6, inf = 2e18, mod = 1e9+7;
void solve(){
int n, m;
cin >> n >> m;
vii v(m);
for(auto &[i, j]:v)cin >> i >> j;
int dp[n];
for(int &i:dp)i = inf;
dp[v[0].ff] = 0;
vi r[n];
for(auto [i, j]:v)r[i].pb(j);
priority_queue<pair<int,pii>> q;
q.push({0, v[0]});
int dp2[n][3001];
for(auto &i:dp2)for(auto &j:i)j = inf;
bool us[n]{};
while(!q.empty()){
auto [dist, uka] = q.top();
auto [b, p] = uka;
q.pop();
dp[b] = min(dp[b], dist);
dp2[b][p] = min(dp2[b][p], dist);
if(!us[b]){
us[b] = 1;
for(auto i:r[b]){
if(b+i<n)q.push({dist+1, {b+i, i}});
if(b-i>=0)q.push({dist+1, {b-i, i}});
}
}
if(b+p < n && dp2[b+p][p] > dist + 1){
dp2[b+p][p] = dist + 1;
q.push({dist + 1, {b+p, p}});
}
if(b-p >=0 && dp2[b-p][p] > dist + 1){
dp2[b-p][p] = dist + 1;
q.push({dist + 1, {b-p, p}});
}
}
cout << (dp[v[1].ff]==inf?-1:dp[v[1].ff]) << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int test = 1;
//cin >> test;
while(test--){
solve();
}
}
# | 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... |