#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 9;
const ll LOGN = 21;
const ll MAXN = 3e4 + 100;
int B[MAXN], P[MAXN];
bool vis[MAXN];
vector<vector<pair<int,int>>> graph;
vector<int> dist;
set<int> places[MAXN];
int main() {
int N, M;
cin >> N >> M;
graph = vector<vector<pair<int,int>>>(N+1, vector<pair<int,int>>());
dist = vector<int>(N+1, 1e8);
for (int i = 1; i <= M; i++) {
cin >> B[i] >> P[i];
B[i]++;
places[P[i]].insert(B[i]);
}
for (int i = 1; i < MAXN; i++) {
for (auto u : places[i]) {
for (int p = u - i; p >= 1; p -= i) {
graph[u].push_back({p, (u - p) / i});
if (places[i].count(p))
break;
}
for (int p = u + i; p <= N; p += i) {
graph[u].push_back({p, (p - u) / i});
if (places[i].count(p))
break;
}
}
}
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({0, B[1]});
dist[B[1]] = 0;
while (!pq.empty()) {
int node = pq.top().s;
int d = pq.top().f;
pq.pop();
if (vis[node])
continue;
vis[node] = true;
for (auto u : graph[node]) {
if (dist[u.f] > dist[node] + u.s) {
dist[u.f] = dist[node] + u.s;
pq.push({dist[u.f], u.f});
}
}
}
cout << (dist[B[2]] >= 1e7 ? -1 : dist[B[2]]) << "\n";
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:51:7: warning: unused variable 'd' [-Wunused-variable]
51 | int d = pq.top().f;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1800 KB |
Output is correct |
8 |
Correct |
1 ms |
1880 KB |
Output is correct |
9 |
Correct |
1 ms |
1856 KB |
Output is correct |
10 |
Correct |
2 ms |
1880 KB |
Output is correct |
11 |
Correct |
2 ms |
1880 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1624 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
2 ms |
1864 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1864 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
2140 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
2136 KB |
Output is correct |
26 |
Correct |
2 ms |
1880 KB |
Output is correct |
27 |
Correct |
2 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
2136 KB |
Output is correct |
29 |
Correct |
4 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2116 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
5 ms |
3420 KB |
Output is correct |
34 |
Correct |
6 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1624 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1636 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
2 ms |
2136 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1864 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1860 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
2140 KB |
Output is correct |
18 |
Correct |
2 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1880 KB |
Output is correct |
20 |
Correct |
2 ms |
1880 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
2140 KB |
Output is correct |
26 |
Correct |
2 ms |
1868 KB |
Output is correct |
27 |
Correct |
2 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
2140 KB |
Output is correct |
29 |
Correct |
4 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
2 ms |
2384 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
5 ms |
3420 KB |
Output is correct |
34 |
Correct |
6 ms |
3420 KB |
Output is correct |
35 |
Correct |
16 ms |
4424 KB |
Output is correct |
36 |
Correct |
4 ms |
2144 KB |
Output is correct |
37 |
Correct |
15 ms |
5144 KB |
Output is correct |
38 |
Correct |
19 ms |
5464 KB |
Output is correct |
39 |
Correct |
20 ms |
5464 KB |
Output is correct |
40 |
Correct |
19 ms |
5476 KB |
Output is correct |
41 |
Correct |
19 ms |
5484 KB |
Output is correct |
42 |
Correct |
11 ms |
2656 KB |
Output is correct |
43 |
Correct |
10 ms |
2352 KB |
Output is correct |
44 |
Correct |
10 ms |
2404 KB |
Output is correct |
45 |
Correct |
23 ms |
8160 KB |
Output is correct |
46 |
Correct |
24 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1636 KB |
Output is correct |
2 |
Correct |
1 ms |
1636 KB |
Output is correct |
3 |
Correct |
1 ms |
1636 KB |
Output is correct |
4 |
Correct |
1 ms |
1632 KB |
Output is correct |
5 |
Correct |
1 ms |
1632 KB |
Output is correct |
6 |
Correct |
1 ms |
1636 KB |
Output is correct |
7 |
Correct |
1 ms |
1888 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
2136 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
2140 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
1864 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
2140 KB |
Output is correct |
26 |
Correct |
2 ms |
1884 KB |
Output is correct |
27 |
Correct |
2 ms |
1896 KB |
Output is correct |
28 |
Correct |
3 ms |
2148 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
6 ms |
3420 KB |
Output is correct |
34 |
Correct |
5 ms |
3420 KB |
Output is correct |
35 |
Correct |
15 ms |
4432 KB |
Output is correct |
36 |
Correct |
3 ms |
2136 KB |
Output is correct |
37 |
Correct |
18 ms |
5208 KB |
Output is correct |
38 |
Correct |
23 ms |
5468 KB |
Output is correct |
39 |
Correct |
20 ms |
5468 KB |
Output is correct |
40 |
Correct |
25 ms |
5596 KB |
Output is correct |
41 |
Correct |
19 ms |
5464 KB |
Output is correct |
42 |
Correct |
10 ms |
2396 KB |
Output is correct |
43 |
Correct |
10 ms |
2316 KB |
Output is correct |
44 |
Correct |
11 ms |
2396 KB |
Output is correct |
45 |
Correct |
23 ms |
8332 KB |
Output is correct |
46 |
Correct |
23 ms |
8284 KB |
Output is correct |
47 |
Correct |
42 ms |
12760 KB |
Output is correct |
48 |
Correct |
18 ms |
6104 KB |
Output is correct |
49 |
Correct |
16 ms |
5296 KB |
Output is correct |
50 |
Correct |
11 ms |
4956 KB |
Output is correct |
51 |
Correct |
34 ms |
8496 KB |
Output is correct |
52 |
Correct |
34 ms |
9112 KB |
Output is correct |
53 |
Correct |
23 ms |
6108 KB |
Output is correct |
54 |
Correct |
3 ms |
3164 KB |
Output is correct |
55 |
Correct |
4 ms |
3420 KB |
Output is correct |
56 |
Correct |
22 ms |
5468 KB |
Output is correct |
57 |
Correct |
31 ms |
11700 KB |
Output is correct |
58 |
Correct |
13 ms |
4020 KB |
Output is correct |
59 |
Correct |
14 ms |
4316 KB |
Output is correct |
60 |
Correct |
15 ms |
5024 KB |
Output is correct |
61 |
Correct |
14 ms |
4364 KB |
Output is correct |
62 |
Correct |
26 ms |
9388 KB |
Output is correct |
63 |
Correct |
70 ms |
27864 KB |
Output is correct |
64 |
Correct |
76 ms |
36468 KB |
Output is correct |
65 |
Correct |
110 ms |
46148 KB |
Output is correct |
66 |
Correct |
177 ms |
74824 KB |
Output is correct |
67 |
Correct |
195 ms |
75732 KB |
Output is correct |