#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int hsh(pair<int,int> pr)
{
return pr.first * 31000 + pr.second;
}
pair<int,int> dehsh(int x)
{
return {x / 31000, x % 31000};
}
int n,m;
int b[30005],p[30005];
unordered_map<int, bool> viz,luat;
unordered_map<int, int> d;
vector<int> what[30005];
int str,fin;
pair<int,int> q[2000005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> b[i] >> p[i],what[b[i]].push_back(p[i]);
str = b[1],fin = b[2];
int bk = 1e6,fr = 1e6;
q[bk] = {str,0};
viz[hsh({str,0})] = true;
int cate = 0,lul = 1e6;
while (bk <= fr)
{
pair<int,int> nod = q[fr];
int dehnod = hsh(nod);
fr--;
if (luat[dehnod])
continue;
cate++;
if (cate > lul)
assert(false);
luat[dehnod] = true;
int dist = d[dehnod];
if (nod.second != 0)
{
pair<int,int> vec;
vec = {nod.first,0};
int dehvec = hsh(vec);
if (!viz[dehvec] or d[dehvec] > dist)
{
viz[dehvec] = true;
q[++fr] = (vec);
d[dehvec] = dist;
}
vec = {nod.first + nod.second,nod.second};
dehvec = hsh(vec);
if (!viz[dehvec] and vec.first >= 0 and vec.first < n)
{
viz[dehvec] = true;
q[--bk] = vec;
d[dehvec] = dist + 1;
}
vec = {nod.first - nod.second,nod.second};
dehvec = hsh(vec);
if (!viz[dehvec] and vec.first >= 0 and vec.first < n)
{
viz[dehvec] = true;
q[--bk] = vec;
d[dehvec] = dist + 1;
}
}
else
{
for (auto it : what[nod.first])
{
pair<int,int> vec = {nod.first,it};
int dehvec = hsh(vec);
if (!viz[dehvec] or d[dehvec] > dist)
{
viz[dehvec] = true;
q[++fr] = vec;
d[dehvec] = dist;
}
}
}
}
if (!viz[hsh({fin,0})])
cout << -1;
else
cout << d[hsh({fin,0})];
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 |
0 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 |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 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 |
2392 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 |
0 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
3164 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
3420 KB |
Output is correct |
15 |
Correct |
3 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2444 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
2 ms |
3164 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
3468 KB |
Output is correct |
15 |
Correct |
3 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
6 ms |
4444 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
3 ms |
3420 KB |
Output is correct |
24 |
Correct |
6 ms |
4444 KB |
Output is correct |
25 |
Correct |
4 ms |
3812 KB |
Output is correct |
26 |
Correct |
3 ms |
3672 KB |
Output is correct |
27 |
Correct |
3 ms |
3420 KB |
Output is correct |
28 |
Correct |
10 ms |
6000 KB |
Output is correct |
29 |
Correct |
21 ms |
10892 KB |
Output is correct |
30 |
Correct |
6 ms |
4956 KB |
Output is correct |
31 |
Correct |
12 ms |
7064 KB |
Output is correct |
32 |
Correct |
9 ms |
6000 KB |
Output is correct |
33 |
Correct |
46 ms |
18856 KB |
Output is correct |
34 |
Correct |
46 ms |
18860 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 |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
2 ms |
2996 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
3468 KB |
Output is correct |
15 |
Correct |
3 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
6 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2480 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
3 ms |
3420 KB |
Output is correct |
24 |
Correct |
7 ms |
4444 KB |
Output is correct |
25 |
Correct |
4 ms |
3932 KB |
Output is correct |
26 |
Correct |
3 ms |
3676 KB |
Output is correct |
27 |
Correct |
4 ms |
3672 KB |
Output is correct |
28 |
Correct |
11 ms |
6252 KB |
Output is correct |
29 |
Correct |
21 ms |
10832 KB |
Output is correct |
30 |
Correct |
6 ms |
4956 KB |
Output is correct |
31 |
Correct |
12 ms |
7024 KB |
Output is correct |
32 |
Correct |
9 ms |
6088 KB |
Output is correct |
33 |
Correct |
44 ms |
18940 KB |
Output is correct |
34 |
Correct |
45 ms |
18864 KB |
Output is correct |
35 |
Correct |
57 ms |
18484 KB |
Output is correct |
36 |
Correct |
9 ms |
5240 KB |
Output is correct |
37 |
Correct |
108 ms |
30688 KB |
Output is correct |
38 |
Correct |
93 ms |
26096 KB |
Output is correct |
39 |
Correct |
96 ms |
26132 KB |
Output is correct |
40 |
Correct |
94 ms |
26092 KB |
Output is correct |
41 |
Correct |
93 ms |
26108 KB |
Output is correct |
42 |
Correct |
6 ms |
3676 KB |
Output is correct |
43 |
Correct |
5 ms |
3676 KB |
Output is correct |
44 |
Correct |
4 ms |
3164 KB |
Output is correct |
45 |
Correct |
285 ms |
70128 KB |
Output is correct |
46 |
Correct |
290 ms |
70180 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 |
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 |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2484 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
3164 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
3420 KB |
Output is correct |
15 |
Correct |
3 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
6 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2476 KB |
Output is correct |
23 |
Correct |
3 ms |
3420 KB |
Output is correct |
24 |
Correct |
6 ms |
4444 KB |
Output is correct |
25 |
Correct |
4 ms |
3932 KB |
Output is correct |
26 |
Correct |
3 ms |
3676 KB |
Output is correct |
27 |
Correct |
3 ms |
3420 KB |
Output is correct |
28 |
Correct |
10 ms |
6000 KB |
Output is correct |
29 |
Correct |
20 ms |
10976 KB |
Output is correct |
30 |
Correct |
6 ms |
4956 KB |
Output is correct |
31 |
Correct |
11 ms |
7044 KB |
Output is correct |
32 |
Correct |
9 ms |
5996 KB |
Output is correct |
33 |
Correct |
42 ms |
19000 KB |
Output is correct |
34 |
Correct |
46 ms |
18856 KB |
Output is correct |
35 |
Correct |
63 ms |
18492 KB |
Output is correct |
36 |
Correct |
9 ms |
5304 KB |
Output is correct |
37 |
Correct |
105 ms |
30600 KB |
Output is correct |
38 |
Correct |
94 ms |
26124 KB |
Output is correct |
39 |
Correct |
97 ms |
26344 KB |
Output is correct |
40 |
Correct |
92 ms |
26100 KB |
Output is correct |
41 |
Correct |
91 ms |
26204 KB |
Output is correct |
42 |
Correct |
5 ms |
3672 KB |
Output is correct |
43 |
Correct |
5 ms |
3672 KB |
Output is correct |
44 |
Correct |
4 ms |
3164 KB |
Output is correct |
45 |
Correct |
285 ms |
69976 KB |
Output is correct |
46 |
Correct |
307 ms |
70176 KB |
Output is correct |
47 |
Correct |
969 ms |
116172 KB |
Output is correct |
48 |
Correct |
4 ms |
2904 KB |
Output is correct |
49 |
Correct |
3 ms |
2904 KB |
Output is correct |
50 |
Correct |
3 ms |
2652 KB |
Output is correct |
51 |
Correct |
311 ms |
50716 KB |
Output is correct |
52 |
Correct |
378 ms |
59736 KB |
Output is correct |
53 |
Correct |
58 ms |
22696 KB |
Output is correct |
54 |
Correct |
7 ms |
6256 KB |
Output is correct |
55 |
Correct |
15 ms |
8956 KB |
Output is correct |
56 |
Correct |
18 ms |
11148 KB |
Output is correct |
57 |
Correct |
364 ms |
65964 KB |
Output is correct |
58 |
Correct |
32 ms |
16296 KB |
Output is correct |
59 |
Correct |
39 ms |
19120 KB |
Output is correct |
60 |
Correct |
51 ms |
21928 KB |
Output is correct |
61 |
Correct |
49 ms |
20652 KB |
Output is correct |
62 |
Correct |
439 ms |
74648 KB |
Output is correct |
63 |
Execution timed out |
1042 ms |
134880 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |