#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 3e4;
const int inf = 1e9;
vector<pii> edges[MAXN];
set<int> at[MAXN];
int n, m;
int dist[MAXN];
void dijkstra(int s) {
fill(dist, dist+n, inf);
dist[s] = 0;
priority_queue<pii> pq;
pq.push(pii(0, s));
while (!pq.empty()) {
pii p = pq.top();
pq.pop();
int cur = p.second;
if (dist[cur] < p.first) continue;
for (pii e: edges[cur]) {
if (dist[cur]+e.second < dist[e.first]) {
dist[e.first] = dist[cur]+e.second;
pq.push(pii(dist[e.first], e.first));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> m;
int posf;
int poss;
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
if (i == 1) posf = x;
if (i == 0) poss = x;
at[x].insert(y);
}
for (int i = n-1; i >= 0; i--) {
for (int p: at[i]) {
for (int j = i+p; j < n; j += p) {
edges[i].push_back(pii(j, (j-i)/p));
if (at[j].find(p) != at[j].end()) break;
}
}
}
for (int i = 0; i < n; i++) {
for (int p: at[i]) {
for (int j = i-p; j >= 0; j -= p) {
edges[i].push_back(pii(j, (i-j)/p));
if (at[j].find(p) != at[j].end()) break;
}
}
}
dijkstra(poss);
cout << ((dist[posf] == inf) ? (-1) : dist[posf]) << "\n";
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:60:10: warning: 'poss' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | dijkstra(poss);
| ~~~~~~~~^~~~~~
skyscraper.cpp:61:21: warning: 'posf' may be used uninitialized in this function [-Wmaybe-uninitialized]
61 | cout << ((dist[posf] == inf) ? (-1) : dist[posf]) << "\n";
| ~~~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2444 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2440 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2576 KB |
Output is correct |
16 |
Correct |
2 ms |
2516 KB |
Output is correct |
17 |
Correct |
18 ms |
2728 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2448 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
6 ms |
2516 KB |
Output is correct |
24 |
Correct |
26 ms |
2644 KB |
Output is correct |
25 |
Correct |
5 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
4 ms |
2716 KB |
Output is correct |
29 |
Correct |
5 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
2644 KB |
Output is correct |
31 |
Correct |
4 ms |
2832 KB |
Output is correct |
32 |
Correct |
4 ms |
2644 KB |
Output is correct |
33 |
Correct |
5 ms |
3924 KB |
Output is correct |
34 |
Correct |
6 ms |
4052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2440 KB |
Output is correct |
11 |
Correct |
4 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
3 ms |
2388 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2516 KB |
Output is correct |
17 |
Correct |
19 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2584 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
6 ms |
2516 KB |
Output is correct |
24 |
Correct |
34 ms |
2708 KB |
Output is correct |
25 |
Correct |
6 ms |
2660 KB |
Output is correct |
26 |
Correct |
3 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
4 ms |
3156 KB |
Output is correct |
30 |
Correct |
3 ms |
2644 KB |
Output is correct |
31 |
Correct |
4 ms |
2828 KB |
Output is correct |
32 |
Correct |
4 ms |
2644 KB |
Output is correct |
33 |
Correct |
6 ms |
3924 KB |
Output is correct |
34 |
Correct |
6 ms |
3924 KB |
Output is correct |
35 |
Correct |
108 ms |
4876 KB |
Output is correct |
36 |
Correct |
27 ms |
2772 KB |
Output is correct |
37 |
Correct |
193 ms |
5568 KB |
Output is correct |
38 |
Correct |
225 ms |
5852 KB |
Output is correct |
39 |
Correct |
210 ms |
5856 KB |
Output is correct |
40 |
Correct |
254 ms |
5844 KB |
Output is correct |
41 |
Correct |
287 ms |
5832 KB |
Output is correct |
42 |
Correct |
6 ms |
2712 KB |
Output is correct |
43 |
Correct |
6 ms |
2772 KB |
Output is correct |
44 |
Correct |
5 ms |
2716 KB |
Output is correct |
45 |
Correct |
28 ms |
8432 KB |
Output is correct |
46 |
Correct |
22 ms |
8368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2416 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
3 ms |
2440 KB |
Output is correct |
10 |
Correct |
3 ms |
2444 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2516 KB |
Output is correct |
17 |
Correct |
19 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2452 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2452 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
6 ms |
2516 KB |
Output is correct |
24 |
Correct |
28 ms |
2644 KB |
Output is correct |
25 |
Correct |
7 ms |
2584 KB |
Output is correct |
26 |
Correct |
3 ms |
2584 KB |
Output is correct |
27 |
Correct |
2 ms |
2480 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
5 ms |
3216 KB |
Output is correct |
30 |
Correct |
3 ms |
2644 KB |
Output is correct |
31 |
Correct |
4 ms |
2772 KB |
Output is correct |
32 |
Correct |
4 ms |
2644 KB |
Output is correct |
33 |
Correct |
5 ms |
3924 KB |
Output is correct |
34 |
Correct |
5 ms |
3924 KB |
Output is correct |
35 |
Correct |
110 ms |
4880 KB |
Output is correct |
36 |
Correct |
24 ms |
2772 KB |
Output is correct |
37 |
Correct |
180 ms |
5548 KB |
Output is correct |
38 |
Correct |
223 ms |
5868 KB |
Output is correct |
39 |
Correct |
214 ms |
5844 KB |
Output is correct |
40 |
Correct |
297 ms |
5844 KB |
Output is correct |
41 |
Correct |
257 ms |
5844 KB |
Output is correct |
42 |
Correct |
6 ms |
2788 KB |
Output is correct |
43 |
Correct |
8 ms |
2708 KB |
Output is correct |
44 |
Correct |
8 ms |
2716 KB |
Output is correct |
45 |
Correct |
29 ms |
8376 KB |
Output is correct |
46 |
Correct |
22 ms |
8348 KB |
Output is correct |
47 |
Execution timed out |
1074 ms |
12624 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |