#include <bits/stdc++.h>
#define pii pair<int, int>
#define mp make_pair
#define F first
#define S second
#define PB push_back
#define N 30005
#define MAGIC 205
#define maxc 1000000007
using namespace std;
int n, m, b[N], p[N];
vector<int> vec[N];
int d[N][MAGIC];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("INP.TXT", "r", stdin);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
vec[b[i]].PB(p[i]);
}
priority_queue<pair<int, pii> >q;
q.push(mp(0, mp(b[0], 0)));
memset(d, 60, sizeof d);
d[b[0]][0] = 0;
while (!q.empty())
{
auto z = q.top(); q.pop();
int l = -z.F;
int x = z.S.F;
int w = z.S.S;
if (d[x][w] < l) continue;
if (x == b[1])
{
cout <<l;
return 0;
}
if (w)
{
if (d[x][0] > l) {
d[x][0] = l; q.push(mp(-l, mp(x, 0)));
}
if (x + w < n && d[x + w][w] > l + 1) {
d[x + w][w] = l+1; q.push(mp(-l-1, mp(x+w, w)));
}
if (x - w >= 0 && d[x - w][w] > l + 1) {
d[x - w][w] = l+1; q.push(mp(-l-1, mp(x-w, w)));
}
}
else
{
for (auto ww : vec[x])
{
if (ww >= MAGIC)
{
for (int y = x % ww; y < n; y += ww)
if (d[y][0] > d[x][w] + abs(x - y)/ww)
{
d[y][0] = d[x][w] + abs(x - y)/ww;
q.push(mp(-d[y][0], mp(y, 0)));
}
}
else
{
if (d[x][ww] > l)
{
d[x][ww] = l;
q.push(mp(-d[x][ww], mp(x, ww)));
}
}
}
}
}
cout <<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
25080 KB |
Output is correct |
2 |
Correct |
21 ms |
25192 KB |
Output is correct |
3 |
Correct |
20 ms |
25248 KB |
Output is correct |
4 |
Correct |
20 ms |
25300 KB |
Output is correct |
5 |
Correct |
21 ms |
25300 KB |
Output is correct |
6 |
Correct |
21 ms |
25308 KB |
Output is correct |
7 |
Correct |
20 ms |
25308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
25308 KB |
Output is correct |
2 |
Correct |
20 ms |
25356 KB |
Output is correct |
3 |
Correct |
21 ms |
25356 KB |
Output is correct |
4 |
Correct |
21 ms |
25356 KB |
Output is correct |
5 |
Correct |
20 ms |
25416 KB |
Output is correct |
6 |
Correct |
20 ms |
25416 KB |
Output is correct |
7 |
Correct |
20 ms |
25472 KB |
Output is correct |
8 |
Correct |
20 ms |
25516 KB |
Output is correct |
9 |
Correct |
21 ms |
25516 KB |
Output is correct |
10 |
Correct |
20 ms |
25516 KB |
Output is correct |
11 |
Correct |
25 ms |
25588 KB |
Output is correct |
12 |
Correct |
21 ms |
25596 KB |
Output is correct |
13 |
Correct |
23 ms |
25624 KB |
Output is correct |
14 |
Correct |
22 ms |
25756 KB |
Output is correct |
15 |
Correct |
20 ms |
25756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
25756 KB |
Output is correct |
2 |
Correct |
45 ms |
25756 KB |
Output is correct |
3 |
Correct |
19 ms |
25756 KB |
Output is correct |
4 |
Correct |
20 ms |
25756 KB |
Output is correct |
5 |
Correct |
20 ms |
25756 KB |
Output is correct |
6 |
Correct |
20 ms |
25756 KB |
Output is correct |
7 |
Correct |
20 ms |
25756 KB |
Output is correct |
8 |
Correct |
21 ms |
25756 KB |
Output is correct |
9 |
Correct |
22 ms |
25756 KB |
Output is correct |
10 |
Correct |
20 ms |
25756 KB |
Output is correct |
11 |
Correct |
20 ms |
25756 KB |
Output is correct |
12 |
Correct |
20 ms |
25756 KB |
Output is correct |
13 |
Correct |
20 ms |
25756 KB |
Output is correct |
14 |
Correct |
21 ms |
25772 KB |
Output is correct |
15 |
Correct |
21 ms |
25800 KB |
Output is correct |
16 |
Correct |
20 ms |
25800 KB |
Output is correct |
17 |
Correct |
21 ms |
25828 KB |
Output is correct |
18 |
Correct |
21 ms |
25828 KB |
Output is correct |
19 |
Correct |
20 ms |
25828 KB |
Output is correct |
20 |
Correct |
21 ms |
25828 KB |
Output is correct |
21 |
Correct |
20 ms |
25828 KB |
Output is correct |
22 |
Correct |
21 ms |
25828 KB |
Output is correct |
23 |
Correct |
25 ms |
25892 KB |
Output is correct |
24 |
Correct |
24 ms |
25904 KB |
Output is correct |
25 |
Correct |
20 ms |
25924 KB |
Output is correct |
26 |
Correct |
21 ms |
25944 KB |
Output is correct |
27 |
Correct |
20 ms |
25960 KB |
Output is correct |
28 |
Correct |
23 ms |
25976 KB |
Output is correct |
29 |
Correct |
28 ms |
25996 KB |
Output is correct |
30 |
Correct |
22 ms |
25996 KB |
Output is correct |
31 |
Correct |
25 ms |
26000 KB |
Output is correct |
32 |
Correct |
22 ms |
26012 KB |
Output is correct |
33 |
Correct |
35 ms |
26020 KB |
Output is correct |
34 |
Correct |
31 ms |
26032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
26032 KB |
Output is correct |
2 |
Correct |
22 ms |
26032 KB |
Output is correct |
3 |
Correct |
21 ms |
26032 KB |
Output is correct |
4 |
Correct |
20 ms |
26032 KB |
Output is correct |
5 |
Correct |
20 ms |
26032 KB |
Output is correct |
6 |
Correct |
20 ms |
26032 KB |
Output is correct |
7 |
Correct |
20 ms |
26032 KB |
Output is correct |
8 |
Correct |
20 ms |
26032 KB |
Output is correct |
9 |
Correct |
20 ms |
26032 KB |
Output is correct |
10 |
Correct |
22 ms |
26032 KB |
Output is correct |
11 |
Correct |
20 ms |
26068 KB |
Output is correct |
12 |
Correct |
22 ms |
26068 KB |
Output is correct |
13 |
Correct |
22 ms |
26092 KB |
Output is correct |
14 |
Correct |
21 ms |
26104 KB |
Output is correct |
15 |
Correct |
22 ms |
26104 KB |
Output is correct |
16 |
Correct |
20 ms |
26104 KB |
Output is correct |
17 |
Correct |
21 ms |
26120 KB |
Output is correct |
18 |
Correct |
20 ms |
26152 KB |
Output is correct |
19 |
Correct |
20 ms |
26152 KB |
Output is correct |
20 |
Correct |
21 ms |
26172 KB |
Output is correct |
21 |
Correct |
20 ms |
26188 KB |
Output is correct |
22 |
Correct |
23 ms |
26200 KB |
Output is correct |
23 |
Correct |
22 ms |
26208 KB |
Output is correct |
24 |
Correct |
24 ms |
26268 KB |
Output is correct |
25 |
Correct |
22 ms |
26268 KB |
Output is correct |
26 |
Correct |
22 ms |
26268 KB |
Output is correct |
27 |
Correct |
21 ms |
26276 KB |
Output is correct |
28 |
Correct |
23 ms |
26420 KB |
Output is correct |
29 |
Correct |
28 ms |
26420 KB |
Output is correct |
30 |
Correct |
23 ms |
26420 KB |
Output is correct |
31 |
Correct |
24 ms |
26420 KB |
Output is correct |
32 |
Correct |
25 ms |
26420 KB |
Output is correct |
33 |
Correct |
36 ms |
26420 KB |
Output is correct |
34 |
Correct |
30 ms |
26420 KB |
Output is correct |
35 |
Correct |
28 ms |
26996 KB |
Output is correct |
36 |
Correct |
22 ms |
26996 KB |
Output is correct |
37 |
Correct |
26 ms |
27128 KB |
Output is correct |
38 |
Correct |
31 ms |
27556 KB |
Output is correct |
39 |
Correct |
28 ms |
27680 KB |
Output is correct |
40 |
Correct |
35 ms |
28072 KB |
Output is correct |
41 |
Correct |
32 ms |
28336 KB |
Output is correct |
42 |
Correct |
25 ms |
28508 KB |
Output is correct |
43 |
Correct |
25 ms |
28716 KB |
Output is correct |
44 |
Correct |
26 ms |
28892 KB |
Output is correct |
45 |
Correct |
96 ms |
29552 KB |
Output is correct |
46 |
Correct |
58 ms |
29760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
29760 KB |
Output is correct |
2 |
Correct |
21 ms |
29760 KB |
Output is correct |
3 |
Correct |
20 ms |
29760 KB |
Output is correct |
4 |
Correct |
20 ms |
29760 KB |
Output is correct |
5 |
Correct |
21 ms |
29760 KB |
Output is correct |
6 |
Correct |
25 ms |
29760 KB |
Output is correct |
7 |
Correct |
21 ms |
29760 KB |
Output is correct |
8 |
Correct |
21 ms |
29760 KB |
Output is correct |
9 |
Correct |
22 ms |
29760 KB |
Output is correct |
10 |
Correct |
20 ms |
29760 KB |
Output is correct |
11 |
Correct |
20 ms |
29760 KB |
Output is correct |
12 |
Correct |
23 ms |
29760 KB |
Output is correct |
13 |
Correct |
21 ms |
29760 KB |
Output is correct |
14 |
Correct |
21 ms |
29760 KB |
Output is correct |
15 |
Correct |
21 ms |
29760 KB |
Output is correct |
16 |
Correct |
20 ms |
29760 KB |
Output is correct |
17 |
Correct |
21 ms |
29760 KB |
Output is correct |
18 |
Correct |
22 ms |
29760 KB |
Output is correct |
19 |
Correct |
21 ms |
29760 KB |
Output is correct |
20 |
Correct |
21 ms |
29760 KB |
Output is correct |
21 |
Correct |
24 ms |
29760 KB |
Output is correct |
22 |
Correct |
24 ms |
29760 KB |
Output is correct |
23 |
Correct |
20 ms |
29760 KB |
Output is correct |
24 |
Correct |
27 ms |
29760 KB |
Output is correct |
25 |
Correct |
21 ms |
29760 KB |
Output is correct |
26 |
Correct |
22 ms |
29760 KB |
Output is correct |
27 |
Correct |
22 ms |
29760 KB |
Output is correct |
28 |
Correct |
25 ms |
29760 KB |
Output is correct |
29 |
Correct |
28 ms |
29760 KB |
Output is correct |
30 |
Correct |
23 ms |
29760 KB |
Output is correct |
31 |
Correct |
25 ms |
29760 KB |
Output is correct |
32 |
Correct |
23 ms |
29760 KB |
Output is correct |
33 |
Correct |
37 ms |
29760 KB |
Output is correct |
34 |
Correct |
33 ms |
29760 KB |
Output is correct |
35 |
Correct |
32 ms |
29892 KB |
Output is correct |
36 |
Correct |
23 ms |
29892 KB |
Output is correct |
37 |
Correct |
29 ms |
30152 KB |
Output is correct |
38 |
Correct |
36 ms |
30520 KB |
Output is correct |
39 |
Correct |
27 ms |
30576 KB |
Output is correct |
40 |
Correct |
31 ms |
30936 KB |
Output is correct |
41 |
Correct |
30 ms |
31228 KB |
Output is correct |
42 |
Correct |
25 ms |
31368 KB |
Output is correct |
43 |
Correct |
30 ms |
31688 KB |
Output is correct |
44 |
Correct |
27 ms |
31736 KB |
Output is correct |
45 |
Correct |
97 ms |
32568 KB |
Output is correct |
46 |
Correct |
66 ms |
32652 KB |
Output is correct |
47 |
Correct |
34 ms |
32984 KB |
Output is correct |
48 |
Correct |
30 ms |
32984 KB |
Output is correct |
49 |
Correct |
27 ms |
32984 KB |
Output is correct |
50 |
Correct |
25 ms |
33020 KB |
Output is correct |
51 |
Correct |
41 ms |
34264 KB |
Output is correct |
52 |
Correct |
46 ms |
34596 KB |
Output is correct |
53 |
Correct |
31 ms |
34596 KB |
Output is correct |
54 |
Correct |
23 ms |
34596 KB |
Output is correct |
55 |
Correct |
26 ms |
34596 KB |
Output is correct |
56 |
Correct |
44 ms |
34960 KB |
Output is correct |
57 |
Correct |
21 ms |
34960 KB |
Output is correct |
58 |
Correct |
34 ms |
34960 KB |
Output is correct |
59 |
Correct |
34 ms |
34960 KB |
Output is correct |
60 |
Correct |
32 ms |
34960 KB |
Output is correct |
61 |
Correct |
34 ms |
35148 KB |
Output is correct |
62 |
Correct |
75 ms |
36164 KB |
Output is correct |
63 |
Correct |
75 ms |
36632 KB |
Output is correct |
64 |
Correct |
90 ms |
36860 KB |
Output is correct |
65 |
Correct |
480 ms |
37092 KB |
Output is correct |
66 |
Execution timed out |
1087 ms |
37948 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |