#include <bits/stdc++.h>
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;
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];
deque<pair<int,int>> q;
q.push_back({str,0});
viz[hsh({str,0})] = true;
int lul = 1e7,cate = 0;
while (!q.empty())
{
pair<int,int> nod = q.front();
int dehnod = hsh(nod);
q.pop_front();
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.push_front(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.push_back(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.push_back(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.push_front(vec);
d[dehvec] = dist;
}
}
}
}
if (!viz[hsh({fin,0})])
cout << -1;
else
cout << d[hsh({fin,0})];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
2140 KB |
Output is correct |
15 |
Correct |
3 ms |
2112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1756 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
2140 KB |
Output is correct |
15 |
Correct |
3 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
7 ms |
3036 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1112 KB |
Output is correct |
23 |
Correct |
3 ms |
2184 KB |
Output is correct |
24 |
Correct |
6 ms |
3064 KB |
Output is correct |
25 |
Correct |
5 ms |
2476 KB |
Output is correct |
26 |
Correct |
3 ms |
2140 KB |
Output is correct |
27 |
Correct |
3 ms |
2140 KB |
Output is correct |
28 |
Correct |
10 ms |
4596 KB |
Output is correct |
29 |
Correct |
20 ms |
9100 KB |
Output is correct |
30 |
Correct |
6 ms |
3420 KB |
Output is correct |
31 |
Correct |
12 ms |
5488 KB |
Output is correct |
32 |
Correct |
9 ms |
4420 KB |
Output is correct |
33 |
Correct |
43 ms |
16824 KB |
Output is correct |
34 |
Correct |
43 ms |
16824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
2140 KB |
Output is correct |
15 |
Correct |
3 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
6 ms |
3224 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
0 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
2140 KB |
Output is correct |
24 |
Correct |
9 ms |
3164 KB |
Output is correct |
25 |
Correct |
4 ms |
2420 KB |
Output is correct |
26 |
Correct |
3 ms |
2140 KB |
Output is correct |
27 |
Correct |
3 ms |
2140 KB |
Output is correct |
28 |
Correct |
10 ms |
4692 KB |
Output is correct |
29 |
Correct |
21 ms |
9100 KB |
Output is correct |
30 |
Correct |
6 ms |
3420 KB |
Output is correct |
31 |
Correct |
12 ms |
5488 KB |
Output is correct |
32 |
Correct |
9 ms |
4452 KB |
Output is correct |
33 |
Correct |
43 ms |
16740 KB |
Output is correct |
34 |
Correct |
42 ms |
16824 KB |
Output is correct |
35 |
Correct |
59 ms |
16952 KB |
Output is correct |
36 |
Correct |
9 ms |
3768 KB |
Output is correct |
37 |
Correct |
108 ms |
28468 KB |
Output is correct |
38 |
Correct |
96 ms |
24048 KB |
Output is correct |
39 |
Correct |
92 ms |
24048 KB |
Output is correct |
40 |
Correct |
97 ms |
24044 KB |
Output is correct |
41 |
Correct |
92 ms |
24044 KB |
Output is correct |
42 |
Correct |
6 ms |
2652 KB |
Output is correct |
43 |
Correct |
5 ms |
2396 KB |
Output is correct |
44 |
Correct |
4 ms |
1884 KB |
Output is correct |
45 |
Correct |
289 ms |
65496 KB |
Output is correct |
46 |
Correct |
303 ms |
65316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1172 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
3 ms |
2140 KB |
Output is correct |
15 |
Correct |
3 ms |
2136 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
8 ms |
3192 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
0 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
2140 KB |
Output is correct |
24 |
Correct |
6 ms |
3164 KB |
Output is correct |
25 |
Correct |
4 ms |
2396 KB |
Output is correct |
26 |
Correct |
3 ms |
2356 KB |
Output is correct |
27 |
Correct |
3 ms |
2140 KB |
Output is correct |
28 |
Correct |
9 ms |
4720 KB |
Output is correct |
29 |
Correct |
21 ms |
9164 KB |
Output is correct |
30 |
Correct |
6 ms |
3416 KB |
Output is correct |
31 |
Correct |
11 ms |
5512 KB |
Output is correct |
32 |
Correct |
9 ms |
4548 KB |
Output is correct |
33 |
Correct |
43 ms |
16816 KB |
Output is correct |
34 |
Correct |
45 ms |
16812 KB |
Output is correct |
35 |
Correct |
61 ms |
16952 KB |
Output is correct |
36 |
Correct |
9 ms |
3764 KB |
Output is correct |
37 |
Correct |
100 ms |
28432 KB |
Output is correct |
38 |
Correct |
94 ms |
23996 KB |
Output is correct |
39 |
Correct |
95 ms |
24044 KB |
Output is correct |
40 |
Correct |
91 ms |
24044 KB |
Output is correct |
41 |
Correct |
103 ms |
24048 KB |
Output is correct |
42 |
Correct |
6 ms |
2652 KB |
Output is correct |
43 |
Correct |
5 ms |
2396 KB |
Output is correct |
44 |
Correct |
4 ms |
1884 KB |
Output is correct |
45 |
Correct |
271 ms |
65492 KB |
Output is correct |
46 |
Correct |
311 ms |
65444 KB |
Output is correct |
47 |
Execution timed out |
1024 ms |
109664 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |