#include <bits/stdc++.h>
#define int long long
#define debug cout << "ok\n";
#define SQR(x) (1LL * ((x) * (x)))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pli pair<ll,int>
#define vi vector<int>
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui;
using namespace std;
const int M = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);
const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
template<class _, class __>
bool minimize(_ &x, const __ y){
if(x > y){
x = y;
return true;
} else return false;
}
template<class _, class __>
bool maximize(_ &x, const __ y){
if(x < y){
x = y;
return true;
} else return false;
}
template<class _,class __>
void Add(_ &x, const __ y) {
x += y;
if (x >= M) {
x -= M;
}
return;
}
template<class _,class __>
void Diff(_ &x, const __ y) {
x -= y;
if (x < 0) {
x += M;
}
return;
}
//--------------------------------------------------------------
const int MaxN = 3e4+7;
const int S = 200;
int n,m,Dist[MaxN][S + 7],ed;
bool vis[MaxN];
vi lis[MaxN];
bool inSide(int u) {
return 0 <= u && u < n;
}
void sol() {
cin >> n >> m;
priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>>> pq;
int p,st;
cin >> p >> st;
memset(Dist,0x3f,sizeof(Dist));
if (st <= S) Dist[0][st] = 0, pq.push(mp(0,mp(p,st)));
else {
for (int i=0;i*st<n;i++) {
Dist[i*st][0] = i;
pq.push(mp(i,mp(i*st,0)));
}
}
cin >> ed >> st;
for (int i=2;i<m;i++) {
int p,st;
cin >> p >> st;
lis[p].pb(st);
}
while (!pq.empty()) {
int du = pq.top().fi;
int u = pq.top().se.fi;
int st = pq.top().se.se;
pq.pop();
if (Dist[u][st] != du) continue;
if (u == ed) {
cout << du;
return;
}
if (st > 0) {
for (int i=-1;i<=1;i+=2) {
int v = u + i * st;
if (inSide(v) && minimize(Dist[v][st],du + 1)) {
pq.push(mp(Dist[v][st],mp(v,st)));
}
}
}
if (!vis[u]) {
vis[u] = true;
for (int n_st : lis[u]) {
if (n_st <= S) {
if (minimize(Dist[u][n_st],du)) {
pq.push(mp(Dist[u][n_st],mp(u,n_st)));
}
}
else {
for (int i=0;u + i * n_st < n;i++) {
if (minimize(Dist[u + i * n_st][0],du + i)) {
pq.push(mp(Dist[u + i * n_st][0],mp(u + i * n_st,0)));
}
}
for (int i=1;u - i * n_st >= 0;i++) {
if (minimize(Dist[u - i * n_st][0],du + i)) {
pq.push(mp(Dist[u - i * n_st][0],mp(u - i * n_st,0)));
}
}
}
}
}
}
cout << -1;
}
signed main() {
// freopen("test.inp","r",stdin);
// freopen("test.out","w",stdout);
FAST
int t=1;
// cin >> t;
while (t--) sol();
}
| # | 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... |