#include <bits/stdc++.h>
using namespace std;
const int N = (int)3e4, M = (int)3e4;
int n, m;
array<int, 2> doge[M];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
set<int> P;
vector<int> ind;
for(int i = 0;i < m;i ++) {
int b, p;
cin >> b >> p;
P.insert(p);
doge[i] = {b, p};
}
for(auto& e: P) ind.push_back(e);
auto id = [&](int x) {
return lower_bound(ind.begin(), ind.end(), x) - ind.begin();
};
vector<set<int>> pos(n);
for(int i = 0;i < m;i ++) {
pos[doge[i][0]].insert(id(doge[i][1]));
}
vector<vector<pair<int, int>>> adj(n);
set<array<int, 2>> same;
for(int i = 0;i < m;i ++) {
if(same.find(doge[i]) != same.end()) continue;
same.insert(doge[i]);
int it = doge[i][0]+doge[i][1];
int cnt = 1;
while(it < n) {
adj[doge[i][0]].emplace_back(it, cnt);
if(pos[it].find(id(doge[i][1])) != pos[it].end()) break;
it += doge[i][1];
cnt ++;
}
it = doge[i][0]-doge[i][1];
cnt = 1;
while(it >= 0) {
adj[doge[i][0]].emplace_back(it, cnt);
if(pos[it].find(id(doge[i][1])) != pos[it].end()) break;
it -= doge[i][1];
cnt ++;
}
}
vector<int> dist(n, INT_MAX);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, doge[0][0]});
dist[doge[0][0]] = 0;
while((int)pq.size()) {
auto X = pq.top();
pq.pop();
int d, x; tie(d, x) = X;
if(d != dist[x]) continue;
for(auto E : adj[x]) {
int e, w;tie(e, w) = E;
if(dist[e] > dist[x] + w) {
dist[e] = dist[x] + w;
pq.push({dist[e], e});
}
}
}
cout << (dist[doge[1][0]] == INT_MAX ? -1 : dist[doge[1][0]]) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
716 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
844 KB |
Output is correct |
25 |
Correct |
3 ms |
844 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
3 ms |
844 KB |
Output is correct |
29 |
Correct |
4 ms |
1356 KB |
Output is correct |
30 |
Correct |
2 ms |
716 KB |
Output is correct |
31 |
Correct |
2 ms |
972 KB |
Output is correct |
32 |
Correct |
2 ms |
844 KB |
Output is correct |
33 |
Correct |
6 ms |
2124 KB |
Output is correct |
34 |
Correct |
8 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
716 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
844 KB |
Output is correct |
25 |
Correct |
3 ms |
844 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
3 ms |
896 KB |
Output is correct |
29 |
Correct |
4 ms |
1356 KB |
Output is correct |
30 |
Correct |
2 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
972 KB |
Output is correct |
32 |
Correct |
2 ms |
844 KB |
Output is correct |
33 |
Correct |
7 ms |
2124 KB |
Output is correct |
34 |
Correct |
7 ms |
2152 KB |
Output is correct |
35 |
Correct |
52 ms |
4292 KB |
Output is correct |
36 |
Correct |
6 ms |
1128 KB |
Output is correct |
37 |
Correct |
42 ms |
4712 KB |
Output is correct |
38 |
Correct |
54 ms |
5592 KB |
Output is correct |
39 |
Correct |
56 ms |
5616 KB |
Output is correct |
40 |
Correct |
56 ms |
5572 KB |
Output is correct |
41 |
Correct |
58 ms |
5540 KB |
Output is correct |
42 |
Correct |
8 ms |
972 KB |
Output is correct |
43 |
Correct |
8 ms |
972 KB |
Output is correct |
44 |
Correct |
8 ms |
1100 KB |
Output is correct |
45 |
Correct |
50 ms |
8332 KB |
Output is correct |
46 |
Correct |
51 ms |
8268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
4 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
716 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
844 KB |
Output is correct |
25 |
Correct |
3 ms |
844 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
3 ms |
844 KB |
Output is correct |
29 |
Correct |
4 ms |
1356 KB |
Output is correct |
30 |
Correct |
2 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
972 KB |
Output is correct |
32 |
Correct |
3 ms |
844 KB |
Output is correct |
33 |
Correct |
6 ms |
2124 KB |
Output is correct |
34 |
Correct |
7 ms |
2088 KB |
Output is correct |
35 |
Correct |
40 ms |
4368 KB |
Output is correct |
36 |
Correct |
6 ms |
1212 KB |
Output is correct |
37 |
Correct |
54 ms |
4668 KB |
Output is correct |
38 |
Correct |
53 ms |
5548 KB |
Output is correct |
39 |
Correct |
55 ms |
5664 KB |
Output is correct |
40 |
Correct |
55 ms |
5640 KB |
Output is correct |
41 |
Correct |
54 ms |
5592 KB |
Output is correct |
42 |
Correct |
8 ms |
972 KB |
Output is correct |
43 |
Correct |
9 ms |
972 KB |
Output is correct |
44 |
Correct |
8 ms |
1100 KB |
Output is correct |
45 |
Correct |
52 ms |
8248 KB |
Output is correct |
46 |
Correct |
50 ms |
8284 KB |
Output is correct |
47 |
Correct |
102 ms |
13112 KB |
Output is correct |
48 |
Correct |
45 ms |
7596 KB |
Output is correct |
49 |
Correct |
37 ms |
6860 KB |
Output is correct |
50 |
Correct |
29 ms |
6048 KB |
Output is correct |
51 |
Correct |
82 ms |
10684 KB |
Output is correct |
52 |
Correct |
89 ms |
11216 KB |
Output is correct |
53 |
Correct |
41 ms |
7536 KB |
Output is correct |
54 |
Correct |
3 ms |
3020 KB |
Output is correct |
55 |
Correct |
4 ms |
3464 KB |
Output is correct |
56 |
Correct |
31 ms |
6732 KB |
Output is correct |
57 |
Correct |
41 ms |
11592 KB |
Output is correct |
58 |
Correct |
11 ms |
3976 KB |
Output is correct |
59 |
Correct |
13 ms |
4308 KB |
Output is correct |
60 |
Correct |
15 ms |
5000 KB |
Output is correct |
61 |
Correct |
22 ms |
4760 KB |
Output is correct |
62 |
Correct |
91 ms |
11020 KB |
Output is correct |
63 |
Correct |
132 ms |
30484 KB |
Output is correct |
64 |
Correct |
153 ms |
39356 KB |
Output is correct |
65 |
Correct |
172 ms |
51640 KB |
Output is correct |
66 |
Correct |
243 ms |
80592 KB |
Output is correct |
67 |
Correct |
259 ms |
79784 KB |
Output is correct |