#include <bits/stdc++.h>
using namespace std;
struct loc{int x; int doge; int d; bool r;};
int N, M;
int b[30000], p[30000];
vector<int> graph[30000];
int dist[30000];
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for(int i = 0; i < M; i++){
cin >> b[i] >> p[i];
graph[b[i]].push_back(i);
}
fill(dist, dist+N, -1);
deque<loc> q;
q.push_back({b[0], 0, 0, true});
q.push_back({b[0], 0, 0, false});
while(!q.empty()){
if(~dist[b[1]]) break;
int x = q.front().x, doge = q.front().doge, d = q.front().d;
bool r = q.front().r;
q.pop_front();
for(int i : graph[x]){
if(i != doge){
q.push_front({x, i, d, true});
q.push_front({x, i, d, false});
}
}
int dv = p[doge];
if(!r) dv = -dv;
x += dv;
d++;
while(x >= 0 && x < N && ~dist[x]){
x += dv;
d++;
}
if(x < 0 || x >= N) continue;
dist[x] = d;
q.push_back({x, doge, d, r});
}
cout << dist[b[1]] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Runtime error |
489 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Runtime error |
493 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Runtime error |
490 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Runtime error |
488 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Runtime error |
490 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |