///
/// ♪ Pizza mozzarella rella rella rella... ♪
///
#define _USE_MATH_DEFINES
#define FAST ios::sync_with_stdio(false),cin.tie(0);
#include <bits/stdc++.h>
#define Loop(x, l, r) for(int x = (l); x < (r); ++x)
#define LoopR(x, l, r) for(int x = (r)-1; x >= (l); --x)
#define all(x) x.begin(), x.end()
#define Kill(x) exit((cout << (x) << '\n', 0))
#define YN(flag) ((flag)? "YES": "NO")
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
typedef std::pair<ll ,ll > pll;
using namespace std;
const int N = 30'010;
map<int,int> d;
vector<int> P[N];
bool vis[N];
int n, m;
void bfs(int sv, int sp)
{
vector<int> q;
vis[sv] = 1;
for(auto p : P[sv]) if(!d.count(sv<<15^p)) d[sv<<15^p] = 0, q.push_back(sv<<15^p);
if(!d.count(sv<<15^sp)) d[sv<<15^sp] = 0, q.push_back(sv<<15^sp);
for(int i = 0, j = q.size(), d = 0; i < q.size(); j = q.size(), ++d) for(; i < j; ++i)
{
int v = q[i]>>15, p = q[i]&32767;
//cerr << v << ' ' << p << ": " << d << '\n';
int u = v+p;
if(0 <= u && u < n)
{
if(!vis[u]){
vis[u] = 1;
for(auto p : P[u]) if(!::d.count(u<<15^p)) ::d[u<<15^p] = d+1, q.push_back(u<<15^p);
}
if(!::d.count(u<<15^p)) ::d[u<<15^p] = d+1, q.push_back(u<<15^p);
}
u = v-p;
if(0 <= u && u < n)
{
if(!vis[u]){
vis[u] = 1;
for(auto p : P[u]) if(!::d.count(u<<15^p)) ::d[u<<15^p] = d+1, q.push_back(u<<15^p);
}
if(!::d.count(u<<15^p)) ::d[u<<15^p] = d+1, q.push_back(u<<15^p);
}
}
}
int main()
{
FAST;
cin >> n >> m;
int p, src, des, dmy;
cin >> src >> p;
cin >> des >> dmy;
P[des].push_back(dmy);
Loop(i,2,m)
{
int v, p;
cin >> v >> p;
P[v].push_back(p);
}
bfs(src, p);
cout << (d.count(des<<15^dmy)? d[des<<15^dmy]: -1) << '\n';
}
Compilation message
skyscraper.cpp: In function 'void bfs(int, int)':
skyscraper.cpp:33:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i = 0, j = q.size(), d = 0; i < q.size(); j = q.size(), ++d) for(; i < j; ++i)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
4 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1036 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
4 ms |
1276 KB |
Output is correct |
15 |
Correct |
4 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
3 ms |
1284 KB |
Output is correct |
15 |
Correct |
3 ms |
1288 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
8 ms |
1780 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
2 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
4 ms |
1356 KB |
Output is correct |
24 |
Correct |
9 ms |
1652 KB |
Output is correct |
25 |
Correct |
4 ms |
1356 KB |
Output is correct |
26 |
Correct |
4 ms |
1356 KB |
Output is correct |
27 |
Correct |
4 ms |
1356 KB |
Output is correct |
28 |
Correct |
11 ms |
2248 KB |
Output is correct |
29 |
Correct |
33 ms |
4092 KB |
Output is correct |
30 |
Correct |
8 ms |
1868 KB |
Output is correct |
31 |
Correct |
16 ms |
2808 KB |
Output is correct |
32 |
Correct |
11 ms |
2164 KB |
Output is correct |
33 |
Correct |
77 ms |
7104 KB |
Output is correct |
34 |
Correct |
79 ms |
7100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
1032 KB |
Output is correct |
14 |
Correct |
4 ms |
1356 KB |
Output is correct |
15 |
Correct |
3 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
8 ms |
1740 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
2 ms |
1168 KB |
Output is correct |
21 |
Correct |
1 ms |
1036 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
4 ms |
1356 KB |
Output is correct |
24 |
Correct |
9 ms |
1676 KB |
Output is correct |
25 |
Correct |
5 ms |
1496 KB |
Output is correct |
26 |
Correct |
4 ms |
1356 KB |
Output is correct |
27 |
Correct |
4 ms |
1356 KB |
Output is correct |
28 |
Correct |
12 ms |
2252 KB |
Output is correct |
29 |
Correct |
43 ms |
4060 KB |
Output is correct |
30 |
Correct |
8 ms |
1868 KB |
Output is correct |
31 |
Correct |
16 ms |
2740 KB |
Output is correct |
32 |
Correct |
11 ms |
2124 KB |
Output is correct |
33 |
Correct |
86 ms |
7112 KB |
Output is correct |
34 |
Correct |
95 ms |
7068 KB |
Output is correct |
35 |
Correct |
86 ms |
6480 KB |
Output is correct |
36 |
Correct |
10 ms |
1996 KB |
Output is correct |
37 |
Correct |
171 ms |
10024 KB |
Output is correct |
38 |
Correct |
129 ms |
9484 KB |
Output is correct |
39 |
Correct |
126 ms |
9532 KB |
Output is correct |
40 |
Correct |
144 ms |
9500 KB |
Output is correct |
41 |
Correct |
170 ms |
9504 KB |
Output is correct |
42 |
Correct |
12 ms |
1612 KB |
Output is correct |
43 |
Correct |
9 ms |
1612 KB |
Output is correct |
44 |
Correct |
9 ms |
1356 KB |
Output is correct |
45 |
Correct |
524 ms |
25748 KB |
Output is correct |
46 |
Correct |
437 ms |
25688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
3 ms |
1356 KB |
Output is correct |
15 |
Correct |
3 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
8 ms |
1688 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
2 ms |
1160 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
1356 KB |
Output is correct |
24 |
Correct |
7 ms |
1612 KB |
Output is correct |
25 |
Correct |
4 ms |
1420 KB |
Output is correct |
26 |
Correct |
4 ms |
1356 KB |
Output is correct |
27 |
Correct |
4 ms |
1292 KB |
Output is correct |
28 |
Correct |
11 ms |
2252 KB |
Output is correct |
29 |
Correct |
33 ms |
4056 KB |
Output is correct |
30 |
Correct |
8 ms |
1868 KB |
Output is correct |
31 |
Correct |
16 ms |
2764 KB |
Output is correct |
32 |
Correct |
10 ms |
2124 KB |
Output is correct |
33 |
Correct |
79 ms |
7068 KB |
Output is correct |
34 |
Correct |
85 ms |
7224 KB |
Output is correct |
35 |
Correct |
74 ms |
6456 KB |
Output is correct |
36 |
Correct |
11 ms |
1964 KB |
Output is correct |
37 |
Correct |
140 ms |
10008 KB |
Output is correct |
38 |
Correct |
136 ms |
9520 KB |
Output is correct |
39 |
Correct |
139 ms |
9460 KB |
Output is correct |
40 |
Correct |
159 ms |
9516 KB |
Output is correct |
41 |
Correct |
127 ms |
9524 KB |
Output is correct |
42 |
Correct |
9 ms |
1612 KB |
Output is correct |
43 |
Correct |
8 ms |
1612 KB |
Output is correct |
44 |
Correct |
9 ms |
1380 KB |
Output is correct |
45 |
Correct |
466 ms |
25748 KB |
Output is correct |
46 |
Correct |
451 ms |
25680 KB |
Output is correct |
47 |
Execution timed out |
1094 ms |
37924 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |