#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 3e4+4;
int n, m, power[N], position[N];
vi building[N];
void sol() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, p; cin >> x >> p; x++;
position[i] = x;
power[i] = p;
building[x].pb(power[i]);
}
for (int i = 1; i <= n; i++) sort(all(building[i])), building[i].erase(unique(all(building[i])), building[i].end());
priority_queue<pii, vector<pii>, greater<>> q;
q.push({0, position[1]}); // dist, curr building
vi dis(n+1, inf); dis[position[1]] = 0;
while (!q.empty()) {
pii f = q.top(); q.pop();
int currBuild = f.s, dist = f.f;
if (dist != dis[currBuild]) continue;
if (currBuild == position[2]) {
cout << dist << endl;
return;
}
for (auto x : building[currBuild]) {
int count = 0;
for (int i = currBuild; i <= n; i += x) {
if (dis[i] > dis[currBuild] + count) {
dis[i] = dis[currBuild] + count;
q.push({dis[i], i});
}
count++;
}
count = 0;
for (int i = currBuild; i >= 1; i -= x) {
if (dis[i] > dis[currBuild] + count) {
dis[i] = dis[currBuild] + count;
q.push({dis[i], i});
}
count++;
}
}
}
cout << -1 << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1368 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
1096 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1500 KB |
Output is correct |
34 |
Correct |
2 ms |
1500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
924 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1164 KB |
Output is correct |
20 |
Correct |
4 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1024 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
1112 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1500 KB |
Output is correct |
34 |
Correct |
2 ms |
1504 KB |
Output is correct |
35 |
Correct |
5 ms |
1628 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
4 ms |
1628 KB |
Output is correct |
38 |
Correct |
6 ms |
1736 KB |
Output is correct |
39 |
Correct |
5 ms |
1628 KB |
Output is correct |
40 |
Correct |
5 ms |
1628 KB |
Output is correct |
41 |
Correct |
5 ms |
1628 KB |
Output is correct |
42 |
Correct |
4 ms |
1628 KB |
Output is correct |
43 |
Correct |
4 ms |
1628 KB |
Output is correct |
44 |
Correct |
7 ms |
1880 KB |
Output is correct |
45 |
Correct |
7 ms |
2140 KB |
Output is correct |
46 |
Correct |
6 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1112 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1112 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1368 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1500 KB |
Output is correct |
34 |
Correct |
2 ms |
1504 KB |
Output is correct |
35 |
Correct |
5 ms |
1628 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
4 ms |
1624 KB |
Output is correct |
38 |
Correct |
5 ms |
1628 KB |
Output is correct |
39 |
Correct |
5 ms |
1496 KB |
Output is correct |
40 |
Correct |
5 ms |
1628 KB |
Output is correct |
41 |
Correct |
5 ms |
1620 KB |
Output is correct |
42 |
Correct |
4 ms |
1640 KB |
Output is correct |
43 |
Correct |
4 ms |
1664 KB |
Output is correct |
44 |
Correct |
6 ms |
1540 KB |
Output is correct |
45 |
Correct |
7 ms |
2092 KB |
Output is correct |
46 |
Correct |
6 ms |
2152 KB |
Output is correct |
47 |
Correct |
7 ms |
2532 KB |
Output is correct |
48 |
Correct |
5 ms |
2152 KB |
Output is correct |
49 |
Correct |
7 ms |
1896 KB |
Output is correct |
50 |
Correct |
3 ms |
1896 KB |
Output is correct |
51 |
Correct |
12 ms |
3524 KB |
Output is correct |
52 |
Correct |
11 ms |
3020 KB |
Output is correct |
53 |
Correct |
7 ms |
2664 KB |
Output is correct |
54 |
Correct |
2 ms |
1384 KB |
Output is correct |
55 |
Correct |
2 ms |
1448 KB |
Output is correct |
56 |
Correct |
530 ms |
3044 KB |
Output is correct |
57 |
Correct |
2 ms |
2008 KB |
Output is correct |
58 |
Correct |
6 ms |
1884 KB |
Output is correct |
59 |
Correct |
6 ms |
1880 KB |
Output is correct |
60 |
Correct |
6 ms |
2024 KB |
Output is correct |
61 |
Correct |
6 ms |
2136 KB |
Output is correct |
62 |
Correct |
7 ms |
2652 KB |
Output is correct |
63 |
Correct |
25 ms |
4048 KB |
Output is correct |
64 |
Correct |
25 ms |
4044 KB |
Output is correct |
65 |
Correct |
32 ms |
4092 KB |
Output is correct |
66 |
Correct |
69 ms |
6860 KB |
Output is correct |
67 |
Correct |
40 ms |
7624 KB |
Output is correct |