#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pi;
#define to first
#define cost second
#define pos first
#define range second
struct Q {
int pos, cost;
};
bool operator<(const Q& a, const Q& b) {
return a.cost > b.cost;
}
set<pi> V[30001];
set<int> inpos[30001];
int dst[30001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
memset(dst, INF, sizeof(dst));
int n, m; cin >> n >> m;
vector<vector<int>> doge(n);
int s = 0, e = 0;
for (int i = 0; i < m; i++)
{
int pos, range; cin >> pos >> range;
if (i == 0) s = pos;
if (i == 1) e = pos;
doge[pos].push_back(range);
inpos[pos].insert(range);
}
for (int i = 0; i < n; i++) {
sort(doge[i].begin(), doge[i].end());
doge[i].erase(unique(doge[i].begin(), doge[i].end()), doge[i].end());
}
for (int pos = 0; pos < n; pos++) {
set<pi>& nV = V[pos];
for (int range : doge[pos]) {
for (int j = pos + range; j < n; j += range) {
if (inpos[j].size() == 0) continue;
int dst = j - pos;
nV.insert({ j, dst / range });
if (inpos[j].find(range) != inpos[j].end()) break;
}
for (int j = pos - range; j >= 0; j -= range) {
if (inpos[j].size() == 0) continue;
int dst = pos - j;
nV.insert({ j, dst / range });
if (inpos[j].find(range) != inpos[j].end()) break;
}
}
}
vector<int> vit(n, -1);
int num = 0;
priority_queue<Q> pq;
pq.push({ s, 0 });
while (!pq.empty()) {
Q top = pq.top(); pq.pop();
if (top.pos == e) {
cout << top.cost;
return 0;
}
for (pi w : V[top.pos]) {
if (vit[w.to] == num) continue;
if (dst[w.to] > top.cost + w.cost) {
dst[w.to] = top.cost + w.cost;
pq.push({ w.to, dst[w.to] });
}
vit[w.to] = num;
}
num++;
}
cout << -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3308 KB |
Output is correct |
2 |
Correct |
2 ms |
3328 KB |
Output is correct |
3 |
Correct |
2 ms |
3308 KB |
Output is correct |
4 |
Correct |
2 ms |
3308 KB |
Output is correct |
5 |
Correct |
3 ms |
3316 KB |
Output is correct |
6 |
Correct |
2 ms |
3308 KB |
Output is correct |
7 |
Correct |
3 ms |
3308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3308 KB |
Output is correct |
2 |
Correct |
3 ms |
3308 KB |
Output is correct |
3 |
Correct |
2 ms |
3308 KB |
Output is correct |
4 |
Correct |
2 ms |
3308 KB |
Output is correct |
5 |
Correct |
3 ms |
3308 KB |
Output is correct |
6 |
Correct |
2 ms |
3184 KB |
Output is correct |
7 |
Correct |
2 ms |
3308 KB |
Output is correct |
8 |
Correct |
2 ms |
3308 KB |
Output is correct |
9 |
Correct |
3 ms |
3308 KB |
Output is correct |
10 |
Correct |
3 ms |
3308 KB |
Output is correct |
11 |
Correct |
4 ms |
3564 KB |
Output is correct |
12 |
Correct |
3 ms |
3308 KB |
Output is correct |
13 |
Correct |
3 ms |
3308 KB |
Output is correct |
14 |
Correct |
3 ms |
3436 KB |
Output is correct |
15 |
Correct |
4 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3308 KB |
Output is correct |
2 |
Correct |
2 ms |
3308 KB |
Output is correct |
3 |
Correct |
3 ms |
3308 KB |
Output is correct |
4 |
Correct |
2 ms |
3308 KB |
Output is correct |
5 |
Correct |
2 ms |
3308 KB |
Output is correct |
6 |
Correct |
2 ms |
3308 KB |
Output is correct |
7 |
Correct |
2 ms |
3308 KB |
Output is correct |
8 |
Correct |
2 ms |
3308 KB |
Output is correct |
9 |
Correct |
3 ms |
3308 KB |
Output is correct |
10 |
Correct |
3 ms |
3308 KB |
Output is correct |
11 |
Correct |
4 ms |
3564 KB |
Output is correct |
12 |
Correct |
3 ms |
3308 KB |
Output is correct |
13 |
Correct |
3 ms |
3308 KB |
Output is correct |
14 |
Correct |
4 ms |
3436 KB |
Output is correct |
15 |
Correct |
3 ms |
3436 KB |
Output is correct |
16 |
Correct |
3 ms |
3436 KB |
Output is correct |
17 |
Correct |
5 ms |
4076 KB |
Output is correct |
18 |
Correct |
3 ms |
3564 KB |
Output is correct |
19 |
Correct |
3 ms |
3436 KB |
Output is correct |
20 |
Correct |
3 ms |
3692 KB |
Output is correct |
21 |
Correct |
3 ms |
3436 KB |
Output is correct |
22 |
Correct |
3 ms |
3436 KB |
Output is correct |
23 |
Correct |
4 ms |
3564 KB |
Output is correct |
24 |
Correct |
5 ms |
3820 KB |
Output is correct |
25 |
Correct |
5 ms |
3692 KB |
Output is correct |
26 |
Correct |
3 ms |
3308 KB |
Output is correct |
27 |
Correct |
3 ms |
3308 KB |
Output is correct |
28 |
Correct |
6 ms |
4460 KB |
Output is correct |
29 |
Correct |
3 ms |
3436 KB |
Output is correct |
30 |
Correct |
3 ms |
3308 KB |
Output is correct |
31 |
Correct |
3 ms |
3308 KB |
Output is correct |
32 |
Correct |
3 ms |
3308 KB |
Output is correct |
33 |
Correct |
4 ms |
3564 KB |
Output is correct |
34 |
Correct |
4 ms |
3564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3308 KB |
Output is correct |
2 |
Correct |
3 ms |
3308 KB |
Output is correct |
3 |
Correct |
2 ms |
3308 KB |
Output is correct |
4 |
Correct |
2 ms |
3308 KB |
Output is correct |
5 |
Correct |
2 ms |
3308 KB |
Output is correct |
6 |
Correct |
2 ms |
3308 KB |
Output is correct |
7 |
Correct |
2 ms |
3308 KB |
Output is correct |
8 |
Correct |
2 ms |
3308 KB |
Output is correct |
9 |
Correct |
2 ms |
3308 KB |
Output is correct |
10 |
Correct |
3 ms |
3308 KB |
Output is correct |
11 |
Correct |
4 ms |
3564 KB |
Output is correct |
12 |
Correct |
3 ms |
3308 KB |
Output is correct |
13 |
Correct |
3 ms |
3308 KB |
Output is correct |
14 |
Correct |
4 ms |
3564 KB |
Output is correct |
15 |
Correct |
3 ms |
3436 KB |
Output is correct |
16 |
Correct |
3 ms |
3436 KB |
Output is correct |
17 |
Correct |
5 ms |
4076 KB |
Output is correct |
18 |
Correct |
3 ms |
3564 KB |
Output is correct |
19 |
Correct |
3 ms |
3436 KB |
Output is correct |
20 |
Correct |
3 ms |
3692 KB |
Output is correct |
21 |
Correct |
3 ms |
3436 KB |
Output is correct |
22 |
Correct |
3 ms |
3436 KB |
Output is correct |
23 |
Correct |
3 ms |
3564 KB |
Output is correct |
24 |
Correct |
5 ms |
3820 KB |
Output is correct |
25 |
Correct |
4 ms |
3692 KB |
Output is correct |
26 |
Correct |
3 ms |
3308 KB |
Output is correct |
27 |
Correct |
3 ms |
3308 KB |
Output is correct |
28 |
Correct |
6 ms |
4492 KB |
Output is correct |
29 |
Correct |
3 ms |
3436 KB |
Output is correct |
30 |
Correct |
2 ms |
3308 KB |
Output is correct |
31 |
Correct |
3 ms |
3328 KB |
Output is correct |
32 |
Correct |
3 ms |
3308 KB |
Output is correct |
33 |
Correct |
4 ms |
3564 KB |
Output is correct |
34 |
Correct |
4 ms |
3564 KB |
Output is correct |
35 |
Correct |
33 ms |
8812 KB |
Output is correct |
36 |
Correct |
6 ms |
4204 KB |
Output is correct |
37 |
Correct |
47 ms |
12524 KB |
Output is correct |
38 |
Correct |
49 ms |
11840 KB |
Output is correct |
39 |
Correct |
45 ms |
11884 KB |
Output is correct |
40 |
Correct |
47 ms |
11884 KB |
Output is correct |
41 |
Correct |
47 ms |
11884 KB |
Output is correct |
42 |
Correct |
7 ms |
3564 KB |
Output is correct |
43 |
Correct |
9 ms |
3564 KB |
Output is correct |
44 |
Correct |
8 ms |
3840 KB |
Output is correct |
45 |
Correct |
23 ms |
6252 KB |
Output is correct |
46 |
Correct |
23 ms |
6252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3308 KB |
Output is correct |
2 |
Correct |
2 ms |
3308 KB |
Output is correct |
3 |
Correct |
2 ms |
3308 KB |
Output is correct |
4 |
Correct |
2 ms |
3308 KB |
Output is correct |
5 |
Correct |
2 ms |
3308 KB |
Output is correct |
6 |
Correct |
2 ms |
3308 KB |
Output is correct |
7 |
Correct |
2 ms |
3308 KB |
Output is correct |
8 |
Correct |
2 ms |
3308 KB |
Output is correct |
9 |
Correct |
2 ms |
3308 KB |
Output is correct |
10 |
Correct |
3 ms |
3308 KB |
Output is correct |
11 |
Correct |
4 ms |
3564 KB |
Output is correct |
12 |
Correct |
3 ms |
3308 KB |
Output is correct |
13 |
Correct |
3 ms |
3308 KB |
Output is correct |
14 |
Correct |
3 ms |
3436 KB |
Output is correct |
15 |
Correct |
4 ms |
3436 KB |
Output is correct |
16 |
Correct |
4 ms |
3436 KB |
Output is correct |
17 |
Correct |
5 ms |
4076 KB |
Output is correct |
18 |
Correct |
3 ms |
3564 KB |
Output is correct |
19 |
Correct |
3 ms |
3436 KB |
Output is correct |
20 |
Correct |
4 ms |
3692 KB |
Output is correct |
21 |
Correct |
3 ms |
3436 KB |
Output is correct |
22 |
Correct |
3 ms |
3436 KB |
Output is correct |
23 |
Correct |
3 ms |
3564 KB |
Output is correct |
24 |
Correct |
5 ms |
3820 KB |
Output is correct |
25 |
Correct |
4 ms |
3692 KB |
Output is correct |
26 |
Correct |
3 ms |
3308 KB |
Output is correct |
27 |
Correct |
3 ms |
3308 KB |
Output is correct |
28 |
Correct |
6 ms |
4460 KB |
Output is correct |
29 |
Correct |
3 ms |
3436 KB |
Output is correct |
30 |
Correct |
3 ms |
3308 KB |
Output is correct |
31 |
Correct |
3 ms |
3308 KB |
Output is correct |
32 |
Correct |
3 ms |
3308 KB |
Output is correct |
33 |
Correct |
4 ms |
3564 KB |
Output is correct |
34 |
Correct |
4 ms |
3564 KB |
Output is correct |
35 |
Correct |
34 ms |
8812 KB |
Output is correct |
36 |
Correct |
6 ms |
4204 KB |
Output is correct |
37 |
Correct |
48 ms |
12524 KB |
Output is correct |
38 |
Correct |
48 ms |
11884 KB |
Output is correct |
39 |
Correct |
47 ms |
11884 KB |
Output is correct |
40 |
Correct |
49 ms |
11884 KB |
Output is correct |
41 |
Correct |
48 ms |
11904 KB |
Output is correct |
42 |
Correct |
7 ms |
3564 KB |
Output is correct |
43 |
Correct |
7 ms |
3564 KB |
Output is correct |
44 |
Correct |
10 ms |
3820 KB |
Output is correct |
45 |
Correct |
24 ms |
6252 KB |
Output is correct |
46 |
Correct |
24 ms |
6252 KB |
Output is correct |
47 |
Correct |
136 ms |
36588 KB |
Output is correct |
48 |
Correct |
35 ms |
11628 KB |
Output is correct |
49 |
Correct |
28 ms |
8684 KB |
Output is correct |
50 |
Correct |
21 ms |
7660 KB |
Output is correct |
51 |
Correct |
68 ms |
15592 KB |
Output is correct |
52 |
Correct |
76 ms |
17128 KB |
Output is correct |
53 |
Correct |
31 ms |
8940 KB |
Output is correct |
54 |
Correct |
3 ms |
4076 KB |
Output is correct |
55 |
Correct |
3 ms |
4076 KB |
Output is correct |
56 |
Correct |
22 ms |
9196 KB |
Output is correct |
57 |
Correct |
14 ms |
6892 KB |
Output is correct |
58 |
Correct |
7 ms |
4204 KB |
Output is correct |
59 |
Correct |
9 ms |
4460 KB |
Output is correct |
60 |
Correct |
9 ms |
4588 KB |
Output is correct |
61 |
Correct |
10 ms |
4716 KB |
Output is correct |
62 |
Correct |
105 ms |
28524 KB |
Output is correct |
63 |
Correct |
24 ms |
6764 KB |
Output is correct |
64 |
Correct |
24 ms |
6508 KB |
Output is correct |
65 |
Correct |
29 ms |
6764 KB |
Output is correct |
66 |
Correct |
37 ms |
7276 KB |
Output is correct |
67 |
Correct |
39 ms |
7276 KB |
Output is correct |