#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define PB push_back
#define int long long
#define _sz(e) (int)e.size()
#define pii pair <int , int>
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse4,avx,avx2")
const ll maxn = 3e4 + 4 , N = 2e6 + 4 , mod = 1e9 + 7 , INF = 1e18 + 4 , lg = 43;
int n , m , b[maxn] , p[maxn] , dist[maxn];
vector <pii> edges[maxn];
set <pii> se;
void dijkstra() {
fill(dist , dist + maxn , INF);
priority_queue <pii> pq;
pq.push({dist[b[0]] = 0 , b[0]});
while(_sz(pq)) {
pii cur = pq.top();
pq.pop();
int v = cur.S;
for (auto u : edges[v]) {
if(dist[v] + u.S < dist[u.F]) {
dist[u.F] = dist[v] + u.S;
pq.push({-dist[u.F] , u.F});
}
}
}
}
int32_t main() {
FAST;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
se.insert({b[i] , p[i]});
}
for (auto v : se) {
for (int h = v.F + v.S; h < n; h += v.S) {
edges[v.F].PB({h , (h - v.F) / v.S});
if(se.find({h , v.S}) != se.end()) break;
}
for (int h = v.F - v.S; h >= 0; h -= v.S) {
edges[v.F].PB({h , (v.F - h) / v.S});
if(se.find({h , v.S}) != se.end()) break;
}
}
dijkstra();
cout << (dist[b[1]] == INF ? -1 : dist[b[1]]) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1252 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1160 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1484 KB |
Output is correct |
12 |
Correct |
1 ms |
1288 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1484 KB |
Output is correct |
15 |
Correct |
2 ms |
1476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1176 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1484 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1484 KB |
Output is correct |
15 |
Correct |
2 ms |
1484 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
4 ms |
1740 KB |
Output is correct |
18 |
Correct |
2 ms |
1484 KB |
Output is correct |
19 |
Correct |
2 ms |
1416 KB |
Output is correct |
20 |
Correct |
2 ms |
1424 KB |
Output is correct |
21 |
Correct |
2 ms |
1292 KB |
Output is correct |
22 |
Correct |
2 ms |
1356 KB |
Output is correct |
23 |
Correct |
2 ms |
1484 KB |
Output is correct |
24 |
Correct |
4 ms |
1740 KB |
Output is correct |
25 |
Correct |
3 ms |
1612 KB |
Output is correct |
26 |
Correct |
2 ms |
1484 KB |
Output is correct |
27 |
Correct |
2 ms |
1484 KB |
Output is correct |
28 |
Correct |
4 ms |
1868 KB |
Output is correct |
29 |
Correct |
5 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
1868 KB |
Output is correct |
31 |
Correct |
3 ms |
2124 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
8 ms |
3888 KB |
Output is correct |
34 |
Correct |
8 ms |
3920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
2 ms |
1284 KB |
Output is correct |
11 |
Correct |
3 ms |
1484 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1484 KB |
Output is correct |
15 |
Correct |
2 ms |
1484 KB |
Output is correct |
16 |
Correct |
2 ms |
1416 KB |
Output is correct |
17 |
Correct |
4 ms |
1740 KB |
Output is correct |
18 |
Correct |
2 ms |
1484 KB |
Output is correct |
19 |
Correct |
2 ms |
1356 KB |
Output is correct |
20 |
Correct |
2 ms |
1484 KB |
Output is correct |
21 |
Correct |
2 ms |
1312 KB |
Output is correct |
22 |
Correct |
2 ms |
1356 KB |
Output is correct |
23 |
Correct |
2 ms |
1424 KB |
Output is correct |
24 |
Correct |
4 ms |
1740 KB |
Output is correct |
25 |
Correct |
3 ms |
1612 KB |
Output is correct |
26 |
Correct |
2 ms |
1484 KB |
Output is correct |
27 |
Correct |
2 ms |
1420 KB |
Output is correct |
28 |
Correct |
4 ms |
1868 KB |
Output is correct |
29 |
Correct |
5 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
1868 KB |
Output is correct |
31 |
Correct |
3 ms |
2124 KB |
Output is correct |
32 |
Correct |
3 ms |
1868 KB |
Output is correct |
33 |
Correct |
7 ms |
3920 KB |
Output is correct |
34 |
Correct |
8 ms |
3928 KB |
Output is correct |
35 |
Correct |
35 ms |
5444 KB |
Output is correct |
36 |
Correct |
6 ms |
1996 KB |
Output is correct |
37 |
Correct |
50 ms |
6968 KB |
Output is correct |
38 |
Correct |
53 ms |
7316 KB |
Output is correct |
39 |
Correct |
54 ms |
7364 KB |
Output is correct |
40 |
Correct |
62 ms |
7380 KB |
Output is correct |
41 |
Correct |
53 ms |
7176 KB |
Output is correct |
42 |
Correct |
8 ms |
2124 KB |
Output is correct |
43 |
Correct |
8 ms |
2068 KB |
Output is correct |
44 |
Correct |
8 ms |
2124 KB |
Output is correct |
45 |
Correct |
42 ms |
12104 KB |
Output is correct |
46 |
Correct |
42 ms |
12096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1160 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
3 ms |
1484 KB |
Output is correct |
12 |
Correct |
1 ms |
1292 KB |
Output is correct |
13 |
Correct |
2 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1484 KB |
Output is correct |
15 |
Correct |
2 ms |
1484 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
4 ms |
1740 KB |
Output is correct |
18 |
Correct |
2 ms |
1484 KB |
Output is correct |
19 |
Correct |
2 ms |
1412 KB |
Output is correct |
20 |
Correct |
2 ms |
1484 KB |
Output is correct |
21 |
Correct |
2 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
1356 KB |
Output is correct |
23 |
Correct |
2 ms |
1484 KB |
Output is correct |
24 |
Correct |
4 ms |
1728 KB |
Output is correct |
25 |
Correct |
3 ms |
1612 KB |
Output is correct |
26 |
Correct |
2 ms |
1484 KB |
Output is correct |
27 |
Correct |
2 ms |
1420 KB |
Output is correct |
28 |
Correct |
4 ms |
1808 KB |
Output is correct |
29 |
Correct |
4 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
1740 KB |
Output is correct |
31 |
Correct |
3 ms |
2176 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
8 ms |
3940 KB |
Output is correct |
34 |
Correct |
9 ms |
3920 KB |
Output is correct |
35 |
Correct |
36 ms |
5408 KB |
Output is correct |
36 |
Correct |
6 ms |
2012 KB |
Output is correct |
37 |
Correct |
49 ms |
6940 KB |
Output is correct |
38 |
Correct |
53 ms |
7364 KB |
Output is correct |
39 |
Correct |
53 ms |
7364 KB |
Output is correct |
40 |
Correct |
56 ms |
7376 KB |
Output is correct |
41 |
Correct |
52 ms |
7156 KB |
Output is correct |
42 |
Correct |
7 ms |
2124 KB |
Output is correct |
43 |
Correct |
7 ms |
2036 KB |
Output is correct |
44 |
Correct |
8 ms |
2124 KB |
Output is correct |
45 |
Correct |
43 ms |
12104 KB |
Output is correct |
46 |
Correct |
43 ms |
12184 KB |
Output is correct |
47 |
Correct |
181 ms |
20892 KB |
Output is correct |
48 |
Correct |
55 ms |
7556 KB |
Output is correct |
49 |
Correct |
38 ms |
5804 KB |
Output is correct |
50 |
Correct |
31 ms |
5224 KB |
Output is correct |
51 |
Correct |
99 ms |
11216 KB |
Output is correct |
52 |
Correct |
106 ms |
12356 KB |
Output is correct |
53 |
Correct |
35 ms |
6924 KB |
Output is correct |
54 |
Correct |
3 ms |
2124 KB |
Output is correct |
55 |
Correct |
5 ms |
3016 KB |
Output is correct |
56 |
Correct |
24 ms |
5124 KB |
Output is correct |
57 |
Correct |
81 ms |
18464 KB |
Output is correct |
58 |
Correct |
10 ms |
3900 KB |
Output is correct |
59 |
Correct |
13 ms |
4484 KB |
Output is correct |
60 |
Correct |
16 ms |
5912 KB |
Output is correct |
61 |
Correct |
18 ms |
4552 KB |
Output is correct |
62 |
Correct |
100 ms |
13600 KB |
Output is correct |
63 |
Correct |
117 ms |
44860 KB |
Output is correct |
64 |
Correct |
124 ms |
51576 KB |
Output is correct |
65 |
Correct |
179 ms |
71228 KB |
Output is correct |
66 |
Correct |
359 ms |
125428 KB |
Output is correct |
67 |
Correct |
343 ms |
125636 KB |
Output is correct |