This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of God
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll maxn = 3e4 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
ll pw(ll a, ll b){
ll c = 1;
while(b){
if(b & 1) c = c * a % mod;
a = a * a % mod;
b >>= 1;
}
return c;
}
map<int, bool> vis[maxn];
int n, m, start, goal, dis;
bool vs[maxn];
bool siv[256][maxn];
vector<int> vec[maxn];
vector<int> q, qu;
void add(int v){
if(0 > v || v >= m || vs[v]) return;
vs[v] = 1;
if(v == goal){
cout << dis;
exit(0);
}
for(int x : vec[v]){
if(x < 250) siv[x][v] = 1;
else vis[x][v] = 1;
q.pb((x << 15) | v);
}
return;
}
int main(){
fast_io;
q.reserve(1000000);
qu.reserve(1000000);
cin >> m >> n;
for(int i = 0; i < n; i++){
int v, x;
cin >> v >> x;
if(i == 0) start = v;
if(i == 1) goal = v;
vec[v].pb(x);
}
add(start);
while(!q.empty()){
dis++;
qu = q;
q.clear();
for(int i : qu){
int x = (i >> 15);
int v = (i ^ (x << 15));
add(v - x);
add(v + x);
if(v + x < m && !(x < 250 ? siv[x][v + x] : vis[x][v + x])){
if(x < 250) siv[x][v + x] = 1;
else vis[x][v + x] = 1;
q.pb((x << 15) | (v + x));
}
if(0 < v - x && !(x < 250 ? siv[x][v - x] : vis[x][v - x])){
if(x < 250) siv[x][v - x] = 1;
else vis[x][v - x] = 1;
q.pb((x << 15) | (v - x));
}
}
}
cout << -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... |