#include <bits/stdc++.h>
using namespace std;
int hsh(pair<int,int> pr)
{
return pr.first * 31000 + pr.second;
}
int n,m;
int b[30005],p[30005];
unordered_map<int, bool> luat;
bitset<1000000000> viz;
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})] = 1;
int cate = 0,lul = 1e6;
while (bk <= fr)
{
pair<int,int> nod = q[fr];
//cout << nod.first << ' ' << nod.second << endl;
int dehnod = hsh(nod);
fr--;
if (luat[dehnod])
continue;
if (nod.first == fin and nod.second == 0)
break;
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] = 1;
q[++fr] = (vec);
d[dehvec] = dist;
}
vec = {nod.first + nod.second,nod.second};
dehvec = hsh(vec);
if (vec.first >= 0 and vec.first < n and !viz[dehvec])
{
viz[dehvec] = 1;
q[--bk] = vec;
d[dehvec] = dist + 1;
}
vec = {nod.first - nod.second,nod.second};
dehvec = hsh(vec);
if (vec.first >= 0 and vec.first < n and !viz[dehvec])
{
viz[dehvec] = 1;
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] = 1;
q[++fr] = vec;
d[dehvec] = dist;
}
}
}
}
if (!viz[hsh({fin,0})])
cout << -1;
else
cout << d[hsh({fin,0})];
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:31:18: warning: unused variable 'lul' [-Wunused-variable]
31 | int cate = 0,lul = 1e6;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1112 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 |
1 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 |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
1 ms |
1368 KB |
Output is correct |
14 |
Correct |
2 ms |
2140 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1112 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 |
1 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 |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
2136 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
4184 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
9052 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 |
7260 KB |
Output is correct |
24 |
Correct |
7 ms |
8540 KB |
Output is correct |
25 |
Correct |
1 ms |
2620 KB |
Output is correct |
26 |
Correct |
5 ms |
8028 KB |
Output is correct |
27 |
Correct |
4 ms |
8540 KB |
Output is correct |
28 |
Correct |
8 ms |
11028 KB |
Output is correct |
29 |
Correct |
16 ms |
14460 KB |
Output is correct |
30 |
Correct |
7 ms |
10320 KB |
Output is correct |
31 |
Correct |
11 ms |
11796 KB |
Output is correct |
32 |
Correct |
9 ms |
11028 KB |
Output is correct |
33 |
Correct |
32 ms |
19840 KB |
Output is correct |
34 |
Correct |
26 ms |
14704 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 |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1376 KB |
Output is correct |
14 |
Correct |
2 ms |
2140 KB |
Output is correct |
15 |
Correct |
2 ms |
1624 KB |
Output is correct |
16 |
Correct |
0 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
4188 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
9048 KB |
Output is correct |
21 |
Correct |
1 ms |
1112 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
7260 KB |
Output is correct |
24 |
Correct |
5 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
5 ms |
8000 KB |
Output is correct |
27 |
Correct |
4 ms |
8536 KB |
Output is correct |
28 |
Correct |
12 ms |
11028 KB |
Output is correct |
29 |
Correct |
17 ms |
14460 KB |
Output is correct |
30 |
Correct |
6 ms |
10332 KB |
Output is correct |
31 |
Correct |
10 ms |
11756 KB |
Output is correct |
32 |
Correct |
8 ms |
11028 KB |
Output is correct |
33 |
Correct |
31 ms |
20032 KB |
Output is correct |
34 |
Correct |
22 ms |
14716 KB |
Output is correct |
35 |
Correct |
17 ms |
10692 KB |
Output is correct |
36 |
Correct |
2 ms |
3672 KB |
Output is correct |
37 |
Correct |
10 ms |
10912 KB |
Output is correct |
38 |
Correct |
11 ms |
10912 KB |
Output is correct |
39 |
Correct |
4 ms |
2396 KB |
Output is correct |
40 |
Correct |
8 ms |
8284 KB |
Output is correct |
41 |
Correct |
17 ms |
12756 KB |
Output is correct |
42 |
Correct |
5 ms |
8284 KB |
Output is correct |
43 |
Correct |
5 ms |
8796 KB |
Output is correct |
44 |
Correct |
6 ms |
9304 KB |
Output is correct |
45 |
Correct |
136 ms |
54060 KB |
Output is correct |
46 |
Correct |
60 ms |
32420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1368 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 |
1 ms |
1112 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 |
980 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
2140 KB |
Output is correct |
15 |
Correct |
1 ms |
1840 KB |
Output is correct |
16 |
Correct |
0 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
4188 KB |
Output is correct |
18 |
Correct |
0 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
4 ms |
9200 KB |
Output is correct |
21 |
Correct |
0 ms |
1116 KB |
Output is correct |
22 |
Correct |
0 ms |
1116 KB |
Output is correct |
23 |
Correct |
4 ms |
7260 KB |
Output is correct |
24 |
Correct |
5 ms |
8616 KB |
Output is correct |
25 |
Correct |
1 ms |
2580 KB |
Output is correct |
26 |
Correct |
3 ms |
8028 KB |
Output is correct |
27 |
Correct |
4 ms |
8540 KB |
Output is correct |
28 |
Correct |
9 ms |
11024 KB |
Output is correct |
29 |
Correct |
15 ms |
14348 KB |
Output is correct |
30 |
Correct |
6 ms |
10332 KB |
Output is correct |
31 |
Correct |
10 ms |
11772 KB |
Output is correct |
32 |
Correct |
8 ms |
11024 KB |
Output is correct |
33 |
Correct |
29 ms |
19960 KB |
Output is correct |
34 |
Correct |
17 ms |
14716 KB |
Output is correct |
35 |
Correct |
15 ms |
10696 KB |
Output is correct |
36 |
Correct |
2 ms |
3676 KB |
Output is correct |
37 |
Correct |
17 ms |
10936 KB |
Output is correct |
38 |
Correct |
10 ms |
10844 KB |
Output is correct |
39 |
Correct |
4 ms |
2396 KB |
Output is correct |
40 |
Correct |
10 ms |
8028 KB |
Output is correct |
41 |
Correct |
15 ms |
13000 KB |
Output is correct |
42 |
Correct |
6 ms |
8284 KB |
Output is correct |
43 |
Correct |
6 ms |
8796 KB |
Output is correct |
44 |
Correct |
6 ms |
9304 KB |
Output is correct |
45 |
Correct |
156 ms |
53804 KB |
Output is correct |
46 |
Correct |
84 ms |
32296 KB |
Output is correct |
47 |
Correct |
28 ms |
41904 KB |
Output is correct |
48 |
Correct |
5 ms |
1624 KB |
Output is correct |
49 |
Correct |
6 ms |
1628 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 KB |
Output is correct |
51 |
Correct |
67 ms |
98376 KB |
Output is correct |
52 |
Correct |
75 ms |
104720 KB |
Output is correct |
53 |
Correct |
22 ms |
23388 KB |
Output is correct |
54 |
Correct |
30 ms |
64028 KB |
Output is correct |
55 |
Correct |
40 ms |
83568 KB |
Output is correct |
56 |
Correct |
62 ms |
121420 KB |
Output is correct |
57 |
Correct |
3 ms |
2904 KB |
Output is correct |
58 |
Correct |
69 ms |
124464 KB |
Output is correct |
59 |
Correct |
65 ms |
116160 KB |
Output is correct |
60 |
Correct |
77 ms |
116180 KB |
Output is correct |
61 |
Correct |
60 ms |
108188 KB |
Output is correct |
62 |
Correct |
256 ms |
160476 KB |
Output is correct |
63 |
Execution timed out |
3026 ms |
262144 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |