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 ll long long
#define fi first
#define se second
using namespace std;
const int N = 30000, inf = 1e9 + 7; //
struct node{
int a, b, dis;
};
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
deque<node> q;
vector<int> pos[n];
int d[m], x[m];
for (int i = 0; i < m; i++){
int x[i];
cin >> x[i] >> d[i];
pos[x[i]].push_back(i);
}
bitset<900000000> vis{};
int ans = inf;
q.push_back({0, x[0], 0});
vis[x[0]] = true;
while(!q.empty()){
node u = q.front();
q.pop_front();
//cout << u.a << ' ' << u.b << ' ' << u.dis << '\n';
if (u.b + d[u.a] < n and !vis[u.a * N + u.b + d[u.a]]){
q.push_back({u.a, u.b + d[u.a], u.dis + 1});
vis[u.a * N + u.b + d[u.a]] = true;
}
if (u.b - d[u.a] >= 0 and !vis[u.a * N + u.b - d[u.a]]){
q.push_back({u.a, u.b - d[u.a], u.dis + 1});
vis[u.a * N + u.b - d[u.a]] = true;
}
while (!pos[u.b].empty()){
int v = pos[u.b].back();
pos[u.b].pop_back();
if (!vis[v * N + u.b]){
q.push_front({v, u.b, u.dis});
vis[v * N + u.b] = true;
}
}
if (u.a == 1) ans = min(ans, u.dis);
}
cout << (ans == inf ? -1 : ans);
}
# | 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... |