// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int maxn=3e4+5;
int n,m;
int b[maxn],p[maxn];
vector<int>v[maxn];
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
unordered_map<int,int,custom_hash>dis;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int mx = max(n,m) + 1;
for(int i = 0;i < m;i++){
cin >> b[i] >> p[i];
v[b[i]].push_back(p[i]);
}
queue<pair<int,int>>q;
q.push({b[0],p[0]});
dis[b[0] * mx + p[0]] = 0;
while(!q.empty()){
int idx = q.front().first;
int pi = q.front().second;
q.pop();
int now = dis[idx * mx + pi];
if(idx == b[1]) return cout<<now,0;
if(idx - pi >= 0){
auto t = dis.find((idx - pi) * mx + pi);
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[(idx - pi) * mx + pi] = now + 1;
q.push({idx - pi,pi});
}
}
if(idx + pi < n){
auto t = dis.find((idx + pi) * mx + pi);
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[(idx + pi) * mx + pi] = now + 1;
q.push({idx + pi,pi});
}
}
for(auto i : v[idx]){
if(idx - i >= 0){
auto t = dis.find((idx - i) * mx + i);
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[(idx - i) * mx + i] = now + 1;
q.push({idx - i,i});
}
}
if(idx + i < n){
auto t = dis.find((idx + i) * mx + i);
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[(idx + i) * mx + i] = now + 1;
q.push({idx + i,i});
}
}
}
}
cout<<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
6 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
6 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
1236 KB |
Output is correct |
24 |
Correct |
3 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
5 ms |
1876 KB |
Output is correct |
29 |
Correct |
14 ms |
3556 KB |
Output is correct |
30 |
Correct |
4 ms |
1620 KB |
Output is correct |
31 |
Correct |
11 ms |
2304 KB |
Output is correct |
32 |
Correct |
8 ms |
2184 KB |
Output is correct |
33 |
Correct |
39 ms |
6068 KB |
Output is correct |
34 |
Correct |
20 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
6 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
1236 KB |
Output is correct |
24 |
Correct |
4 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
3 ms |
1236 KB |
Output is correct |
28 |
Correct |
5 ms |
1876 KB |
Output is correct |
29 |
Correct |
15 ms |
3636 KB |
Output is correct |
30 |
Correct |
4 ms |
1620 KB |
Output is correct |
31 |
Correct |
7 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2132 KB |
Output is correct |
33 |
Correct |
31 ms |
6072 KB |
Output is correct |
34 |
Correct |
17 ms |
3684 KB |
Output is correct |
35 |
Correct |
14 ms |
2732 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
9 ms |
2260 KB |
Output is correct |
38 |
Correct |
12 ms |
2864 KB |
Output is correct |
39 |
Correct |
5 ms |
1492 KB |
Output is correct |
40 |
Correct |
5 ms |
1492 KB |
Output is correct |
41 |
Correct |
7 ms |
1724 KB |
Output is correct |
42 |
Correct |
5 ms |
1492 KB |
Output is correct |
43 |
Correct |
5 ms |
1492 KB |
Output is correct |
44 |
Correct |
6 ms |
1520 KB |
Output is correct |
45 |
Correct |
504 ms |
22164 KB |
Output is correct |
46 |
Correct |
388 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
6 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
1148 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
1236 KB |
Output is correct |
24 |
Correct |
3 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1060 KB |
Output is correct |
26 |
Correct |
1 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
6 ms |
1832 KB |
Output is correct |
29 |
Correct |
14 ms |
3584 KB |
Output is correct |
30 |
Correct |
4 ms |
1620 KB |
Output is correct |
31 |
Correct |
9 ms |
2352 KB |
Output is correct |
32 |
Correct |
8 ms |
2100 KB |
Output is correct |
33 |
Correct |
38 ms |
6136 KB |
Output is correct |
34 |
Correct |
18 ms |
3776 KB |
Output is correct |
35 |
Correct |
14 ms |
2812 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
8 ms |
2260 KB |
Output is correct |
38 |
Correct |
12 ms |
2864 KB |
Output is correct |
39 |
Correct |
6 ms |
1492 KB |
Output is correct |
40 |
Correct |
6 ms |
1504 KB |
Output is correct |
41 |
Correct |
7 ms |
1748 KB |
Output is correct |
42 |
Correct |
6 ms |
1620 KB |
Output is correct |
43 |
Correct |
5 ms |
1492 KB |
Output is correct |
44 |
Correct |
6 ms |
1496 KB |
Output is correct |
45 |
Correct |
539 ms |
22044 KB |
Output is correct |
46 |
Correct |
334 ms |
12840 KB |
Output is correct |
47 |
Correct |
11 ms |
2260 KB |
Output is correct |
48 |
Correct |
9 ms |
1656 KB |
Output is correct |
49 |
Correct |
5 ms |
1620 KB |
Output is correct |
50 |
Correct |
5 ms |
1492 KB |
Output is correct |
51 |
Correct |
23 ms |
4144 KB |
Output is correct |
52 |
Correct |
22 ms |
4272 KB |
Output is correct |
53 |
Correct |
8 ms |
2132 KB |
Output is correct |
54 |
Correct |
4 ms |
1620 KB |
Output is correct |
55 |
Correct |
6 ms |
2132 KB |
Output is correct |
56 |
Correct |
12 ms |
3376 KB |
Output is correct |
57 |
Correct |
1 ms |
1108 KB |
Output is correct |
58 |
Correct |
27 ms |
4068 KB |
Output is correct |
59 |
Correct |
17 ms |
3788 KB |
Output is correct |
60 |
Correct |
19 ms |
3772 KB |
Output is correct |
61 |
Correct |
19 ms |
3684 KB |
Output is correct |
62 |
Correct |
251 ms |
21444 KB |
Output is correct |
63 |
Execution timed out |
1100 ms |
82632 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |