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>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 3e4 + 7;
const int M = 3e4 + 7;
int n, m, a[M], b[M];
vector<int> v[N];
int col0 = 0, col1 = 1;
map<int, int> d[N];
void update_front(int x, int a, deque<pii>& dq, int cost) {
if (d[x].find(a) == d[x].end()) {
d[x][a] = cost;
dq.push_front({x, a});
}
else
if (minimize(d[x][a], cost)) dq.push_front({x, a});
}
void update_back(int x, int a, deque<pii>& dq, int cost) {
if (d[x].find(a) == d[x].end()) {
d[x][a] = cost;
dq.push_back({x, a});
}
else
if (minimize(d[x][a], cost)) dq.push_back({x, a});
}
void bfs(int s, int x, int t) {
deque<pii> dq;
dq.push_front({s, x});
d[s][x] = 0;
while (!dq.empty()) {
pii tmp = dq.front();
dq.pop_front();
int x = tmp.X, y = tmp.Y;
if (x == t) {
cout<<d[x][y];
exit(0);
}
for (auto a : v[x]) update_front(x, a, dq, d[x][y]);
v[x].clear();
if (x - y >= 0) update_back(x - y, y, dq, d[x][y] + 1);
if (x + y < n) update_back(x + y, y, dq, d[x][y] + 1);
}
cout<<-1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
REP(i, 0, m) {
cin>>a[i]>>b[i];
v[a[i]].push_back(b[i]);
}
bfs(a[0], b[0], a[1]);
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... |