#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define mk make_pair
#define pii pair<int, int>
#define F first
#define S second
#define sz size()
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
const int N = 3e4 + 5;
int b[N], p[N], d[N];
set <pii> st;
int n;
void dij ()
{
int v = st.begin () -> S;
st.erase (st.begin ());
for (int i = 0; i < n; i++)
if (b[v] % p[v] == b[i] % p[v] && d[v] + abs (b[v] - b[i]) / p[v] < d[i])
{
st.erase (mk (d[i], i));
d[i] = d[v] + abs (b[v] - b[i]) / p[v];
st.insert (mk (d[i], i));
}
if (st.sz)
dij ();
}
void Solve ()
{
int m;
cin >> m >> n;
for (int i = 0; i < n; i++)
cin >> b[i] >> p[i];
memset (d, 63, sizeof d);
d[0] = 0;
st.insert (mk (d[0], 0));
dij ();
if (d[1] == d[N - 1])
cout << -1 << endl;
else
cout << d[1] << endl;
}
int32_t main ()
{
ios::sync_with_stdio (0), cin.tie (0), cout.tie (0);
int tt = 1;
// cin >> tt;
while (tt--)
Solve ();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
46 ms |
700 KB |
Output is correct |
12 |
Correct |
65 ms |
712 KB |
Output is correct |
13 |
Correct |
64 ms |
596 KB |
Output is correct |
14 |
Correct |
42 ms |
596 KB |
Output is correct |
15 |
Correct |
42 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
42 ms |
596 KB |
Output is correct |
12 |
Correct |
65 ms |
596 KB |
Output is correct |
13 |
Correct |
69 ms |
700 KB |
Output is correct |
14 |
Correct |
42 ms |
596 KB |
Output is correct |
15 |
Correct |
44 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
34 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
76 ms |
704 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
9 ms |
596 KB |
Output is correct |
24 |
Correct |
37 ms |
596 KB |
Output is correct |
25 |
Correct |
40 ms |
596 KB |
Output is correct |
26 |
Correct |
73 ms |
596 KB |
Output is correct |
27 |
Correct |
68 ms |
692 KB |
Output is correct |
28 |
Correct |
41 ms |
596 KB |
Output is correct |
29 |
Correct |
8 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
43 ms |
704 KB |
Output is correct |
34 |
Correct |
46 ms |
696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
44 ms |
696 KB |
Output is correct |
12 |
Correct |
70 ms |
700 KB |
Output is correct |
13 |
Correct |
68 ms |
700 KB |
Output is correct |
14 |
Correct |
44 ms |
700 KB |
Output is correct |
15 |
Correct |
47 ms |
700 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
36 ms |
684 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
80 ms |
704 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
10 ms |
596 KB |
Output is correct |
24 |
Correct |
37 ms |
648 KB |
Output is correct |
25 |
Correct |
40 ms |
684 KB |
Output is correct |
26 |
Correct |
69 ms |
596 KB |
Output is correct |
27 |
Correct |
68 ms |
596 KB |
Output is correct |
28 |
Correct |
40 ms |
596 KB |
Output is correct |
29 |
Correct |
6 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
42 ms |
596 KB |
Output is correct |
34 |
Correct |
44 ms |
596 KB |
Output is correct |
35 |
Execution timed out |
1091 ms |
2460 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
516 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
476 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
42 ms |
596 KB |
Output is correct |
12 |
Correct |
64 ms |
596 KB |
Output is correct |
13 |
Correct |
65 ms |
696 KB |
Output is correct |
14 |
Correct |
43 ms |
696 KB |
Output is correct |
15 |
Correct |
41 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
33 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
73 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
9 ms |
596 KB |
Output is correct |
24 |
Correct |
36 ms |
628 KB |
Output is correct |
25 |
Correct |
40 ms |
680 KB |
Output is correct |
26 |
Correct |
68 ms |
700 KB |
Output is correct |
27 |
Correct |
67 ms |
696 KB |
Output is correct |
28 |
Correct |
41 ms |
468 KB |
Output is correct |
29 |
Correct |
7 ms |
624 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
42 ms |
704 KB |
Output is correct |
34 |
Correct |
41 ms |
596 KB |
Output is correct |
35 |
Execution timed out |
1097 ms |
2432 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |