#include <bits/stdc++.h>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 3e4 + 10, M = 5e6, INF = 1e9;
unordered_set<ll> st;
vector<int> t[N];
bool vis[N];
int d[N];
deque<pii> q;
int sp , sb, se;
ll gg(ll a, ll b){
return a * N + b;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
for(int i=0; i<m; i++){
int p, b; cin >> p >> b;
t[p].pb(b);
if(i == 0)
sp = p, sb = b;
if(i == 1)
se = p;
}
fill(d, d + n, INF);
q.push_back({sp, sb});
st.insert(gg(sp, sb));
d[sp] = 0;
while(!q.empty()){
int p = q.front().F, b = q.front().S;
q.pop_front();
int dis = d[p];
if(p == se)return cout << dis << '\n', 0;
if(!vis[p]){
for(auto l : t[p]){
if(st.find(gg(p, l)) == st.end())
q.push_front({p, l}), st.insert(gg(p, l));
}
vis[p] = true;
}
if(p + b < n){
if(st.find(gg(p + b, b)) == st.end()){
d[p + b] = dis + 1;
q.push_back({p + b, b}); st.insert(gg(p + b, b));
}
}
if(p - b >= 0){
if(st.find(gg(p - b, b)) == st.end()){
d[p - b] = dis + 1;
q.push_back({p - b, b}); st.insert(gg(p - b, b));
}
}
}
if(d[se] != INF)
cout << d[se] << endl;
else cout << -1 << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3036 KB |
Output is correct |
2 |
Correct |
0 ms |
3036 KB |
Output is correct |
3 |
Correct |
0 ms |
3036 KB |
Output is correct |
4 |
Correct |
0 ms |
3036 KB |
Output is correct |
5 |
Correct |
0 ms |
3036 KB |
Output is correct |
6 |
Correct |
0 ms |
3036 KB |
Output is correct |
7 |
Correct |
0 ms |
3036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3036 KB |
Output is correct |
2 |
Correct |
0 ms |
3036 KB |
Output is correct |
3 |
Correct |
0 ms |
3036 KB |
Output is correct |
4 |
Correct |
0 ms |
3036 KB |
Output is correct |
5 |
Correct |
0 ms |
3036 KB |
Output is correct |
6 |
Correct |
0 ms |
3036 KB |
Output is correct |
7 |
Correct |
0 ms |
3036 KB |
Output is correct |
8 |
Correct |
0 ms |
3036 KB |
Output is correct |
9 |
Correct |
0 ms |
3036 KB |
Output is correct |
10 |
Correct |
0 ms |
3036 KB |
Output is correct |
11 |
Incorrect |
0 ms |
3036 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3036 KB |
Output is correct |
2 |
Correct |
0 ms |
3036 KB |
Output is correct |
3 |
Correct |
0 ms |
3036 KB |
Output is correct |
4 |
Correct |
0 ms |
3036 KB |
Output is correct |
5 |
Correct |
0 ms |
3036 KB |
Output is correct |
6 |
Correct |
0 ms |
3036 KB |
Output is correct |
7 |
Correct |
0 ms |
3036 KB |
Output is correct |
8 |
Correct |
0 ms |
3036 KB |
Output is correct |
9 |
Correct |
0 ms |
3036 KB |
Output is correct |
10 |
Correct |
0 ms |
3036 KB |
Output is correct |
11 |
Incorrect |
0 ms |
3036 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3036 KB |
Output is correct |
2 |
Correct |
0 ms |
3036 KB |
Output is correct |
3 |
Correct |
0 ms |
3036 KB |
Output is correct |
4 |
Correct |
0 ms |
3036 KB |
Output is correct |
5 |
Correct |
0 ms |
3036 KB |
Output is correct |
6 |
Correct |
0 ms |
3036 KB |
Output is correct |
7 |
Correct |
0 ms |
3036 KB |
Output is correct |
8 |
Correct |
0 ms |
3036 KB |
Output is correct |
9 |
Correct |
0 ms |
3036 KB |
Output is correct |
10 |
Correct |
0 ms |
3036 KB |
Output is correct |
11 |
Incorrect |
0 ms |
3036 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3036 KB |
Output is correct |
2 |
Correct |
0 ms |
3036 KB |
Output is correct |
3 |
Correct |
0 ms |
3036 KB |
Output is correct |
4 |
Correct |
0 ms |
3036 KB |
Output is correct |
5 |
Correct |
0 ms |
3036 KB |
Output is correct |
6 |
Correct |
0 ms |
3036 KB |
Output is correct |
7 |
Correct |
0 ms |
3036 KB |
Output is correct |
8 |
Correct |
0 ms |
3036 KB |
Output is correct |
9 |
Correct |
0 ms |
3036 KB |
Output is correct |
10 |
Correct |
0 ms |
3036 KB |
Output is correct |
11 |
Incorrect |
0 ms |
3036 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |