#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int nax = 30005;
const int INF = 1e9 + 5;
vector<pii> adj[nax];
vector<int> dist(nax, INF);
set<int> used[nax];
set<int> all[nax];
struct Edge {
int to, w;
bool operator<(const Edge& other) const {
return w > other.w;
}
};
priority_queue<Edge> q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<pii> a(m);
for(auto& i : a) {
cin >> i.first >> i.second;
}
for(auto i : a) {
all[i.first].insert(i.second);
}
for(auto i : a) {
if (used[i.first].count(i.second)) continue;
used[i.first].insert(i.second);
int idx = i.first + i.second;
for(int cost = 1;;cost++) {
if (idx >= n) break;
adj[i.first].push_back({idx, cost});
if (all[idx].count(i.second)) break;
// cout << i.first << "->" << idx << " " << cost << "\n";
idx += i.second;
}
idx = i.first - i.second;
for(int cost = 1;;cost++) {
if (idx < 0) break;
adj[i.first].push_back({idx, cost});
if (all[idx].count(i.second)) break;
// cout << i.first << "->" << idx << " " << cost << "\n";
idx -= i.second;
}
}
q.push({a[0].first, 0});
dist[a[0].first] = 0;
while(!q.empty()) {
Edge v = q.top();
q.pop();
if (dist[v.to] < v.w) continue;
for(pii u : adj[v.to]) {
if (dist[u.first] > u.second + v.w) {
dist[u.first] = u.second + v.w;
q.push({u.first, dist[u.first]});
}
}
}
cout << (dist[a[1].first] == INF ? -1 : dist[a[1].first]) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
4 |
Correct |
2 ms |
3916 KB |
Output is correct |
5 |
Correct |
3 ms |
3916 KB |
Output is correct |
6 |
Correct |
2 ms |
3916 KB |
Output is correct |
7 |
Correct |
2 ms |
3916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3916 KB |
Output is correct |
2 |
Correct |
3 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
4 |
Correct |
3 ms |
3916 KB |
Output is correct |
5 |
Correct |
3 ms |
3916 KB |
Output is correct |
6 |
Correct |
2 ms |
3916 KB |
Output is correct |
7 |
Correct |
2 ms |
3916 KB |
Output is correct |
8 |
Correct |
3 ms |
3916 KB |
Output is correct |
9 |
Correct |
2 ms |
3916 KB |
Output is correct |
10 |
Correct |
3 ms |
3916 KB |
Output is correct |
11 |
Correct |
4 ms |
4172 KB |
Output is correct |
12 |
Correct |
3 ms |
3916 KB |
Output is correct |
13 |
Correct |
3 ms |
3916 KB |
Output is correct |
14 |
Correct |
5 ms |
4172 KB |
Output is correct |
15 |
Correct |
4 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
4 |
Correct |
2 ms |
3916 KB |
Output is correct |
5 |
Correct |
2 ms |
3916 KB |
Output is correct |
6 |
Correct |
3 ms |
3916 KB |
Output is correct |
7 |
Correct |
3 ms |
3916 KB |
Output is correct |
8 |
Correct |
3 ms |
3916 KB |
Output is correct |
9 |
Correct |
2 ms |
3916 KB |
Output is correct |
10 |
Correct |
4 ms |
3916 KB |
Output is correct |
11 |
Correct |
5 ms |
4172 KB |
Output is correct |
12 |
Correct |
3 ms |
3916 KB |
Output is correct |
13 |
Correct |
3 ms |
3916 KB |
Output is correct |
14 |
Correct |
4 ms |
4172 KB |
Output is correct |
15 |
Correct |
4 ms |
4172 KB |
Output is correct |
16 |
Correct |
3 ms |
4060 KB |
Output is correct |
17 |
Correct |
4 ms |
4300 KB |
Output is correct |
18 |
Correct |
3 ms |
4044 KB |
Output is correct |
19 |
Correct |
3 ms |
4044 KB |
Output is correct |
20 |
Correct |
5 ms |
4172 KB |
Output is correct |
21 |
Correct |
3 ms |
4044 KB |
Output is correct |
22 |
Correct |
3 ms |
4044 KB |
Output is correct |
23 |
Correct |
3 ms |
4044 KB |
Output is correct |
24 |
Correct |
4 ms |
4300 KB |
Output is correct |
25 |
Correct |
5 ms |
4300 KB |
Output is correct |
26 |
Correct |
3 ms |
4044 KB |
Output is correct |
27 |
Correct |
3 ms |
4044 KB |
Output is correct |
28 |
Correct |
4 ms |
4300 KB |
Output is correct |
29 |
Correct |
5 ms |
4812 KB |
Output is correct |
30 |
Correct |
4 ms |
4300 KB |
Output is correct |
31 |
Correct |
4 ms |
4428 KB |
Output is correct |
32 |
Correct |
4 ms |
4300 KB |
Output is correct |
33 |
Correct |
7 ms |
5552 KB |
Output is correct |
34 |
Correct |
8 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3912 KB |
Output is correct |
4 |
Correct |
2 ms |
3916 KB |
Output is correct |
5 |
Correct |
3 ms |
3916 KB |
Output is correct |
6 |
Correct |
3 ms |
3916 KB |
Output is correct |
7 |
Correct |
3 ms |
3916 KB |
Output is correct |
8 |
Correct |
3 ms |
3916 KB |
Output is correct |
9 |
Correct |
3 ms |
3916 KB |
Output is correct |
10 |
Correct |
3 ms |
3916 KB |
Output is correct |
11 |
Correct |
5 ms |
4172 KB |
Output is correct |
12 |
Correct |
3 ms |
3916 KB |
Output is correct |
13 |
Correct |
3 ms |
3916 KB |
Output is correct |
14 |
Correct |
4 ms |
4172 KB |
Output is correct |
15 |
Correct |
4 ms |
4172 KB |
Output is correct |
16 |
Correct |
3 ms |
4044 KB |
Output is correct |
17 |
Correct |
4 ms |
4300 KB |
Output is correct |
18 |
Correct |
3 ms |
4044 KB |
Output is correct |
19 |
Correct |
3 ms |
4044 KB |
Output is correct |
20 |
Correct |
4 ms |
4172 KB |
Output is correct |
21 |
Correct |
3 ms |
4096 KB |
Output is correct |
22 |
Correct |
3 ms |
4044 KB |
Output is correct |
23 |
Correct |
3 ms |
4044 KB |
Output is correct |
24 |
Correct |
4 ms |
4300 KB |
Output is correct |
25 |
Correct |
4 ms |
4300 KB |
Output is correct |
26 |
Correct |
3 ms |
4044 KB |
Output is correct |
27 |
Correct |
3 ms |
4044 KB |
Output is correct |
28 |
Correct |
4 ms |
4300 KB |
Output is correct |
29 |
Correct |
5 ms |
4812 KB |
Output is correct |
30 |
Correct |
3 ms |
4300 KB |
Output is correct |
31 |
Correct |
4 ms |
4428 KB |
Output is correct |
32 |
Correct |
4 ms |
4300 KB |
Output is correct |
33 |
Correct |
7 ms |
5580 KB |
Output is correct |
34 |
Correct |
7 ms |
5592 KB |
Output is correct |
35 |
Correct |
33 ms |
7492 KB |
Output is correct |
36 |
Correct |
6 ms |
4556 KB |
Output is correct |
37 |
Correct |
34 ms |
8012 KB |
Output is correct |
38 |
Correct |
34 ms |
8756 KB |
Output is correct |
39 |
Correct |
47 ms |
8768 KB |
Output is correct |
40 |
Correct |
45 ms |
8784 KB |
Output is correct |
41 |
Correct |
46 ms |
8772 KB |
Output is correct |
42 |
Correct |
9 ms |
4336 KB |
Output is correct |
43 |
Correct |
9 ms |
4300 KB |
Output is correct |
44 |
Correct |
8 ms |
4428 KB |
Output is correct |
45 |
Correct |
38 ms |
11544 KB |
Output is correct |
46 |
Correct |
40 ms |
11488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3916 KB |
Output is correct |
2 |
Correct |
2 ms |
3916 KB |
Output is correct |
3 |
Correct |
2 ms |
3916 KB |
Output is correct |
4 |
Correct |
2 ms |
3916 KB |
Output is correct |
5 |
Correct |
2 ms |
3916 KB |
Output is correct |
6 |
Correct |
3 ms |
3916 KB |
Output is correct |
7 |
Correct |
3 ms |
3916 KB |
Output is correct |
8 |
Correct |
3 ms |
3916 KB |
Output is correct |
9 |
Correct |
2 ms |
3916 KB |
Output is correct |
10 |
Correct |
3 ms |
3916 KB |
Output is correct |
11 |
Correct |
4 ms |
4172 KB |
Output is correct |
12 |
Correct |
3 ms |
3912 KB |
Output is correct |
13 |
Correct |
3 ms |
3916 KB |
Output is correct |
14 |
Correct |
4 ms |
4172 KB |
Output is correct |
15 |
Correct |
5 ms |
4172 KB |
Output is correct |
16 |
Correct |
3 ms |
4044 KB |
Output is correct |
17 |
Correct |
5 ms |
4256 KB |
Output is correct |
18 |
Correct |
3 ms |
4044 KB |
Output is correct |
19 |
Correct |
3 ms |
4044 KB |
Output is correct |
20 |
Correct |
4 ms |
4172 KB |
Output is correct |
21 |
Correct |
3 ms |
4044 KB |
Output is correct |
22 |
Correct |
3 ms |
4044 KB |
Output is correct |
23 |
Correct |
3 ms |
4044 KB |
Output is correct |
24 |
Correct |
5 ms |
4300 KB |
Output is correct |
25 |
Correct |
4 ms |
4300 KB |
Output is correct |
26 |
Correct |
3 ms |
4044 KB |
Output is correct |
27 |
Correct |
3 ms |
4044 KB |
Output is correct |
28 |
Correct |
4 ms |
4300 KB |
Output is correct |
29 |
Correct |
5 ms |
4892 KB |
Output is correct |
30 |
Correct |
4 ms |
4300 KB |
Output is correct |
31 |
Correct |
4 ms |
4428 KB |
Output is correct |
32 |
Correct |
4 ms |
4300 KB |
Output is correct |
33 |
Correct |
7 ms |
5580 KB |
Output is correct |
34 |
Correct |
8 ms |
5632 KB |
Output is correct |
35 |
Correct |
29 ms |
7508 KB |
Output is correct |
36 |
Correct |
6 ms |
4556 KB |
Output is correct |
37 |
Correct |
25 ms |
7976 KB |
Output is correct |
38 |
Correct |
35 ms |
8780 KB |
Output is correct |
39 |
Correct |
33 ms |
8716 KB |
Output is correct |
40 |
Correct |
34 ms |
8664 KB |
Output is correct |
41 |
Correct |
34 ms |
8772 KB |
Output is correct |
42 |
Correct |
8 ms |
4300 KB |
Output is correct |
43 |
Correct |
8 ms |
4300 KB |
Output is correct |
44 |
Correct |
10 ms |
4328 KB |
Output is correct |
45 |
Correct |
44 ms |
11552 KB |
Output is correct |
46 |
Correct |
36 ms |
11476 KB |
Output is correct |
47 |
Correct |
74 ms |
15552 KB |
Output is correct |
48 |
Correct |
25 ms |
8520 KB |
Output is correct |
49 |
Correct |
29 ms |
7504 KB |
Output is correct |
50 |
Correct |
16 ms |
6884 KB |
Output is correct |
51 |
Correct |
52 ms |
10836 KB |
Output is correct |
52 |
Correct |
52 ms |
11252 KB |
Output is correct |
53 |
Correct |
30 ms |
8692 KB |
Output is correct |
54 |
Correct |
5 ms |
4448 KB |
Output is correct |
55 |
Correct |
6 ms |
4872 KB |
Output is correct |
56 |
Correct |
28 ms |
8140 KB |
Output is correct |
57 |
Correct |
36 ms |
12984 KB |
Output is correct |
58 |
Correct |
13 ms |
5512 KB |
Output is correct |
59 |
Correct |
13 ms |
5760 KB |
Output is correct |
60 |
Correct |
14 ms |
6324 KB |
Output is correct |
61 |
Correct |
14 ms |
5924 KB |
Output is correct |
62 |
Correct |
38 ms |
12260 KB |
Output is correct |
63 |
Correct |
94 ms |
31908 KB |
Output is correct |
64 |
Correct |
118 ms |
40636 KB |
Output is correct |
65 |
Correct |
132 ms |
53156 KB |
Output is correct |
66 |
Correct |
198 ms |
81896 KB |
Output is correct |
67 |
Correct |
197 ms |
81104 KB |
Output is correct |