///
/// ♪ 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;
#pragma GCC optimize("O3,unroll-loops")
const int N = 30'010;
int mod;
struct myset
{
int v[10000000+32768][6] = {};
unsigned char sz[10000000+32768] = {};
bool add(int x)
{
int y = x%mod;
Loop(i,0,sz[y])
if(v[y][i]==x)
return 0;
if(sz[y] == 6) exit(0);
v[y][sz[y]++] = x;
return 1;
}
};
myset d;
vector<int> P[N];
bool vis[N];
int n, m;
int bfs(int sv, int sp, int des)
{
if(sv == des) return 0;
vector<int> q, q2;
vis[sv] = 1;
for(auto p : P[sv]) if(d.add(sv<<15^p)) q.push_back(sv<<15^p);
if(d.add(sv<<15^sp)) q.push_back(sv<<15^sp);
for(int d = 0; q.size();)
{
for(ull i = 0; i < q.size(); ++i)
{
int v = q[i]>>15, p = q[i]&32767;
//cerr << v << ' ' << p << ": " << d << '\n';
int u = v+p;
if(u == des) return d+1;
if(0 <= u && u < n)
{
if(!vis[u]){
vis[u] = 1;
for(auto p : P[u]) if(::d.add(u<<15^p)) q2.push_back(u<<15^p);
}
if(::d.add(u<<15^p)) q2.push_back(u<<15^p);
}
u = v-p;
if(u == des) return d+1;
if(0 <= u && u < n)
{
if(!vis[u]){
vis[u] = 1;
for(auto p : P[u]) if(::d.add(u<<15^p)) q2.push_back(u<<15^p);
}
if(::d.add(u<<15^p)) q2.push_back(u<<15^p);
}
}
q = q2;
q2.clear();
++d;
}
return -1;
}
bool isPrime(int n)
{
if(n%2 == 0) return n==2;
for(int d = 3; d*d <= n; d += 2)
if(n%d == 0)
return 0;
return 1;
}
int main()
{
srand(time(0));
for(;;){int x = 10000000+(rand()&32767); if(isPrime(x)){mod=x; 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);
}
cout << bfs(src, p, des) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1868 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1868 KB |
Output is correct |
14 |
Correct |
2 ms |
1996 KB |
Output is correct |
15 |
Correct |
2 ms |
1868 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 |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1868 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1996 KB |
Output is correct |
14 |
Correct |
2 ms |
1996 KB |
Output is correct |
15 |
Correct |
2 ms |
1996 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
4 ms |
6476 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
6 ms |
10700 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
7 ms |
14156 KB |
Output is correct |
24 |
Correct |
10 ms |
18508 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
6 ms |
9420 KB |
Output is correct |
27 |
Correct |
10 ms |
15948 KB |
Output is correct |
28 |
Correct |
17 ms |
29004 KB |
Output is correct |
29 |
Correct |
13 ms |
17484 KB |
Output is correct |
30 |
Correct |
10 ms |
16204 KB |
Output is correct |
31 |
Correct |
12 ms |
17444 KB |
Output is correct |
32 |
Correct |
11 ms |
17484 KB |
Output is correct |
33 |
Correct |
17 ms |
17484 KB |
Output is correct |
34 |
Correct |
13 ms |
15420 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 |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1868 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1996 KB |
Output is correct |
14 |
Correct |
2 ms |
1996 KB |
Output is correct |
15 |
Correct |
2 ms |
1868 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
5 ms |
7500 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
10 ms |
17484 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
11 ms |
20300 KB |
Output is correct |
24 |
Correct |
16 ms |
29116 KB |
Output is correct |
25 |
Correct |
4 ms |
5324 KB |
Output is correct |
26 |
Correct |
9 ms |
15180 KB |
Output is correct |
27 |
Correct |
11 ms |
18636 KB |
Output is correct |
28 |
Correct |
20 ms |
33228 KB |
Output is correct |
29 |
Correct |
10 ms |
12620 KB |
Output is correct |
30 |
Correct |
8 ms |
12620 KB |
Output is correct |
31 |
Correct |
9 ms |
12620 KB |
Output is correct |
32 |
Correct |
8 ms |
12620 KB |
Output is correct |
33 |
Correct |
14 ms |
12664 KB |
Output is correct |
34 |
Correct |
11 ms |
12404 KB |
Output is correct |
35 |
Correct |
27 ms |
40980 KB |
Output is correct |
36 |
Correct |
6 ms |
7500 KB |
Output is correct |
37 |
Correct |
16 ms |
21140 KB |
Output is correct |
38 |
Correct |
27 ms |
41540 KB |
Output is correct |
39 |
Correct |
8 ms |
1484 KB |
Output is correct |
40 |
Correct |
9 ms |
2896 KB |
Output is correct |
41 |
Correct |
27 ms |
42412 KB |
Output is correct |
42 |
Correct |
12 ms |
11468 KB |
Output is correct |
43 |
Correct |
12 ms |
13644 KB |
Output is correct |
44 |
Correct |
12 ms |
12940 KB |
Output is correct |
45 |
Correct |
45 ms |
26052 KB |
Output is correct |
46 |
Correct |
28 ms |
19140 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 |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1868 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1996 KB |
Output is correct |
14 |
Correct |
2 ms |
1996 KB |
Output is correct |
15 |
Correct |
2 ms |
1868 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
5 ms |
7372 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
10 ms |
17484 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
11 ms |
20320 KB |
Output is correct |
24 |
Correct |
15 ms |
29436 KB |
Output is correct |
25 |
Correct |
4 ms |
5452 KB |
Output is correct |
26 |
Correct |
9 ms |
14924 KB |
Output is correct |
27 |
Correct |
11 ms |
17980 KB |
Output is correct |
28 |
Correct |
19 ms |
31516 KB |
Output is correct |
29 |
Correct |
15 ms |
18636 KB |
Output is correct |
30 |
Correct |
11 ms |
17488 KB |
Output is correct |
31 |
Correct |
12 ms |
18636 KB |
Output is correct |
32 |
Correct |
12 ms |
18632 KB |
Output is correct |
33 |
Correct |
19 ms |
19636 KB |
Output is correct |
34 |
Correct |
15 ms |
18508 KB |
Output is correct |
35 |
Correct |
38 ms |
68792 KB |
Output is correct |
36 |
Correct |
6 ms |
8140 KB |
Output is correct |
37 |
Correct |
18 ms |
26832 KB |
Output is correct |
38 |
Correct |
38 ms |
67048 KB |
Output is correct |
39 |
Correct |
8 ms |
1612 KB |
Output is correct |
40 |
Correct |
8 ms |
2892 KB |
Output is correct |
41 |
Correct |
40 ms |
70072 KB |
Output is correct |
42 |
Correct |
14 ms |
14284 KB |
Output is correct |
43 |
Correct |
15 ms |
17884 KB |
Output is correct |
44 |
Correct |
15 ms |
16404 KB |
Output is correct |
45 |
Correct |
63 ms |
30920 KB |
Output is correct |
46 |
Correct |
33 ms |
26580 KB |
Output is correct |
47 |
Correct |
36 ms |
59560 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 |
1308 KB |
Output is correct |
51 |
Correct |
74 ms |
135956 KB |
Output is correct |
52 |
Correct |
85 ms |
149100 KB |
Output is correct |
53 |
Correct |
29 ms |
38604 KB |
Output is correct |
54 |
Correct |
35 ms |
69828 KB |
Output is correct |
55 |
Correct |
38 ms |
72772 KB |
Output is correct |
56 |
Correct |
55 ms |
79300 KB |
Output is correct |
57 |
Correct |
4 ms |
4684 KB |
Output is correct |
58 |
Correct |
51 ms |
79308 KB |
Output is correct |
59 |
Correct |
74 ms |
123720 KB |
Output is correct |
60 |
Correct |
71 ms |
123768 KB |
Output is correct |
61 |
Correct |
70 ms |
127028 KB |
Output is correct |
62 |
Correct |
190 ms |
243092 KB |
Output is correct |
63 |
Correct |
446 ms |
246824 KB |
Output is correct |
64 |
Correct |
485 ms |
244380 KB |
Output is correct |
65 |
Correct |
638 ms |
246708 KB |
Output is correct |
66 |
Correct |
911 ms |
246600 KB |
Output is correct |
67 |
Correct |
495 ms |
179988 KB |
Output is correct |