#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
template <class X, class Y>
bool minimize(X &x, Y y) {
if (x > y) return x = y, true;
else return false;
}
int n, m;
const ll INF = (ll) 1e18 + 7;
#define ii pair <long long, int>
#define MAX 50'005
vector <ii> adj[MAX + 2];
int b[MAX + 2], p[MAX + 2];
ll dist[MAX + 2];
vector <int> jump[MAX + 2];
void Dijkstra(int s) {
priority_queue <ii, vector <ii>, greater <ii>> q;
FOR(i, 1, n) dist[i] = INF;
dist[s] = 0;
q.push({0, s});
while (!q.empty()) {
ll cost = q.top().first;
int u = q.top().second;
q.pop();
if (cost > dist[u]) continue;
for (auto it : adj[u]) {
int v = it.first, w = it.second;
if (minimize(dist[v], dist[u] + w)) {
q.push({dist[v], v});
}
}
}
cout << (dist[b[2]] != INF ? dist[b[2]] : -1);
}
void add_edge(int u, int v, int w) {
adj[u].push_back({v, w});
}
bool used[MAX + 2];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
#define task "sky"
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> m;
memset(used, false, sizeof(used));
FOR(i, 1, m) {
cin >> b[i] >> p[i];
++b[i];
used[b[i]] = true;
jump[b[i]].push_back(p[i]);
}
FOR(i, 1, n) {
if (!used[i]) continue;
for (int x : jump[i]) {
int j = x + i, cur = 1;
while (j <= n) {
if (used[j]) add_edge(i, j, cur);
j += x;
++cur;
}
j = i - x, cur = 1;
while (j >= 1) {
if (used[j]) add_edge(i, j, cur);
j -= x;
++cur;
}
}
}
Dijkstra(b[1]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3408 KB |
Output is correct |
5 |
Correct |
2 ms |
3408 KB |
Output is correct |
6 |
Correct |
2 ms |
3408 KB |
Output is correct |
7 |
Correct |
2 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3408 KB |
Output is correct |
5 |
Correct |
2 ms |
3408 KB |
Output is correct |
6 |
Correct |
2 ms |
3408 KB |
Output is correct |
7 |
Correct |
2 ms |
3408 KB |
Output is correct |
8 |
Correct |
2 ms |
3408 KB |
Output is correct |
9 |
Correct |
2 ms |
3408 KB |
Output is correct |
10 |
Correct |
2 ms |
3664 KB |
Output is correct |
11 |
Correct |
2 ms |
3664 KB |
Output is correct |
12 |
Correct |
2 ms |
3920 KB |
Output is correct |
13 |
Correct |
5 ms |
10068 KB |
Output is correct |
14 |
Correct |
2 ms |
3664 KB |
Output is correct |
15 |
Correct |
3 ms |
3664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3576 KB |
Output is correct |
5 |
Correct |
2 ms |
3408 KB |
Output is correct |
6 |
Correct |
2 ms |
3408 KB |
Output is correct |
7 |
Correct |
2 ms |
3408 KB |
Output is correct |
8 |
Correct |
2 ms |
3408 KB |
Output is correct |
9 |
Correct |
2 ms |
3408 KB |
Output is correct |
10 |
Correct |
2 ms |
3664 KB |
Output is correct |
11 |
Correct |
2 ms |
3664 KB |
Output is correct |
12 |
Correct |
3 ms |
4004 KB |
Output is correct |
13 |
Correct |
6 ms |
9920 KB |
Output is correct |
14 |
Correct |
2 ms |
3664 KB |
Output is correct |
15 |
Correct |
2 ms |
3664 KB |
Output is correct |
16 |
Correct |
2 ms |
3664 KB |
Output is correct |
17 |
Correct |
3 ms |
3920 KB |
Output is correct |
18 |
Correct |
2 ms |
3664 KB |
Output is correct |
19 |
Correct |
2 ms |
3664 KB |
Output is correct |
20 |
Correct |
92 ms |
67880 KB |
Output is correct |
21 |
Correct |
2 ms |
3664 KB |
Output is correct |
22 |
Correct |
2 ms |
3664 KB |
Output is correct |
23 |
Correct |
2 ms |
3664 KB |
Output is correct |
24 |
Correct |
2 ms |
3664 KB |
Output is correct |
25 |
Correct |
2 ms |
3664 KB |
Output is correct |
26 |
Correct |
6 ms |
9928 KB |
Output is correct |
27 |
Correct |
5 ms |
6860 KB |
Output is correct |
28 |
Correct |
3 ms |
3920 KB |
Output is correct |
29 |
Correct |
2 ms |
3664 KB |
Output is correct |
30 |
Correct |
3 ms |
3408 KB |
Output is correct |
31 |
Correct |
2 ms |
3424 KB |
Output is correct |
32 |
Correct |
2 ms |
3408 KB |
Output is correct |
33 |
Correct |
2 ms |
3664 KB |
Output is correct |
34 |
Correct |
2 ms |
3560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
2 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3408 KB |
Output is correct |
5 |
Correct |
2 ms |
3592 KB |
Output is correct |
6 |
Correct |
2 ms |
3408 KB |
Output is correct |
7 |
Correct |
1 ms |
3408 KB |
Output is correct |
8 |
Correct |
2 ms |
3576 KB |
Output is correct |
9 |
Correct |
2 ms |
3576 KB |
Output is correct |
10 |
Correct |
2 ms |
3664 KB |
Output is correct |
11 |
Correct |
2 ms |
3832 KB |
Output is correct |
12 |
Correct |
2 ms |
3920 KB |
Output is correct |
13 |
Correct |
6 ms |
9920 KB |
Output is correct |
14 |
Correct |
2 ms |
3664 KB |
Output is correct |
15 |
Correct |
2 ms |
3664 KB |
Output is correct |
16 |
Correct |
2 ms |
3664 KB |
Output is correct |
17 |
Correct |
3 ms |
3920 KB |
Output is correct |
18 |
Correct |
2 ms |
3664 KB |
Output is correct |
19 |
Correct |
2 ms |
3664 KB |
Output is correct |
20 |
Correct |
84 ms |
67724 KB |
Output is correct |
21 |
Correct |
3 ms |
3664 KB |
Output is correct |
22 |
Correct |
2 ms |
3664 KB |
Output is correct |
23 |
Correct |
2 ms |
3664 KB |
Output is correct |
24 |
Correct |
2 ms |
3664 KB |
Output is correct |
25 |
Correct |
3 ms |
3664 KB |
Output is correct |
26 |
Correct |
8 ms |
9928 KB |
Output is correct |
27 |
Correct |
6 ms |
6860 KB |
Output is correct |
28 |
Correct |
3 ms |
3920 KB |
Output is correct |
29 |
Correct |
2 ms |
3664 KB |
Output is correct |
30 |
Correct |
3 ms |
3408 KB |
Output is correct |
31 |
Correct |
2 ms |
3408 KB |
Output is correct |
32 |
Correct |
2 ms |
3408 KB |
Output is correct |
33 |
Correct |
2 ms |
3664 KB |
Output is correct |
34 |
Correct |
2 ms |
3664 KB |
Output is correct |
35 |
Correct |
9 ms |
6736 KB |
Output is correct |
36 |
Correct |
3 ms |
3932 KB |
Output is correct |
37 |
Correct |
12 ms |
9808 KB |
Output is correct |
38 |
Correct |
12 ms |
8708 KB |
Output is correct |
39 |
Correct |
12 ms |
9040 KB |
Output is correct |
40 |
Correct |
13 ms |
8784 KB |
Output is correct |
41 |
Correct |
12 ms |
8528 KB |
Output is correct |
42 |
Correct |
70 ms |
71640 KB |
Output is correct |
43 |
Correct |
55 ms |
38852 KB |
Output is correct |
44 |
Runtime error |
266 ms |
262144 KB |
Execution killed with signal 9 |
45 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3408 KB |
Output is correct |
2 |
Correct |
2 ms |
3408 KB |
Output is correct |
3 |
Correct |
3 ms |
3408 KB |
Output is correct |
4 |
Correct |
2 ms |
3408 KB |
Output is correct |
5 |
Correct |
2 ms |
3408 KB |
Output is correct |
6 |
Correct |
1 ms |
3408 KB |
Output is correct |
7 |
Correct |
2 ms |
3408 KB |
Output is correct |
8 |
Correct |
2 ms |
3408 KB |
Output is correct |
9 |
Correct |
2 ms |
3408 KB |
Output is correct |
10 |
Correct |
2 ms |
3664 KB |
Output is correct |
11 |
Correct |
3 ms |
3664 KB |
Output is correct |
12 |
Correct |
3 ms |
3920 KB |
Output is correct |
13 |
Correct |
6 ms |
9920 KB |
Output is correct |
14 |
Correct |
2 ms |
3664 KB |
Output is correct |
15 |
Correct |
2 ms |
3664 KB |
Output is correct |
16 |
Correct |
2 ms |
3740 KB |
Output is correct |
17 |
Correct |
3 ms |
3920 KB |
Output is correct |
18 |
Correct |
2 ms |
3664 KB |
Output is correct |
19 |
Correct |
2 ms |
3664 KB |
Output is correct |
20 |
Correct |
89 ms |
67844 KB |
Output is correct |
21 |
Correct |
2 ms |
3664 KB |
Output is correct |
22 |
Correct |
2 ms |
3664 KB |
Output is correct |
23 |
Correct |
2 ms |
3664 KB |
Output is correct |
24 |
Correct |
3 ms |
3664 KB |
Output is correct |
25 |
Correct |
2 ms |
3664 KB |
Output is correct |
26 |
Correct |
6 ms |
10096 KB |
Output is correct |
27 |
Correct |
5 ms |
7028 KB |
Output is correct |
28 |
Correct |
3 ms |
3920 KB |
Output is correct |
29 |
Correct |
2 ms |
3664 KB |
Output is correct |
30 |
Correct |
2 ms |
3408 KB |
Output is correct |
31 |
Correct |
2 ms |
3408 KB |
Output is correct |
32 |
Correct |
2 ms |
3408 KB |
Output is correct |
33 |
Correct |
3 ms |
3664 KB |
Output is correct |
34 |
Correct |
2 ms |
3664 KB |
Output is correct |
35 |
Correct |
9 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
3920 KB |
Output is correct |
37 |
Correct |
12 ms |
9976 KB |
Output is correct |
38 |
Correct |
15 ms |
8528 KB |
Output is correct |
39 |
Correct |
12 ms |
9040 KB |
Output is correct |
40 |
Correct |
12 ms |
8784 KB |
Output is correct |
41 |
Correct |
12 ms |
8528 KB |
Output is correct |
42 |
Correct |
66 ms |
71584 KB |
Output is correct |
43 |
Correct |
46 ms |
38904 KB |
Output is correct |
44 |
Runtime error |
277 ms |
262144 KB |
Execution killed with signal 9 |
45 |
Halted |
0 ms |
0 KB |
- |