#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#define all(v) v.begin(),v.end()
#define compress(v) v.erase(unique(all(v)),v.end())
using namespace std;
const int INF = (int)1e9;
struct edge {
int v, cost;
bool operator<(const edge& tmp)const {
return cost > tmp.cost;
}
};
int N,M,SQRT;
int B[30000];
vector<int> P[30000];
bool Used[30000][173];
vector<edge> Edge[30000];
int Dist[30000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
SQRT = (int)sqrt(N);
for (int i = 0; i < M; i++) {
int p;
cin >> B[i] >> p;
if (p < SQRT)
P[B[i]].push_back(p);
else {
for (int j = 1; B[i] - j * p >= 0; j++)
Edge[B[i]].push_back({ B[i] - j * p,j });
for (int j = 1; B[i] + j * p < N; j++)
Edge[B[i]].push_back({ B[i] + j * p,j });
}
}
for (int i = 0; i < N; i++) {
for (int j : P[i]) {
for (int k = 1; i - k * j >= 0; k++) {
Edge[i].push_back({ i - k * j,k });
if (Used[i - k * j][j])
break;
Used[i - k * j][j] = true;
}
}
}
for (int i = N - 1; i >= 0; i--) {
fill(Used[i], Used[i] + SQRT, false);
for (int j : P[i]) {
for (int k = 1; i + k * j < N; k++) {
Edge[i].push_back({ i + k * j,k });
if (Used[i + k * j][j])
break;
Used[i + k * j][j] = true;
}
}
}
priority_queue<edge> q;
fill(Dist, Dist + N, INF);
Dist[B[0]] = 0;
q.push({ B[0],0});
while (!q.empty()) {
auto t = q.top();
q.pop();
if (Dist[t.v] < t.cost)
continue;
for (edge i : Edge[t.v]) {
if (Dist[i.v] > t.cost + i.cost) {
Dist[i.v] = t.cost + i.cost;
q.push({ i.v,Dist[i.v] });
}
}
}
int ans = Dist[B[1]];
if (ans == INF)
ans = -1;
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1756 KB |
Output is correct |
7 |
Correct |
1 ms |
1716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
2 ms |
1748 KB |
Output is correct |
4 |
Correct |
2 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1620 KB |
Output is correct |
6 |
Correct |
2 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1752 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
2 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1748 KB |
Output is correct |
15 |
Correct |
1 ms |
1764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1752 KB |
Output is correct |
2 |
Correct |
1 ms |
1752 KB |
Output is correct |
3 |
Correct |
2 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
1 ms |
1756 KB |
Output is correct |
15 |
Correct |
1 ms |
1760 KB |
Output is correct |
16 |
Correct |
1 ms |
1748 KB |
Output is correct |
17 |
Correct |
2 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
1 ms |
2132 KB |
Output is correct |
20 |
Correct |
2 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
1760 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
2 ms |
2004 KB |
Output is correct |
24 |
Correct |
2 ms |
2260 KB |
Output is correct |
25 |
Correct |
2 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
3 ms |
2132 KB |
Output is correct |
28 |
Correct |
2 ms |
2260 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
2 ms |
2516 KB |
Output is correct |
32 |
Correct |
2 ms |
2388 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
34 |
Correct |
4 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
2 ms |
1752 KB |
Output is correct |
9 |
Correct |
2 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
1 ms |
1748 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
2 ms |
1760 KB |
Output is correct |
15 |
Correct |
2 ms |
1760 KB |
Output is correct |
16 |
Correct |
2 ms |
1752 KB |
Output is correct |
17 |
Correct |
3 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
3 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
1760 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
2 ms |
2260 KB |
Output is correct |
25 |
Correct |
3 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
2 ms |
2260 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
2 ms |
2516 KB |
Output is correct |
32 |
Correct |
3 ms |
2388 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
34 |
Correct |
4 ms |
3584 KB |
Output is correct |
35 |
Correct |
7 ms |
3436 KB |
Output is correct |
36 |
Correct |
2 ms |
2132 KB |
Output is correct |
37 |
Correct |
9 ms |
4692 KB |
Output is correct |
38 |
Correct |
12 ms |
4436 KB |
Output is correct |
39 |
Correct |
9 ms |
4436 KB |
Output is correct |
40 |
Correct |
9 ms |
4564 KB |
Output is correct |
41 |
Correct |
10 ms |
4564 KB |
Output is correct |
42 |
Correct |
6 ms |
3348 KB |
Output is correct |
43 |
Correct |
5 ms |
3152 KB |
Output is correct |
44 |
Correct |
6 ms |
3280 KB |
Output is correct |
45 |
Correct |
11 ms |
7508 KB |
Output is correct |
46 |
Correct |
11 ms |
7524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1700 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1752 KB |
Output is correct |
8 |
Correct |
1 ms |
1752 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
1 ms |
1760 KB |
Output is correct |
13 |
Correct |
2 ms |
1748 KB |
Output is correct |
14 |
Correct |
1 ms |
1748 KB |
Output is correct |
15 |
Correct |
1 ms |
1748 KB |
Output is correct |
16 |
Correct |
1 ms |
1748 KB |
Output is correct |
17 |
Correct |
2 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
2 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
1748 KB |
Output is correct |
22 |
Correct |
2 ms |
2140 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
2 ms |
2260 KB |
Output is correct |
25 |
Correct |
2 ms |
2148 KB |
Output is correct |
26 |
Correct |
3 ms |
2280 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
2 ms |
2260 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
2 ms |
2516 KB |
Output is correct |
32 |
Correct |
2 ms |
2388 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
34 |
Correct |
4 ms |
3540 KB |
Output is correct |
35 |
Correct |
7 ms |
3412 KB |
Output is correct |
36 |
Correct |
3 ms |
2132 KB |
Output is correct |
37 |
Correct |
9 ms |
4768 KB |
Output is correct |
38 |
Correct |
9 ms |
4468 KB |
Output is correct |
39 |
Correct |
9 ms |
4436 KB |
Output is correct |
40 |
Correct |
9 ms |
4564 KB |
Output is correct |
41 |
Correct |
9 ms |
4580 KB |
Output is correct |
42 |
Correct |
6 ms |
3220 KB |
Output is correct |
43 |
Correct |
5 ms |
3152 KB |
Output is correct |
44 |
Correct |
5 ms |
3280 KB |
Output is correct |
45 |
Correct |
11 ms |
7508 KB |
Output is correct |
46 |
Correct |
13 ms |
7544 KB |
Output is correct |
47 |
Correct |
24 ms |
13904 KB |
Output is correct |
48 |
Correct |
12 ms |
8532 KB |
Output is correct |
49 |
Correct |
10 ms |
7892 KB |
Output is correct |
50 |
Correct |
10 ms |
8276 KB |
Output is correct |
51 |
Correct |
28 ms |
11316 KB |
Output is correct |
52 |
Correct |
29 ms |
11800 KB |
Output is correct |
53 |
Correct |
16 ms |
8992 KB |
Output is correct |
54 |
Correct |
6 ms |
7380 KB |
Output is correct |
55 |
Correct |
8 ms |
7888 KB |
Output is correct |
56 |
Correct |
16 ms |
9556 KB |
Output is correct |
57 |
Correct |
31 ms |
16100 KB |
Output is correct |
58 |
Correct |
12 ms |
8780 KB |
Output is correct |
59 |
Correct |
13 ms |
9248 KB |
Output is correct |
60 |
Correct |
14 ms |
9928 KB |
Output is correct |
61 |
Correct |
13 ms |
9032 KB |
Output is correct |
62 |
Correct |
19 ms |
12348 KB |
Output is correct |
63 |
Correct |
49 ms |
32720 KB |
Output is correct |
64 |
Correct |
59 ms |
40236 KB |
Output is correct |
65 |
Correct |
76 ms |
50248 KB |
Output is correct |
66 |
Correct |
116 ms |
74824 KB |
Output is correct |
67 |
Correct |
119 ms |
76612 KB |
Output is correct |