/// dwuy: _,\,,,_\,__,\,,_
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK ""
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 30005;
int n, m;
int pw[MX];
int pos[MX];
vector<int> G[MX];
bitset<MX> vist[MX];
void nhap(){
cin >> n >> m;
for(int i=1; i<=m; i++){
cin >> pos[i] >> pw[i];
G[pos[i]].push_back(pw[i]);
}
}
void solve(){
queue<tuple<int, int, int>> Q;
Q.push({pos[1], pw[1], 0});
int ans = -1;
while(Q.size()){
int u, p, d;
tie(u, p, d) = Q.front();
Q.pop();
if(u == pos[2]){
ans = d;
break;
}
if(u-p>=0 && !vist[u-p][p]){
vist[u-p][p] = 1;
Q.push({u-p, p, d+1});
}
if(u+p<n && !vist[u+p][p]){
vist[u+p][p] = 1;
Q.push({u+p, p, d+1});
}
for(int p: G[u]){
vist[u][p] = 1;
if(u-p>=0 && !vist[u-p][p]){
vist[u-p][p] = 1;
Q.push({u-p, p, d+1});
}
if(u+p<n && !vist[u+p][p]){
vist[u+p][p] = 1;
Q.push({u+p, p, d+1});
}
}
}
cout << ans;
}
int32_t main(){
fastIO;
//file(TASK);
nhap();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2904 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
4 ms |
9816 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
7772 KB |
Output is correct |
24 |
Correct |
3 ms |
9052 KB |
Output is correct |
25 |
Correct |
1 ms |
3932 KB |
Output is correct |
26 |
Correct |
4 ms |
8684 KB |
Output is correct |
27 |
Correct |
4 ms |
9052 KB |
Output is correct |
28 |
Correct |
4 ms |
9820 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
30 |
Correct |
4 ms |
9820 KB |
Output is correct |
31 |
Correct |
4 ms |
9688 KB |
Output is correct |
32 |
Correct |
4 ms |
9816 KB |
Output is correct |
33 |
Correct |
6 ms |
9820 KB |
Output is correct |
34 |
Correct |
5 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2648 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
2488 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
7772 KB |
Output is correct |
24 |
Correct |
3 ms |
9052 KB |
Output is correct |
25 |
Correct |
1 ms |
3932 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
9052 KB |
Output is correct |
28 |
Correct |
3 ms |
9820 KB |
Output is correct |
29 |
Correct |
4 ms |
9688 KB |
Output is correct |
30 |
Correct |
4 ms |
9820 KB |
Output is correct |
31 |
Correct |
4 ms |
9820 KB |
Output is correct |
32 |
Correct |
4 ms |
9688 KB |
Output is correct |
33 |
Correct |
6 ms |
9820 KB |
Output is correct |
34 |
Correct |
6 ms |
9820 KB |
Output is correct |
35 |
Correct |
8 ms |
9564 KB |
Output is correct |
36 |
Correct |
2 ms |
5468 KB |
Output is correct |
37 |
Correct |
6 ms |
10128 KB |
Output is correct |
38 |
Correct |
9 ms |
11752 KB |
Output is correct |
39 |
Correct |
5 ms |
5468 KB |
Output is correct |
40 |
Correct |
6 ms |
5752 KB |
Output is correct |
41 |
Correct |
8 ms |
9860 KB |
Output is correct |
42 |
Correct |
6 ms |
9308 KB |
Output is correct |
43 |
Correct |
6 ms |
9564 KB |
Output is correct |
44 |
Correct |
7 ms |
10288 KB |
Output is correct |
45 |
Correct |
19 ms |
10720 KB |
Output is correct |
46 |
Correct |
14 ms |
10600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2904 KB |
Output is correct |
15 |
Correct |
1 ms |
2904 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
4 ms |
9816 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
7772 KB |
Output is correct |
24 |
Correct |
3 ms |
9052 KB |
Output is correct |
25 |
Correct |
1 ms |
3932 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
9052 KB |
Output is correct |
28 |
Correct |
4 ms |
9820 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
30 |
Correct |
4 ms |
9684 KB |
Output is correct |
31 |
Correct |
4 ms |
9820 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
5 ms |
9820 KB |
Output is correct |
34 |
Correct |
5 ms |
9820 KB |
Output is correct |
35 |
Correct |
9 ms |
9452 KB |
Output is correct |
36 |
Correct |
2 ms |
5468 KB |
Output is correct |
37 |
Correct |
6 ms |
10216 KB |
Output is correct |
38 |
Correct |
8 ms |
11656 KB |
Output is correct |
39 |
Correct |
5 ms |
5468 KB |
Output is correct |
40 |
Correct |
6 ms |
5724 KB |
Output is correct |
41 |
Correct |
8 ms |
9820 KB |
Output is correct |
42 |
Correct |
6 ms |
9304 KB |
Output is correct |
43 |
Correct |
6 ms |
9564 KB |
Output is correct |
44 |
Correct |
7 ms |
10212 KB |
Output is correct |
45 |
Correct |
19 ms |
10584 KB |
Output is correct |
46 |
Correct |
14 ms |
10624 KB |
Output is correct |
47 |
Correct |
19 ms |
33836 KB |
Output is correct |
48 |
Correct |
5 ms |
5212 KB |
Output is correct |
49 |
Correct |
4 ms |
5212 KB |
Output is correct |
50 |
Correct |
3 ms |
4956 KB |
Output is correct |
51 |
Correct |
45 ms |
87416 KB |
Output is correct |
52 |
Correct |
55 ms |
94052 KB |
Output is correct |
53 |
Correct |
13 ms |
19288 KB |
Output is correct |
54 |
Correct |
26 ms |
62044 KB |
Output is correct |
55 |
Correct |
34 ms |
78428 KB |
Output is correct |
56 |
Correct |
53 ms |
112476 KB |
Output is correct |
57 |
Correct |
2 ms |
4184 KB |
Output is correct |
58 |
Correct |
49 ms |
111724 KB |
Output is correct |
59 |
Correct |
47 ms |
104204 KB |
Output is correct |
60 |
Correct |
47 ms |
104272 KB |
Output is correct |
61 |
Correct |
45 ms |
99156 KB |
Output is correct |
62 |
Correct |
77 ms |
112616 KB |
Output is correct |
63 |
Correct |
129 ms |
112032 KB |
Output is correct |
64 |
Correct |
131 ms |
111952 KB |
Output is correct |
65 |
Correct |
167 ms |
112212 KB |
Output is correct |
66 |
Correct |
261 ms |
112332 KB |
Output is correct |
67 |
Correct |
167 ms |
112120 KB |
Output is correct |