#include <bits/stdc++.h>
using namespace std;
const short N = 3e4 + 5;
#define eb emplace_back
struct node {
int w;
short u, s;
node() {
w = u = s = 0;
}
node(int w, short u, short s) : w(w), u(u), s(s) {}
friend bool operator > (node a, node b) {
return a.w > b.w;
}
};
short n, m;
int dist[N][200];
vector<short> adj[N];
priority_queue<node, vector<node>, greater<node>> Q;
short start, finish;
bool vis[N];
void update(short u, short v, int w, short cur, short siu) {
if(dist[v][siu] > dist[u][cur] + w) {
dist[v][siu] = dist[u][cur] + w;
Q.emplace(dist[v][siu], v, siu);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
short SQRT = sqrt(n);
for(short i = 0; i < m; i++) {
short u, v;
cin >> u >> v;
adj[u].eb(v);
if(i == 0)
start = u;
if(i == 1)
finish = u;
}
memset(dist, 0x3f, sizeof dist);
dist[start][0] = 0;
Q.emplace(0, start, 0);
while(!Q.empty()) {
node top = Q.top();
Q.pop();
short u = top.u;
short cur = top.s;
if(dist[u][cur] != top.w)
continue;
if(u == finish)
return cout << dist[finish][cur], 0;
if(cur) {
if(u + cur <= n)
update(u, u + cur, 1, cur, cur);
if(u - cur >= 0)
update(u, u - cur, 1, cur, cur);
}
if(vis[u])
continue;
vis[u] = true;
for(short v : adj[u]) {
if(v >= SQRT) {
for(short j = 1; u + j * v < n; j++)
update(u, u + j * v, j, cur, 0);
for(short j = 1; u - j * v >= 0; j++)
update(u, u - j * v, j, cur, 0);
} else {
if(u + v <= n)
update(u, u + v, 1, cur, v);
if(u - v >= 0)
update(u, u - v, 1, cur, v);
}
}
}
cout << -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24408 KB |
Output is correct |
2 |
Correct |
4 ms |
24412 KB |
Output is correct |
3 |
Correct |
4 ms |
24408 KB |
Output is correct |
4 |
Correct |
4 ms |
24412 KB |
Output is correct |
5 |
Correct |
4 ms |
24412 KB |
Output is correct |
6 |
Correct |
4 ms |
24408 KB |
Output is correct |
7 |
Correct |
4 ms |
24412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24408 KB |
Output is correct |
2 |
Correct |
4 ms |
24664 KB |
Output is correct |
3 |
Correct |
4 ms |
24412 KB |
Output is correct |
4 |
Correct |
4 ms |
24668 KB |
Output is correct |
5 |
Correct |
4 ms |
24408 KB |
Output is correct |
6 |
Correct |
4 ms |
24408 KB |
Output is correct |
7 |
Correct |
4 ms |
24664 KB |
Output is correct |
8 |
Correct |
4 ms |
24408 KB |
Output is correct |
9 |
Correct |
4 ms |
24408 KB |
Output is correct |
10 |
Correct |
4 ms |
24412 KB |
Output is correct |
11 |
Correct |
4 ms |
24664 KB |
Output is correct |
12 |
Correct |
4 ms |
24408 KB |
Output is correct |
13 |
Correct |
4 ms |
24408 KB |
Output is correct |
14 |
Correct |
4 ms |
24664 KB |
Output is correct |
15 |
Correct |
4 ms |
24664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24408 KB |
Output is correct |
2 |
Correct |
4 ms |
24412 KB |
Output is correct |
3 |
Correct |
3 ms |
24408 KB |
Output is correct |
4 |
Correct |
4 ms |
24408 KB |
Output is correct |
5 |
Correct |
3 ms |
24408 KB |
Output is correct |
6 |
Correct |
4 ms |
24664 KB |
Output is correct |
7 |
Correct |
4 ms |
24412 KB |
Output is correct |
8 |
Correct |
4 ms |
24408 KB |
Output is correct |
9 |
Correct |
4 ms |
24412 KB |
Output is correct |
10 |
Correct |
4 ms |
24408 KB |
Output is correct |
11 |
Correct |
4 ms |
24412 KB |
Output is correct |
12 |
Correct |
4 ms |
24664 KB |
Output is correct |
13 |
Correct |
4 ms |
24408 KB |
Output is correct |
14 |
Correct |
4 ms |
24664 KB |
Output is correct |
15 |
Correct |
4 ms |
24920 KB |
Output is correct |
16 |
Correct |
4 ms |
24664 KB |
Output is correct |
17 |
Correct |
4 ms |
24664 KB |
Output is correct |
18 |
Correct |
4 ms |
24664 KB |
Output is correct |
19 |
Correct |
4 ms |
24664 KB |
Output is correct |
20 |
Correct |
5 ms |
24664 KB |
Output is correct |
21 |
Correct |
4 ms |
24408 KB |
Output is correct |
22 |
Correct |
4 ms |
24920 KB |
Output is correct |
23 |
Correct |
4 ms |
24664 KB |
Output is correct |
24 |
Correct |
5 ms |
24668 KB |
Output is correct |
25 |
Correct |
4 ms |
24664 KB |
Output is correct |
26 |
Correct |
4 ms |
24408 KB |
Output is correct |
27 |
Correct |
4 ms |
24412 KB |
Output is correct |
28 |
Correct |
4 ms |
24664 KB |
Output is correct |
29 |
Correct |
7 ms |
24664 KB |
Output is correct |
30 |
Correct |
4 ms |
24408 KB |
Output is correct |
31 |
Correct |
6 ms |
24664 KB |
Output is correct |
32 |
Correct |
5 ms |
24668 KB |
Output is correct |
33 |
Correct |
9 ms |
24664 KB |
Output is correct |
34 |
Correct |
7 ms |
24668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
24412 KB |
Output is correct |
2 |
Correct |
4 ms |
24408 KB |
Output is correct |
3 |
Correct |
4 ms |
24408 KB |
Output is correct |
4 |
Correct |
3 ms |
24408 KB |
Output is correct |
5 |
Correct |
4 ms |
24408 KB |
Output is correct |
6 |
Correct |
4 ms |
24408 KB |
Output is correct |
7 |
Correct |
3 ms |
24408 KB |
Output is correct |
8 |
Correct |
4 ms |
24412 KB |
Output is correct |
9 |
Correct |
4 ms |
24408 KB |
Output is correct |
10 |
Correct |
5 ms |
24412 KB |
Output is correct |
11 |
Correct |
4 ms |
24664 KB |
Output is correct |
12 |
Correct |
4 ms |
24664 KB |
Output is correct |
13 |
Correct |
4 ms |
24408 KB |
Output is correct |
14 |
Correct |
4 ms |
24664 KB |
Output is correct |
15 |
Correct |
4 ms |
24484 KB |
Output is correct |
16 |
Correct |
4 ms |
24668 KB |
Output is correct |
17 |
Correct |
4 ms |
24668 KB |
Output is correct |
18 |
Correct |
4 ms |
24668 KB |
Output is correct |
19 |
Correct |
4 ms |
24668 KB |
Output is correct |
20 |
Correct |
4 ms |
24720 KB |
Output is correct |
21 |
Correct |
4 ms |
24868 KB |
Output is correct |
22 |
Correct |
4 ms |
24668 KB |
Output is correct |
23 |
Correct |
4 ms |
24664 KB |
Output is correct |
24 |
Correct |
5 ms |
24668 KB |
Output is correct |
25 |
Correct |
4 ms |
24664 KB |
Output is correct |
26 |
Correct |
4 ms |
24668 KB |
Output is correct |
27 |
Correct |
4 ms |
24920 KB |
Output is correct |
28 |
Correct |
5 ms |
24664 KB |
Output is correct |
29 |
Correct |
7 ms |
24664 KB |
Output is correct |
30 |
Correct |
4 ms |
24412 KB |
Output is correct |
31 |
Correct |
6 ms |
24664 KB |
Output is correct |
32 |
Correct |
4 ms |
24664 KB |
Output is correct |
33 |
Correct |
10 ms |
24664 KB |
Output is correct |
34 |
Correct |
6 ms |
24664 KB |
Output is correct |
35 |
Correct |
7 ms |
24664 KB |
Output is correct |
36 |
Correct |
5 ms |
24668 KB |
Output is correct |
37 |
Correct |
7 ms |
24664 KB |
Output is correct |
38 |
Correct |
8 ms |
24668 KB |
Output is correct |
39 |
Correct |
7 ms |
24664 KB |
Output is correct |
40 |
Correct |
8 ms |
24664 KB |
Output is correct |
41 |
Correct |
9 ms |
24664 KB |
Output is correct |
42 |
Correct |
7 ms |
24664 KB |
Output is correct |
43 |
Correct |
6 ms |
24664 KB |
Output is correct |
44 |
Correct |
7 ms |
24664 KB |
Output is correct |
45 |
Correct |
13 ms |
24664 KB |
Output is correct |
46 |
Correct |
10 ms |
24668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24408 KB |
Output is correct |
2 |
Correct |
4 ms |
24408 KB |
Output is correct |
3 |
Correct |
3 ms |
24408 KB |
Output is correct |
4 |
Correct |
4 ms |
24408 KB |
Output is correct |
5 |
Correct |
4 ms |
24408 KB |
Output is correct |
6 |
Correct |
4 ms |
24408 KB |
Output is correct |
7 |
Correct |
4 ms |
24412 KB |
Output is correct |
8 |
Correct |
4 ms |
24612 KB |
Output is correct |
9 |
Correct |
4 ms |
24408 KB |
Output is correct |
10 |
Correct |
4 ms |
24412 KB |
Output is correct |
11 |
Correct |
4 ms |
24664 KB |
Output is correct |
12 |
Correct |
4 ms |
24668 KB |
Output is correct |
13 |
Correct |
4 ms |
24408 KB |
Output is correct |
14 |
Correct |
4 ms |
24408 KB |
Output is correct |
15 |
Correct |
4 ms |
24664 KB |
Output is correct |
16 |
Correct |
4 ms |
24412 KB |
Output is correct |
17 |
Correct |
4 ms |
24664 KB |
Output is correct |
18 |
Correct |
4 ms |
24676 KB |
Output is correct |
19 |
Correct |
4 ms |
24460 KB |
Output is correct |
20 |
Correct |
4 ms |
24664 KB |
Output is correct |
21 |
Correct |
4 ms |
24664 KB |
Output is correct |
22 |
Correct |
4 ms |
24664 KB |
Output is correct |
23 |
Correct |
4 ms |
24668 KB |
Output is correct |
24 |
Correct |
5 ms |
24668 KB |
Output is correct |
25 |
Correct |
4 ms |
24664 KB |
Output is correct |
26 |
Correct |
4 ms |
24920 KB |
Output is correct |
27 |
Correct |
4 ms |
24408 KB |
Output is correct |
28 |
Correct |
4 ms |
24664 KB |
Output is correct |
29 |
Correct |
7 ms |
24664 KB |
Output is correct |
30 |
Correct |
4 ms |
24408 KB |
Output is correct |
31 |
Correct |
6 ms |
24664 KB |
Output is correct |
32 |
Correct |
5 ms |
24668 KB |
Output is correct |
33 |
Correct |
8 ms |
24920 KB |
Output is correct |
34 |
Correct |
6 ms |
24664 KB |
Output is correct |
35 |
Correct |
7 ms |
24664 KB |
Output is correct |
36 |
Correct |
4 ms |
24668 KB |
Output is correct |
37 |
Correct |
7 ms |
24664 KB |
Output is correct |
38 |
Correct |
8 ms |
24664 KB |
Output is correct |
39 |
Correct |
7 ms |
24664 KB |
Output is correct |
40 |
Correct |
8 ms |
24668 KB |
Output is correct |
41 |
Correct |
8 ms |
24664 KB |
Output is correct |
42 |
Correct |
7 ms |
24664 KB |
Output is correct |
43 |
Correct |
6 ms |
24664 KB |
Output is correct |
44 |
Correct |
7 ms |
24668 KB |
Output is correct |
45 |
Correct |
13 ms |
24664 KB |
Output is correct |
46 |
Correct |
9 ms |
24664 KB |
Output is correct |
47 |
Correct |
10 ms |
25300 KB |
Output is correct |
48 |
Correct |
7 ms |
24920 KB |
Output is correct |
49 |
Correct |
9 ms |
24920 KB |
Output is correct |
50 |
Correct |
6 ms |
24920 KB |
Output is correct |
51 |
Correct |
13 ms |
25556 KB |
Output is correct |
52 |
Correct |
14 ms |
25648 KB |
Output is correct |
53 |
Correct |
9 ms |
25176 KB |
Output is correct |
54 |
Correct |
4 ms |
24664 KB |
Output is correct |
55 |
Correct |
4 ms |
24664 KB |
Output is correct |
56 |
Correct |
10 ms |
25432 KB |
Output is correct |
57 |
Correct |
4 ms |
24664 KB |
Output is correct |
58 |
Correct |
8 ms |
24664 KB |
Output is correct |
59 |
Correct |
8 ms |
24664 KB |
Output is correct |
60 |
Correct |
8 ms |
24668 KB |
Output is correct |
61 |
Correct |
8 ms |
24664 KB |
Output is correct |
62 |
Correct |
20 ms |
25436 KB |
Output is correct |
63 |
Correct |
27 ms |
25812 KB |
Output is correct |
64 |
Correct |
31 ms |
25812 KB |
Output is correct |
65 |
Correct |
119 ms |
25816 KB |
Output is correct |
66 |
Correct |
367 ms |
26836 KB |
Output is correct |
67 |
Correct |
174 ms |
26832 KB |
Output is correct |