#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
typedef long long ll;
using namespace std;
const int sqrtn = 180, inf = 1e9 + 5;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int k = sqrtn * n;
vector<vector<pair<int, int> > > g(k);
vector<int> b(m), p(m);
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
if (p[i] < sqrtn)
{
g[b[i]].push_back({ p[i] * n + b[i], 0 });
}
else
{
for (int j = b[i] % p[i]; j < n; j += p[i])
{
g[b[i]].push_back({ j, abs(b[i] - j) / p[i] });
}
}
}
for (int step = 1; step < sqrtn; step++) for (int i = 0; i < n; i++)
{
if (i - step >= 0)
{
g[step * n + i].push_back({ step * n + i - step, 1 });
g[step * n + i].push_back({ i - step, 1 });
}
if (i + step < n)
{
g[step * n + i].push_back({ step * n + i + step, 1 });
g[step * n + i].push_back({ i + step, 1 });
}
}
//cout << "\n";
//for (int i = 0; i < k; i++) for (pair<int, int> j : g[i]) cout << i << " " << j.first << " " << j.second << "\n";
priority_queue<pair<int, int> > pq;
pq.push({ 0, b[0] });
vector<int> dist(k, inf);
while (!pq.empty())
{
int u = pq.top().second, d = -pq.top().first;
//cout << u << endl;
pq.pop();
if (dist[u] != inf) continue;
dist[u] = d;
for (pair<int, int> i : g[u]) if (dist[i.first] == inf)
{
pq.push({ -(d + i.second), i.first });
}
}
cout << (dist[b[1]] == inf ? -1 : dist[b[1]]) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1084 KB |
Output is correct |
14 |
Correct |
3 ms |
1100 KB |
Output is correct |
15 |
Correct |
3 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1128 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1100 KB |
Output is correct |
15 |
Correct |
3 ms |
1100 KB |
Output is correct |
16 |
Correct |
5 ms |
2380 KB |
Output is correct |
17 |
Correct |
23 ms |
9724 KB |
Output is correct |
18 |
Correct |
47 ms |
23108 KB |
Output is correct |
19 |
Correct |
54 ms |
26408 KB |
Output is correct |
20 |
Correct |
54 ms |
26500 KB |
Output is correct |
21 |
Correct |
13 ms |
5008 KB |
Output is correct |
22 |
Correct |
48 ms |
23552 KB |
Output is correct |
23 |
Correct |
46 ms |
21124 KB |
Output is correct |
24 |
Correct |
71 ms |
25224 KB |
Output is correct |
25 |
Correct |
54 ms |
26568 KB |
Output is correct |
26 |
Correct |
55 ms |
26436 KB |
Output is correct |
27 |
Correct |
54 ms |
26460 KB |
Output is correct |
28 |
Correct |
58 ms |
26580 KB |
Output is correct |
29 |
Correct |
62 ms |
26524 KB |
Output is correct |
30 |
Correct |
66 ms |
26436 KB |
Output is correct |
31 |
Correct |
57 ms |
26436 KB |
Output is correct |
32 |
Correct |
56 ms |
26440 KB |
Output is correct |
33 |
Correct |
71 ms |
26560 KB |
Output is correct |
34 |
Correct |
73 ms |
26512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
3 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1100 KB |
Output is correct |
15 |
Correct |
3 ms |
1100 KB |
Output is correct |
16 |
Correct |
5 ms |
2380 KB |
Output is correct |
17 |
Correct |
23 ms |
9700 KB |
Output is correct |
18 |
Correct |
49 ms |
23108 KB |
Output is correct |
19 |
Correct |
54 ms |
26476 KB |
Output is correct |
20 |
Correct |
54 ms |
26452 KB |
Output is correct |
21 |
Correct |
10 ms |
5056 KB |
Output is correct |
22 |
Correct |
48 ms |
23544 KB |
Output is correct |
23 |
Correct |
45 ms |
21188 KB |
Output is correct |
24 |
Correct |
54 ms |
25220 KB |
Output is correct |
25 |
Correct |
54 ms |
26484 KB |
Output is correct |
26 |
Correct |
55 ms |
26428 KB |
Output is correct |
27 |
Correct |
54 ms |
26500 KB |
Output is correct |
28 |
Correct |
56 ms |
26496 KB |
Output is correct |
29 |
Correct |
62 ms |
26460 KB |
Output is correct |
30 |
Correct |
57 ms |
26480 KB |
Output is correct |
31 |
Correct |
58 ms |
26504 KB |
Output is correct |
32 |
Correct |
56 ms |
26412 KB |
Output is correct |
33 |
Correct |
72 ms |
26592 KB |
Output is correct |
34 |
Correct |
71 ms |
26608 KB |
Output is correct |
35 |
Correct |
65 ms |
20164 KB |
Output is correct |
36 |
Correct |
33 ms |
14468 KB |
Output is correct |
37 |
Correct |
104 ms |
27080 KB |
Output is correct |
38 |
Correct |
93 ms |
28168 KB |
Output is correct |
39 |
Correct |
115 ms |
28228 KB |
Output is correct |
40 |
Correct |
96 ms |
28204 KB |
Output is correct |
41 |
Correct |
93 ms |
28328 KB |
Output is correct |
42 |
Correct |
62 ms |
27252 KB |
Output is correct |
43 |
Correct |
66 ms |
27576 KB |
Output is correct |
44 |
Correct |
61 ms |
27176 KB |
Output is correct |
45 |
Correct |
143 ms |
30788 KB |
Output is correct |
46 |
Correct |
146 ms |
30760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1100 KB |
Output is correct |
15 |
Correct |
3 ms |
1088 KB |
Output is correct |
16 |
Correct |
4 ms |
2380 KB |
Output is correct |
17 |
Correct |
23 ms |
9820 KB |
Output is correct |
18 |
Correct |
47 ms |
23156 KB |
Output is correct |
19 |
Correct |
54 ms |
26500 KB |
Output is correct |
20 |
Correct |
55 ms |
26520 KB |
Output is correct |
21 |
Correct |
10 ms |
5068 KB |
Output is correct |
22 |
Correct |
48 ms |
23556 KB |
Output is correct |
23 |
Correct |
50 ms |
21096 KB |
Output is correct |
24 |
Correct |
55 ms |
25180 KB |
Output is correct |
25 |
Correct |
55 ms |
26572 KB |
Output is correct |
26 |
Correct |
55 ms |
26532 KB |
Output is correct |
27 |
Correct |
57 ms |
26484 KB |
Output is correct |
28 |
Correct |
56 ms |
26608 KB |
Output is correct |
29 |
Correct |
64 ms |
26432 KB |
Output is correct |
30 |
Correct |
56 ms |
26416 KB |
Output is correct |
31 |
Correct |
59 ms |
26504 KB |
Output is correct |
32 |
Correct |
56 ms |
26396 KB |
Output is correct |
33 |
Correct |
71 ms |
26636 KB |
Output is correct |
34 |
Correct |
70 ms |
26568 KB |
Output is correct |
35 |
Correct |
65 ms |
20120 KB |
Output is correct |
36 |
Correct |
33 ms |
14512 KB |
Output is correct |
37 |
Correct |
93 ms |
27072 KB |
Output is correct |
38 |
Correct |
107 ms |
28220 KB |
Output is correct |
39 |
Correct |
100 ms |
28256 KB |
Output is correct |
40 |
Correct |
94 ms |
28232 KB |
Output is correct |
41 |
Correct |
107 ms |
28240 KB |
Output is correct |
42 |
Correct |
60 ms |
27052 KB |
Output is correct |
43 |
Correct |
61 ms |
27608 KB |
Output is correct |
44 |
Correct |
60 ms |
27160 KB |
Output is correct |
45 |
Correct |
145 ms |
30704 KB |
Output is correct |
46 |
Correct |
141 ms |
30680 KB |
Output is correct |
47 |
Correct |
530 ms |
170916 KB |
Output is correct |
48 |
Runtime error |
478 ms |
262148 KB |
Execution killed with signal 9 |
49 |
Halted |
0 ms |
0 KB |
- |