#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];
set <int> jump[MAX + 2], go[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;
if (u == b[2]) return void(cout << cost);
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 << -1;
}
void add_edge(int u, int v, int w) {
adj[u].push_back({v, w});
}
bool used[MAX + 2];
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#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]].insert(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);
if (go[j].find(x) != go[j].end()) break;
else go[j].insert(x);
}
j += x;
++cur;
}
}
}
FOR(i, 1, n) {
if (!used[i]) continue;
for (int x : jump[i]) {
int j = i - x, cur = 1;
while (j >= 1) {
if (used[j]) {
add_edge(i, j, cur);
if (go[j].find(x) != go[j].end()) break;
else go[j].insert(x);
}
j -= x;
++cur;
}
}
}
Dijkstra(b[1]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
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".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6992 KB |
Output is correct |
2 |
Correct |
2 ms |
6992 KB |
Output is correct |
3 |
Correct |
2 ms |
6992 KB |
Output is correct |
4 |
Correct |
3 ms |
6992 KB |
Output is correct |
5 |
Correct |
3 ms |
6992 KB |
Output is correct |
6 |
Correct |
2 ms |
6992 KB |
Output is correct |
7 |
Correct |
3 ms |
6992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6992 KB |
Output is correct |
2 |
Correct |
2 ms |
7124 KB |
Output is correct |
3 |
Correct |
3 ms |
7160 KB |
Output is correct |
4 |
Correct |
2 ms |
6992 KB |
Output is correct |
5 |
Correct |
2 ms |
6992 KB |
Output is correct |
6 |
Correct |
2 ms |
6992 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
2 ms |
6992 KB |
Output is correct |
9 |
Correct |
2 ms |
6992 KB |
Output is correct |
10 |
Correct |
2 ms |
7248 KB |
Output is correct |
11 |
Correct |
3 ms |
7248 KB |
Output is correct |
12 |
Correct |
2 ms |
7160 KB |
Output is correct |
13 |
Correct |
2 ms |
6992 KB |
Output is correct |
14 |
Correct |
3 ms |
7248 KB |
Output is correct |
15 |
Correct |
3 ms |
7248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6992 KB |
Output is correct |
2 |
Correct |
2 ms |
6992 KB |
Output is correct |
3 |
Correct |
2 ms |
6992 KB |
Output is correct |
4 |
Correct |
2 ms |
6992 KB |
Output is correct |
5 |
Correct |
2 ms |
6992 KB |
Output is correct |
6 |
Correct |
2 ms |
6992 KB |
Output is correct |
7 |
Correct |
2 ms |
6992 KB |
Output is correct |
8 |
Correct |
2 ms |
6992 KB |
Output is correct |
9 |
Correct |
2 ms |
6992 KB |
Output is correct |
10 |
Correct |
2 ms |
7248 KB |
Output is correct |
11 |
Correct |
3 ms |
7248 KB |
Output is correct |
12 |
Correct |
2 ms |
6992 KB |
Output is correct |
13 |
Correct |
2 ms |
7164 KB |
Output is correct |
14 |
Correct |
3 ms |
7248 KB |
Output is correct |
15 |
Correct |
3 ms |
7248 KB |
Output is correct |
16 |
Correct |
2 ms |
7248 KB |
Output is correct |
17 |
Correct |
5 ms |
8016 KB |
Output is correct |
18 |
Correct |
3 ms |
7248 KB |
Output is correct |
19 |
Correct |
2 ms |
7248 KB |
Output is correct |
20 |
Correct |
3 ms |
7504 KB |
Output is correct |
21 |
Correct |
2 ms |
7248 KB |
Output is correct |
22 |
Correct |
2 ms |
7248 KB |
Output is correct |
23 |
Correct |
3 ms |
7248 KB |
Output is correct |
24 |
Correct |
4 ms |
7608 KB |
Output is correct |
25 |
Correct |
3 ms |
7504 KB |
Output is correct |
26 |
Correct |
2 ms |
7048 KB |
Output is correct |
27 |
Correct |
3 ms |
7160 KB |
Output is correct |
28 |
Correct |
7 ms |
8528 KB |
Output is correct |
29 |
Correct |
2 ms |
7084 KB |
Output is correct |
30 |
Correct |
2 ms |
6992 KB |
Output is correct |
31 |
Correct |
2 ms |
6992 KB |
Output is correct |
32 |
Correct |
2 ms |
6992 KB |
Output is correct |
33 |
Correct |
4 ms |
7248 KB |
Output is correct |
34 |
Correct |
3 ms |
7248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6992 KB |
Output is correct |
2 |
Correct |
2 ms |
6992 KB |
Output is correct |
3 |
Correct |
2 ms |
6992 KB |
Output is correct |
4 |
Correct |
2 ms |
6992 KB |
Output is correct |
5 |
Correct |
2 ms |
7108 KB |
Output is correct |
6 |
Correct |
2 ms |
6992 KB |
Output is correct |
7 |
Correct |
2 ms |
6992 KB |
Output is correct |
8 |
Correct |
2 ms |
7156 KB |
Output is correct |
9 |
Correct |
2 ms |
6992 KB |
Output is correct |
10 |
Correct |
2 ms |
7248 KB |
Output is correct |
11 |
Correct |
3 ms |
7248 KB |
Output is correct |
12 |
Correct |
2 ms |
6992 KB |
Output is correct |
13 |
Correct |
3 ms |
6992 KB |
Output is correct |
14 |
Correct |
4 ms |
7248 KB |
Output is correct |
15 |
Correct |
3 ms |
7248 KB |
Output is correct |
16 |
Correct |
3 ms |
7248 KB |
Output is correct |
17 |
Correct |
4 ms |
8016 KB |
Output is correct |
18 |
Correct |
3 ms |
7248 KB |
Output is correct |
19 |
Correct |
2 ms |
7248 KB |
Output is correct |
20 |
Correct |
4 ms |
7504 KB |
Output is correct |
21 |
Correct |
4 ms |
7248 KB |
Output is correct |
22 |
Correct |
2 ms |
7248 KB |
Output is correct |
23 |
Correct |
3 ms |
7248 KB |
Output is correct |
24 |
Correct |
4 ms |
7672 KB |
Output is correct |
25 |
Correct |
3 ms |
7392 KB |
Output is correct |
26 |
Correct |
3 ms |
6992 KB |
Output is correct |
27 |
Correct |
2 ms |
6992 KB |
Output is correct |
28 |
Correct |
6 ms |
8528 KB |
Output is correct |
29 |
Correct |
2 ms |
6992 KB |
Output is correct |
30 |
Correct |
2 ms |
6992 KB |
Output is correct |
31 |
Correct |
3 ms |
7160 KB |
Output is correct |
32 |
Correct |
3 ms |
6992 KB |
Output is correct |
33 |
Correct |
4 ms |
7420 KB |
Output is correct |
34 |
Correct |
3 ms |
7248 KB |
Output is correct |
35 |
Correct |
28 ms |
13648 KB |
Output is correct |
36 |
Correct |
6 ms |
8272 KB |
Output is correct |
37 |
Correct |
53 ms |
19172 KB |
Output is correct |
38 |
Correct |
58 ms |
17736 KB |
Output is correct |
39 |
Correct |
55 ms |
17736 KB |
Output is correct |
40 |
Correct |
60 ms |
17736 KB |
Output is correct |
41 |
Correct |
66 ms |
17992 KB |
Output is correct |
42 |
Correct |
6 ms |
7248 KB |
Output is correct |
43 |
Correct |
5 ms |
6992 KB |
Output is correct |
44 |
Correct |
5 ms |
7644 KB |
Output is correct |
45 |
Correct |
20 ms |
10844 KB |
Output is correct |
46 |
Correct |
19 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6992 KB |
Output is correct |
2 |
Correct |
2 ms |
6992 KB |
Output is correct |
3 |
Correct |
2 ms |
6992 KB |
Output is correct |
4 |
Correct |
2 ms |
6992 KB |
Output is correct |
5 |
Correct |
2 ms |
6992 KB |
Output is correct |
6 |
Correct |
2 ms |
6992 KB |
Output is correct |
7 |
Correct |
2 ms |
6992 KB |
Output is correct |
8 |
Correct |
2 ms |
6992 KB |
Output is correct |
9 |
Correct |
2 ms |
6992 KB |
Output is correct |
10 |
Correct |
2 ms |
7248 KB |
Output is correct |
11 |
Correct |
3 ms |
7248 KB |
Output is correct |
12 |
Correct |
2 ms |
6992 KB |
Output is correct |
13 |
Correct |
2 ms |
6992 KB |
Output is correct |
14 |
Correct |
2 ms |
7248 KB |
Output is correct |
15 |
Correct |
3 ms |
7248 KB |
Output is correct |
16 |
Correct |
3 ms |
7248 KB |
Output is correct |
17 |
Correct |
4 ms |
8016 KB |
Output is correct |
18 |
Correct |
2 ms |
7248 KB |
Output is correct |
19 |
Correct |
2 ms |
7248 KB |
Output is correct |
20 |
Correct |
3 ms |
7504 KB |
Output is correct |
21 |
Correct |
3 ms |
7248 KB |
Output is correct |
22 |
Correct |
2 ms |
7248 KB |
Output is correct |
23 |
Correct |
4 ms |
7392 KB |
Output is correct |
24 |
Correct |
4 ms |
7760 KB |
Output is correct |
25 |
Correct |
3 ms |
7524 KB |
Output is correct |
26 |
Correct |
3 ms |
6992 KB |
Output is correct |
27 |
Correct |
3 ms |
6992 KB |
Output is correct |
28 |
Correct |
6 ms |
8528 KB |
Output is correct |
29 |
Correct |
3 ms |
6992 KB |
Output is correct |
30 |
Correct |
2 ms |
6992 KB |
Output is correct |
31 |
Correct |
2 ms |
6992 KB |
Output is correct |
32 |
Correct |
2 ms |
6992 KB |
Output is correct |
33 |
Correct |
3 ms |
7248 KB |
Output is correct |
34 |
Correct |
5 ms |
7248 KB |
Output is correct |
35 |
Correct |
25 ms |
13884 KB |
Output is correct |
36 |
Correct |
6 ms |
8160 KB |
Output is correct |
37 |
Correct |
50 ms |
19016 KB |
Output is correct |
38 |
Correct |
48 ms |
17744 KB |
Output is correct |
39 |
Correct |
46 ms |
17736 KB |
Output is correct |
40 |
Correct |
43 ms |
17912 KB |
Output is correct |
41 |
Correct |
49 ms |
17736 KB |
Output is correct |
42 |
Correct |
5 ms |
6992 KB |
Output is correct |
43 |
Correct |
5 ms |
6992 KB |
Output is correct |
44 |
Correct |
6 ms |
7504 KB |
Output is correct |
45 |
Correct |
16 ms |
10760 KB |
Output is correct |
46 |
Correct |
22 ms |
10832 KB |
Output is correct |
47 |
Correct |
240 ms |
51260 KB |
Output is correct |
48 |
Correct |
31 ms |
16844 KB |
Output is correct |
49 |
Correct |
19 ms |
12880 KB |
Output is correct |
50 |
Correct |
13 ms |
11344 KB |
Output is correct |
51 |
Correct |
49 ms |
22080 KB |
Output is correct |
52 |
Correct |
53 ms |
23424 KB |
Output is correct |
53 |
Correct |
18 ms |
11464 KB |
Output is correct |
54 |
Correct |
2 ms |
7160 KB |
Output is correct |
55 |
Correct |
2 ms |
6992 KB |
Output is correct |
56 |
Correct |
21 ms |
12504 KB |
Output is correct |
57 |
Correct |
11 ms |
9808 KB |
Output is correct |
58 |
Correct |
4 ms |
7248 KB |
Output is correct |
59 |
Correct |
6 ms |
6992 KB |
Output is correct |
60 |
Correct |
5 ms |
7248 KB |
Output is correct |
61 |
Correct |
7 ms |
7248 KB |
Output is correct |
62 |
Correct |
153 ms |
39616 KB |
Output is correct |
63 |
Correct |
18 ms |
9808 KB |
Output is correct |
64 |
Correct |
15 ms |
9296 KB |
Output is correct |
65 |
Correct |
17 ms |
9976 KB |
Output is correct |
66 |
Correct |
24 ms |
10564 KB |
Output is correct |
67 |
Correct |
32 ms |
10832 KB |
Output is correct |