///
/// ♪ 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;
const int mod = 15000017;
struct mymap
{
pii* v[mod] = {};
short sz[mod] = {};
int count(int x)
{
int y = x%mod;
if(!v[y]) return 0;
Loop(i,0,sz[y])
if(v[y][i].F==x)
return 1;
return 0;
}
int& operator[](int x)
{
int y = x%mod;
if(!v[y]) v[y] = new pii[1], sz[y]=0;
Loop(i,0,sz[y])
if(v[y][i].F==x)
return v[y][i].S;
if(sz[y] && (sz[y]&-sz[y]) == sz[y]){
auto tmp = new pii[sz[y]*2];
Loop(i,0,sz[y]) tmp[i] = v[y][i];
delete[](v[y]);
v[y] = tmp;
}
v[y][sz[y]++] = {x,0};
return v[y][sz[y]-1].S;
}
};
mymap 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)
{
if(q.size() > 8'000'000) exit(0);
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);
}
}
}
bool isPrime(int n)
{
for(int d = 2; d*d <= n; ++d)
if(n%d == 0)
return 0;
return 1;
}
int main()
{
//Loop(i,15000000,INT_MAX) if(isPrime(i)){cout << i;break;}
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:66:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | 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 |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 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 |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
2 ms |
1996 KB |
Output is correct |
12 |
Correct |
2 ms |
1868 KB |
Output is correct |
13 |
Correct |
2 ms |
1868 KB |
Output is correct |
14 |
Correct |
2 ms |
2124 KB |
Output is correct |
15 |
Correct |
2 ms |
2124 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 |
1060 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
2 ms |
1996 KB |
Output is correct |
12 |
Correct |
2 ms |
1868 KB |
Output is correct |
13 |
Correct |
2 ms |
1868 KB |
Output is correct |
14 |
Correct |
2 ms |
2124 KB |
Output is correct |
15 |
Correct |
3 ms |
2124 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
9 ms |
10572 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
2 ms |
1228 KB |
Output is correct |
20 |
Correct |
10 ms |
17404 KB |
Output is correct |
21 |
Correct |
2 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
15 ms |
22348 KB |
Output is correct |
24 |
Correct |
21 ms |
33452 KB |
Output is correct |
25 |
Correct |
20 ms |
33900 KB |
Output is correct |
26 |
Correct |
12 ms |
20044 KB |
Output is correct |
27 |
Correct |
13 ms |
21016 KB |
Output is correct |
28 |
Correct |
22 ms |
32440 KB |
Output is correct |
29 |
Correct |
21 ms |
21280 KB |
Output is correct |
30 |
Correct |
13 ms |
19672 KB |
Output is correct |
31 |
Correct |
16 ms |
20368 KB |
Output is correct |
32 |
Correct |
16 ms |
20584 KB |
Output is correct |
33 |
Correct |
32 ms |
23952 KB |
Output is correct |
34 |
Correct |
31 ms |
24044 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 |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1484 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
2 ms |
1996 KB |
Output is correct |
12 |
Correct |
2 ms |
1868 KB |
Output is correct |
13 |
Correct |
2 ms |
1868 KB |
Output is correct |
14 |
Correct |
2 ms |
2124 KB |
Output is correct |
15 |
Correct |
2 ms |
2052 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
8 ms |
10572 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1228 KB |
Output is correct |
20 |
Correct |
10 ms |
17356 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
14 ms |
22348 KB |
Output is correct |
24 |
Correct |
27 ms |
33444 KB |
Output is correct |
25 |
Correct |
20 ms |
33900 KB |
Output is correct |
26 |
Correct |
13 ms |
20044 KB |
Output is correct |
27 |
Correct |
14 ms |
20996 KB |
Output is correct |
28 |
Correct |
26 ms |
32356 KB |
Output is correct |
29 |
Correct |
19 ms |
21192 KB |
Output is correct |
30 |
Correct |
15 ms |
19792 KB |
Output is correct |
31 |
Correct |
17 ms |
20428 KB |
Output is correct |
32 |
Correct |
14 ms |
20636 KB |
Output is correct |
33 |
Correct |
31 ms |
23940 KB |
Output is correct |
34 |
Correct |
33 ms |
24124 KB |
Output is correct |
35 |
Correct |
48 ms |
44452 KB |
Output is correct |
36 |
Correct |
22 ms |
26188 KB |
Output is correct |
37 |
Correct |
71 ms |
42040 KB |
Output is correct |
38 |
Correct |
78 ms |
60888 KB |
Output is correct |
39 |
Correct |
73 ms |
60848 KB |
Output is correct |
40 |
Correct |
70 ms |
60984 KB |
Output is correct |
41 |
Correct |
72 ms |
60916 KB |
Output is correct |
42 |
Correct |
18 ms |
20172 KB |
Output is correct |
43 |
Correct |
20 ms |
21044 KB |
Output is correct |
44 |
Correct |
16 ms |
17484 KB |
Output is correct |
45 |
Correct |
109 ms |
44212 KB |
Output is correct |
46 |
Correct |
94 ms |
44196 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 |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
3 ms |
1996 KB |
Output is correct |
12 |
Correct |
2 ms |
1868 KB |
Output is correct |
13 |
Correct |
2 ms |
1868 KB |
Output is correct |
14 |
Correct |
2 ms |
2124 KB |
Output is correct |
15 |
Correct |
2 ms |
2124 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
9 ms |
10620 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1228 KB |
Output is correct |
20 |
Correct |
10 ms |
17448 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
13 ms |
22444 KB |
Output is correct |
24 |
Correct |
22 ms |
33484 KB |
Output is correct |
25 |
Correct |
21 ms |
33868 KB |
Output is correct |
26 |
Correct |
13 ms |
19988 KB |
Output is correct |
27 |
Correct |
13 ms |
20944 KB |
Output is correct |
28 |
Correct |
21 ms |
32324 KB |
Output is correct |
29 |
Correct |
19 ms |
21192 KB |
Output is correct |
30 |
Correct |
13 ms |
19696 KB |
Output is correct |
31 |
Correct |
14 ms |
20380 KB |
Output is correct |
32 |
Correct |
15 ms |
20560 KB |
Output is correct |
33 |
Correct |
32 ms |
23996 KB |
Output is correct |
34 |
Correct |
35 ms |
24128 KB |
Output is correct |
35 |
Correct |
55 ms |
44476 KB |
Output is correct |
36 |
Correct |
19 ms |
26244 KB |
Output is correct |
37 |
Correct |
63 ms |
42276 KB |
Output is correct |
38 |
Correct |
69 ms |
60860 KB |
Output is correct |
39 |
Correct |
68 ms |
60884 KB |
Output is correct |
40 |
Correct |
82 ms |
60928 KB |
Output is correct |
41 |
Correct |
73 ms |
60920 KB |
Output is correct |
42 |
Correct |
19 ms |
20172 KB |
Output is correct |
43 |
Correct |
19 ms |
21076 KB |
Output is correct |
44 |
Correct |
15 ms |
17484 KB |
Output is correct |
45 |
Correct |
92 ms |
44148 KB |
Output is correct |
46 |
Correct |
94 ms |
44152 KB |
Output is correct |
47 |
Correct |
292 ms |
173356 KB |
Output is correct |
48 |
Correct |
7 ms |
1484 KB |
Output is correct |
49 |
Correct |
7 ms |
1484 KB |
Output is correct |
50 |
Correct |
5 ms |
1356 KB |
Output is correct |
51 |
Correct |
176 ms |
159296 KB |
Output is correct |
52 |
Correct |
194 ms |
160212 KB |
Output is correct |
53 |
Correct |
114 ms |
141240 KB |
Output is correct |
54 |
Correct |
38 ms |
65476 KB |
Output is correct |
55 |
Correct |
51 ms |
82088 KB |
Output is correct |
56 |
Correct |
78 ms |
115968 KB |
Output is correct |
57 |
Correct |
177 ms |
131900 KB |
Output is correct |
58 |
Correct |
89 ms |
116808 KB |
Output is correct |
59 |
Correct |
97 ms |
118536 KB |
Output is correct |
60 |
Correct |
108 ms |
125060 KB |
Output is correct |
61 |
Correct |
111 ms |
133096 KB |
Output is correct |
62 |
Correct |
245 ms |
165892 KB |
Output is correct |
63 |
Correct |
815 ms |
223508 KB |
Output is correct |
64 |
Correct |
885 ms |
239732 KB |
Output is correct |
65 |
Execution timed out |
1081 ms |
248456 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |