#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
typedef pair<int,pair<int,int>> pp;
int N,M;
int B[30005],P[30005];
vector<int> V[30005];
deque<pp> Q;
int main(){
cin>>N>>M;
for(int i=0;i<M;i++){
cin>>B[i]>>P[i];
if(i)
V[B[i]].push_back(i);
}
//indice distanza posizione
Q.push_back({0,{0,B[0]}});
while(!Q.empty()){
pp p=Q.front();
Q.pop_front();
//cout<<Q.size()<<" "<<p.first<<" "<<p.second.first<<" "<<p.second.second<<endl;
if(p.first==1){
cout<<p.second.first;
return 0;
}
for(int x:V[p.second.second])
Q.push_front({x,{p.second.first,B[x]}});
V[p.second.second].clear();
if(p.second.second==B[p.first]){
if(p.second.second-P[p.first]>=0)
Q.push_back({p.first,{p.second.first+1,p.second.second-P[p.first]}});
if(p.second.second+P[p.first]<N)
Q.push_back({p.first,{p.second.first+1,p.second.second+P[p.first]}});
}
if(p.second.second<B[p.first]){
if(p.second.second-P[p.first]>=0)
Q.push_back({p.first,{p.second.first+1,p.second.second-P[p.first]}});
}
if(p.second.second>B[p.first]){
if(p.second.second+P[p.first]<N)
Q.push_back({p.first,{p.second.first+1,p.second.second+P[p.first]}});
}
}
cout<<-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
1172 KB |
Output is correct |
3 |
Correct |
4 ms |
1172 KB |
Output is correct |
4 |
Correct |
3 ms |
1172 KB |
Output is correct |
5 |
Correct |
3 ms |
1172 KB |
Output is correct |
6 |
Correct |
3 ms |
1288 KB |
Output is correct |
7 |
Correct |
3 ms |
1288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1288 KB |
Output is correct |
2 |
Correct |
4 ms |
1288 KB |
Output is correct |
3 |
Correct |
4 ms |
1288 KB |
Output is correct |
4 |
Correct |
3 ms |
1288 KB |
Output is correct |
5 |
Correct |
3 ms |
1312 KB |
Output is correct |
6 |
Correct |
3 ms |
1312 KB |
Output is correct |
7 |
Correct |
4 ms |
1312 KB |
Output is correct |
8 |
Correct |
3 ms |
1312 KB |
Output is correct |
9 |
Correct |
3 ms |
1312 KB |
Output is correct |
10 |
Correct |
5 ms |
1312 KB |
Output is correct |
11 |
Correct |
4 ms |
1312 KB |
Output is correct |
12 |
Correct |
4 ms |
1432 KB |
Output is correct |
13 |
Correct |
5 ms |
1432 KB |
Output is correct |
14 |
Correct |
5 ms |
1432 KB |
Output is correct |
15 |
Correct |
4 ms |
1432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1432 KB |
Output is correct |
2 |
Correct |
2 ms |
1432 KB |
Output is correct |
3 |
Correct |
4 ms |
1432 KB |
Output is correct |
4 |
Correct |
3 ms |
1432 KB |
Output is correct |
5 |
Correct |
3 ms |
1432 KB |
Output is correct |
6 |
Correct |
4 ms |
1432 KB |
Output is correct |
7 |
Correct |
3 ms |
1432 KB |
Output is correct |
8 |
Correct |
2 ms |
1432 KB |
Output is correct |
9 |
Correct |
3 ms |
1432 KB |
Output is correct |
10 |
Correct |
4 ms |
1432 KB |
Output is correct |
11 |
Correct |
4 ms |
1432 KB |
Output is correct |
12 |
Correct |
4 ms |
1432 KB |
Output is correct |
13 |
Correct |
4 ms |
1432 KB |
Output is correct |
14 |
Correct |
6 ms |
1432 KB |
Output is correct |
15 |
Correct |
5 ms |
1432 KB |
Output is correct |
16 |
Correct |
4 ms |
1432 KB |
Output is correct |
17 |
Correct |
5 ms |
1432 KB |
Output is correct |
18 |
Correct |
6 ms |
1432 KB |
Output is correct |
19 |
Correct |
4 ms |
1432 KB |
Output is correct |
20 |
Correct |
47 ms |
1448 KB |
Output is correct |
21 |
Correct |
5 ms |
1448 KB |
Output is correct |
22 |
Correct |
4 ms |
1448 KB |
Output is correct |
23 |
Correct |
3 ms |
1448 KB |
Output is correct |
24 |
Correct |
6 ms |
1448 KB |
Output is correct |
25 |
Correct |
5 ms |
1448 KB |
Output is correct |
26 |
Correct |
6 ms |
1448 KB |
Output is correct |
27 |
Correct |
4 ms |
1448 KB |
Output is correct |
28 |
Correct |
5 ms |
1448 KB |
Output is correct |
29 |
Correct |
4 ms |
1448 KB |
Output is correct |
30 |
Correct |
4 ms |
1448 KB |
Output is correct |
31 |
Correct |
4 ms |
1448 KB |
Output is correct |
32 |
Correct |
4 ms |
1448 KB |
Output is correct |
33 |
Correct |
7 ms |
1448 KB |
Output is correct |
34 |
Correct |
5 ms |
1448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1448 KB |
Output is correct |
2 |
Correct |
4 ms |
1448 KB |
Output is correct |
3 |
Correct |
3 ms |
1448 KB |
Output is correct |
4 |
Correct |
3 ms |
1448 KB |
Output is correct |
5 |
Correct |
3 ms |
1448 KB |
Output is correct |
6 |
Correct |
4 ms |
1448 KB |
Output is correct |
7 |
Correct |
3 ms |
1448 KB |
Output is correct |
8 |
Correct |
3 ms |
1448 KB |
Output is correct |
9 |
Correct |
4 ms |
1448 KB |
Output is correct |
10 |
Correct |
3 ms |
1448 KB |
Output is correct |
11 |
Correct |
4 ms |
1448 KB |
Output is correct |
12 |
Correct |
5 ms |
1448 KB |
Output is correct |
13 |
Correct |
4 ms |
1448 KB |
Output is correct |
14 |
Correct |
6 ms |
1448 KB |
Output is correct |
15 |
Correct |
4 ms |
1448 KB |
Output is correct |
16 |
Correct |
3 ms |
1448 KB |
Output is correct |
17 |
Correct |
4 ms |
1448 KB |
Output is correct |
18 |
Correct |
4 ms |
1448 KB |
Output is correct |
19 |
Correct |
3 ms |
1448 KB |
Output is correct |
20 |
Correct |
62 ms |
1448 KB |
Output is correct |
21 |
Correct |
5 ms |
1448 KB |
Output is correct |
22 |
Correct |
3 ms |
1448 KB |
Output is correct |
23 |
Correct |
4 ms |
1448 KB |
Output is correct |
24 |
Correct |
4 ms |
1448 KB |
Output is correct |
25 |
Correct |
4 ms |
1448 KB |
Output is correct |
26 |
Correct |
4 ms |
1448 KB |
Output is correct |
27 |
Correct |
4 ms |
1448 KB |
Output is correct |
28 |
Correct |
5 ms |
1448 KB |
Output is correct |
29 |
Correct |
5 ms |
1448 KB |
Output is correct |
30 |
Correct |
3 ms |
1448 KB |
Output is correct |
31 |
Correct |
3 ms |
1448 KB |
Output is correct |
32 |
Correct |
4 ms |
1448 KB |
Output is correct |
33 |
Correct |
6 ms |
1448 KB |
Output is correct |
34 |
Correct |
6 ms |
1448 KB |
Output is correct |
35 |
Correct |
27 ms |
1916 KB |
Output is correct |
36 |
Correct |
6 ms |
1916 KB |
Output is correct |
37 |
Correct |
16 ms |
1916 KB |
Output is correct |
38 |
Correct |
37 ms |
1920 KB |
Output is correct |
39 |
Correct |
22 ms |
1920 KB |
Output is correct |
40 |
Correct |
23 ms |
1920 KB |
Output is correct |
41 |
Correct |
21 ms |
1920 KB |
Output is correct |
42 |
Correct |
19 ms |
1920 KB |
Output is correct |
43 |
Correct |
20 ms |
2300 KB |
Output is correct |
44 |
Correct |
62 ms |
2456 KB |
Output is correct |
45 |
Correct |
26 ms |
2456 KB |
Output is correct |
46 |
Correct |
28 ms |
2456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2456 KB |
Output is correct |
2 |
Correct |
3 ms |
2456 KB |
Output is correct |
3 |
Correct |
3 ms |
2456 KB |
Output is correct |
4 |
Correct |
3 ms |
2456 KB |
Output is correct |
5 |
Correct |
4 ms |
2456 KB |
Output is correct |
6 |
Correct |
3 ms |
2456 KB |
Output is correct |
7 |
Correct |
3 ms |
2456 KB |
Output is correct |
8 |
Correct |
3 ms |
2456 KB |
Output is correct |
9 |
Correct |
3 ms |
2456 KB |
Output is correct |
10 |
Correct |
3 ms |
2456 KB |
Output is correct |
11 |
Correct |
4 ms |
2456 KB |
Output is correct |
12 |
Correct |
4 ms |
2456 KB |
Output is correct |
13 |
Correct |
4 ms |
2456 KB |
Output is correct |
14 |
Correct |
4 ms |
2456 KB |
Output is correct |
15 |
Correct |
4 ms |
2456 KB |
Output is correct |
16 |
Correct |
3 ms |
2456 KB |
Output is correct |
17 |
Correct |
4 ms |
2456 KB |
Output is correct |
18 |
Correct |
4 ms |
2456 KB |
Output is correct |
19 |
Correct |
3 ms |
2456 KB |
Output is correct |
20 |
Correct |
48 ms |
2456 KB |
Output is correct |
21 |
Correct |
4 ms |
2456 KB |
Output is correct |
22 |
Correct |
4 ms |
2456 KB |
Output is correct |
23 |
Correct |
4 ms |
2456 KB |
Output is correct |
24 |
Correct |
4 ms |
2456 KB |
Output is correct |
25 |
Correct |
6 ms |
2456 KB |
Output is correct |
26 |
Correct |
4 ms |
2456 KB |
Output is correct |
27 |
Correct |
4 ms |
2456 KB |
Output is correct |
28 |
Correct |
4 ms |
2456 KB |
Output is correct |
29 |
Correct |
4 ms |
2456 KB |
Output is correct |
30 |
Correct |
3 ms |
2456 KB |
Output is correct |
31 |
Correct |
4 ms |
2456 KB |
Output is correct |
32 |
Correct |
4 ms |
2456 KB |
Output is correct |
33 |
Correct |
5 ms |
2456 KB |
Output is correct |
34 |
Correct |
5 ms |
2456 KB |
Output is correct |
35 |
Correct |
19 ms |
2456 KB |
Output is correct |
36 |
Correct |
6 ms |
2456 KB |
Output is correct |
37 |
Correct |
19 ms |
2456 KB |
Output is correct |
38 |
Correct |
22 ms |
2456 KB |
Output is correct |
39 |
Correct |
41 ms |
2456 KB |
Output is correct |
40 |
Correct |
26 ms |
2456 KB |
Output is correct |
41 |
Correct |
26 ms |
2456 KB |
Output is correct |
42 |
Correct |
24 ms |
2456 KB |
Output is correct |
43 |
Correct |
35 ms |
2456 KB |
Output is correct |
44 |
Correct |
74 ms |
2516 KB |
Output is correct |
45 |
Correct |
30 ms |
2516 KB |
Output is correct |
46 |
Correct |
26 ms |
2516 KB |
Output is correct |
47 |
Correct |
36 ms |
2516 KB |
Output is correct |
48 |
Correct |
26 ms |
2516 KB |
Output is correct |
49 |
Correct |
32 ms |
2516 KB |
Output is correct |
50 |
Correct |
19 ms |
2516 KB |
Output is correct |
51 |
Correct |
33 ms |
2516 KB |
Output is correct |
52 |
Correct |
39 ms |
2516 KB |
Output is correct |
53 |
Correct |
52 ms |
2516 KB |
Output is correct |
54 |
Correct |
4 ms |
2516 KB |
Output is correct |
55 |
Correct |
4 ms |
2516 KB |
Output is correct |
56 |
Execution timed out |
1079 ms |
2840 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |