#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,int> ii;
const int N = 3e4;
const ll INF = 1e18;
int n, m;
int b[N+2], p[N+2];
ll dis[N+2];
vector<int> adj[N+2];
unordered_map<int,bool> lf[N+2], rg[N+2];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
adj[b[i]].push_back(p[i]);
}
for (int i = 0; i < n; i++) {
sort(adj[i].begin(),adj[i].end());
adj[i].resize(unique(adj[i].begin(),adj[i].end())-adj[i].begin());
}
for (int i = 0; i < n; i++) dis[i] = INF;
priority_queue<ii,vector<ii>,greater<ii>> pq;
pq.push({0,b[0]}); dis[b[0]] = 0;
while (!pq.empty()) {
ii cur = pq.top(); pq.pop();
ll dist = cur.fi, node = cur.se;
if (node == b[1]) {
cout << dist << '\n';
return 0;
}
if (dis[node] != dist) continue;
for (int i : adj[node]) {
if (!lf[node][i]) {
for (int j = node-i, k = 1; j >= 0; j -= i, k++) {
ll nxDist = dist+k, nxNode = j;
if (dis[nxNode] > nxDist) {
dis[nxNode] = nxDist;
pq.push({nxDist,nxNode});
}
int p = lower_bound(adj[nxNode].begin(),adj[nxNode].end(),i)-adj[nxNode].begin();
if (p < adj[nxNode].size() && adj[nxNode][p] == i) {
lf[nxNode][i] = 1;
rg[nxNode][i] = 1;
}
}
}
if (!rg[node][i]) {
for (int j = node+i, k = 1; j < n; j += i, k++) {
ll nxDist = dist+k, nxNode = j;
if (dis[nxNode] > nxDist) {
dis[nxNode] = nxDist;
pq.push({nxDist,nxNode});
}
int p = lower_bound(adj[nxNode].begin(),adj[nxNode].end(),i)-adj[nxNode].begin();
if (p < adj[nxNode].size() && adj[nxNode][p] == i) {
lf[nxNode][i] = 1;
rg[nxNode][i] = 1;
}
}
}
}
}
cout << "-1\n";
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if (p < adj[nxNode].size() && adj[nxNode][p] == i) {
| ~~^~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:63:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if (p < adj[nxNode].size() && adj[nxNode][p] == i) {
| ~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4332 KB |
Output is correct |
2 |
Correct |
4 ms |
4332 KB |
Output is correct |
3 |
Correct |
4 ms |
4332 KB |
Output is correct |
4 |
Correct |
5 ms |
4332 KB |
Output is correct |
5 |
Correct |
4 ms |
4332 KB |
Output is correct |
6 |
Correct |
4 ms |
4332 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4332 KB |
Output is correct |
2 |
Correct |
4 ms |
4332 KB |
Output is correct |
3 |
Correct |
4 ms |
4332 KB |
Output is correct |
4 |
Correct |
4 ms |
4332 KB |
Output is correct |
5 |
Correct |
4 ms |
4332 KB |
Output is correct |
6 |
Correct |
5 ms |
4332 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
5 ms |
4332 KB |
Output is correct |
9 |
Correct |
5 ms |
4332 KB |
Output is correct |
10 |
Correct |
5 ms |
4332 KB |
Output is correct |
11 |
Correct |
7 ms |
4460 KB |
Output is correct |
12 |
Correct |
4 ms |
4332 KB |
Output is correct |
13 |
Correct |
5 ms |
4460 KB |
Output is correct |
14 |
Correct |
5 ms |
4588 KB |
Output is correct |
15 |
Correct |
8 ms |
4588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4332 KB |
Output is correct |
2 |
Correct |
4 ms |
4332 KB |
Output is correct |
3 |
Correct |
4 ms |
4332 KB |
Output is correct |
4 |
Correct |
4 ms |
4332 KB |
Output is correct |
5 |
Correct |
4 ms |
4332 KB |
Output is correct |
6 |
Correct |
4 ms |
4332 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
4 ms |
4332 KB |
Output is correct |
9 |
Correct |
4 ms |
4332 KB |
Output is correct |
10 |
Correct |
4 ms |
4332 KB |
Output is correct |
11 |
Correct |
5 ms |
4460 KB |
Output is correct |
12 |
Correct |
4 ms |
4332 KB |
Output is correct |
13 |
Correct |
5 ms |
4460 KB |
Output is correct |
14 |
Correct |
5 ms |
4588 KB |
Output is correct |
15 |
Correct |
5 ms |
4588 KB |
Output is correct |
16 |
Correct |
5 ms |
4332 KB |
Output is correct |
17 |
Correct |
7 ms |
4588 KB |
Output is correct |
18 |
Correct |
4 ms |
4460 KB |
Output is correct |
19 |
Correct |
5 ms |
4332 KB |
Output is correct |
20 |
Correct |
6 ms |
4716 KB |
Output is correct |
21 |
Correct |
4 ms |
4332 KB |
Output is correct |
22 |
Correct |
4 ms |
4332 KB |
Output is correct |
23 |
Correct |
5 ms |
4460 KB |
Output is correct |
24 |
Correct |
6 ms |
4588 KB |
Output is correct |
25 |
Correct |
5 ms |
4460 KB |
Output is correct |
26 |
Correct |
5 ms |
4460 KB |
Output is correct |
27 |
Correct |
6 ms |
4460 KB |
Output is correct |
28 |
Correct |
6 ms |
4716 KB |
Output is correct |
29 |
Correct |
7 ms |
4724 KB |
Output is correct |
30 |
Correct |
5 ms |
4588 KB |
Output is correct |
31 |
Correct |
6 ms |
4588 KB |
Output is correct |
32 |
Correct |
5 ms |
4588 KB |
Output is correct |
33 |
Correct |
9 ms |
5104 KB |
Output is correct |
34 |
Correct |
7 ms |
5104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4332 KB |
Output is correct |
2 |
Correct |
5 ms |
4332 KB |
Output is correct |
3 |
Correct |
4 ms |
4332 KB |
Output is correct |
4 |
Correct |
5 ms |
4464 KB |
Output is correct |
5 |
Correct |
5 ms |
4332 KB |
Output is correct |
6 |
Correct |
4 ms |
4332 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
4 ms |
4332 KB |
Output is correct |
9 |
Correct |
4 ms |
4332 KB |
Output is correct |
10 |
Correct |
4 ms |
4332 KB |
Output is correct |
11 |
Correct |
4 ms |
4460 KB |
Output is correct |
12 |
Correct |
4 ms |
4332 KB |
Output is correct |
13 |
Correct |
4 ms |
4352 KB |
Output is correct |
14 |
Correct |
6 ms |
4588 KB |
Output is correct |
15 |
Correct |
5 ms |
4588 KB |
Output is correct |
16 |
Correct |
4 ms |
4332 KB |
Output is correct |
17 |
Correct |
5 ms |
4588 KB |
Output is correct |
18 |
Correct |
5 ms |
4480 KB |
Output is correct |
19 |
Correct |
4 ms |
4332 KB |
Output is correct |
20 |
Correct |
6 ms |
4716 KB |
Output is correct |
21 |
Correct |
4 ms |
4332 KB |
Output is correct |
22 |
Correct |
4 ms |
4332 KB |
Output is correct |
23 |
Correct |
6 ms |
4460 KB |
Output is correct |
24 |
Correct |
7 ms |
4588 KB |
Output is correct |
25 |
Correct |
5 ms |
4460 KB |
Output is correct |
26 |
Correct |
4 ms |
4460 KB |
Output is correct |
27 |
Correct |
5 ms |
4460 KB |
Output is correct |
28 |
Correct |
6 ms |
4716 KB |
Output is correct |
29 |
Correct |
7 ms |
4724 KB |
Output is correct |
30 |
Correct |
5 ms |
4588 KB |
Output is correct |
31 |
Correct |
6 ms |
4588 KB |
Output is correct |
32 |
Correct |
5 ms |
4588 KB |
Output is correct |
33 |
Correct |
9 ms |
5104 KB |
Output is correct |
34 |
Correct |
7 ms |
5104 KB |
Output is correct |
35 |
Correct |
18 ms |
6912 KB |
Output is correct |
36 |
Correct |
6 ms |
4588 KB |
Output is correct |
37 |
Correct |
20 ms |
5996 KB |
Output is correct |
38 |
Correct |
16 ms |
5868 KB |
Output is correct |
39 |
Correct |
12 ms |
5100 KB |
Output is correct |
40 |
Correct |
11 ms |
5100 KB |
Output is correct |
41 |
Correct |
17 ms |
5484 KB |
Output is correct |
42 |
Correct |
9 ms |
5120 KB |
Output is correct |
43 |
Correct |
9 ms |
5100 KB |
Output is correct |
44 |
Correct |
10 ms |
5228 KB |
Output is correct |
45 |
Correct |
22 ms |
7908 KB |
Output is correct |
46 |
Correct |
23 ms |
7908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4332 KB |
Output is correct |
2 |
Correct |
4 ms |
4332 KB |
Output is correct |
3 |
Correct |
4 ms |
4332 KB |
Output is correct |
4 |
Correct |
4 ms |
4332 KB |
Output is correct |
5 |
Correct |
4 ms |
4332 KB |
Output is correct |
6 |
Correct |
4 ms |
4332 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
4 ms |
4332 KB |
Output is correct |
9 |
Correct |
4 ms |
4332 KB |
Output is correct |
10 |
Correct |
4 ms |
4332 KB |
Output is correct |
11 |
Correct |
5 ms |
4460 KB |
Output is correct |
12 |
Correct |
4 ms |
4332 KB |
Output is correct |
13 |
Correct |
4 ms |
4460 KB |
Output is correct |
14 |
Correct |
5 ms |
4588 KB |
Output is correct |
15 |
Correct |
5 ms |
4588 KB |
Output is correct |
16 |
Correct |
5 ms |
4588 KB |
Output is correct |
17 |
Correct |
6 ms |
4588 KB |
Output is correct |
18 |
Correct |
4 ms |
4460 KB |
Output is correct |
19 |
Correct |
4 ms |
4460 KB |
Output is correct |
20 |
Correct |
5 ms |
4716 KB |
Output is correct |
21 |
Correct |
4 ms |
4332 KB |
Output is correct |
22 |
Correct |
5 ms |
4332 KB |
Output is correct |
23 |
Correct |
5 ms |
4460 KB |
Output is correct |
24 |
Correct |
6 ms |
4716 KB |
Output is correct |
25 |
Correct |
5 ms |
4468 KB |
Output is correct |
26 |
Correct |
4 ms |
4460 KB |
Output is correct |
27 |
Correct |
4 ms |
4460 KB |
Output is correct |
28 |
Correct |
6 ms |
4716 KB |
Output is correct |
29 |
Correct |
6 ms |
4724 KB |
Output is correct |
30 |
Correct |
5 ms |
4588 KB |
Output is correct |
31 |
Correct |
6 ms |
4588 KB |
Output is correct |
32 |
Correct |
5 ms |
4588 KB |
Output is correct |
33 |
Correct |
9 ms |
5104 KB |
Output is correct |
34 |
Correct |
7 ms |
5104 KB |
Output is correct |
35 |
Correct |
19 ms |
6892 KB |
Output is correct |
36 |
Correct |
6 ms |
4588 KB |
Output is correct |
37 |
Correct |
16 ms |
5996 KB |
Output is correct |
38 |
Correct |
15 ms |
5868 KB |
Output is correct |
39 |
Correct |
11 ms |
5100 KB |
Output is correct |
40 |
Correct |
12 ms |
5228 KB |
Output is correct |
41 |
Correct |
13 ms |
5484 KB |
Output is correct |
42 |
Correct |
9 ms |
5100 KB |
Output is correct |
43 |
Correct |
11 ms |
5100 KB |
Output is correct |
44 |
Correct |
10 ms |
5228 KB |
Output is correct |
45 |
Correct |
22 ms |
7908 KB |
Output is correct |
46 |
Correct |
21 ms |
7908 KB |
Output is correct |
47 |
Correct |
23 ms |
6752 KB |
Output is correct |
48 |
Correct |
11 ms |
5356 KB |
Output is correct |
49 |
Correct |
15 ms |
5356 KB |
Output is correct |
50 |
Correct |
9 ms |
5228 KB |
Output is correct |
51 |
Correct |
36 ms |
8672 KB |
Output is correct |
52 |
Correct |
37 ms |
8748 KB |
Output is correct |
53 |
Correct |
16 ms |
6376 KB |
Output is correct |
54 |
Correct |
6 ms |
4976 KB |
Output is correct |
55 |
Correct |
6 ms |
4976 KB |
Output is correct |
56 |
Correct |
30 ms |
10344 KB |
Output is correct |
57 |
Correct |
8 ms |
5860 KB |
Output is correct |
58 |
Correct |
11 ms |
5548 KB |
Output is correct |
59 |
Correct |
13 ms |
5676 KB |
Output is correct |
60 |
Correct |
12 ms |
5676 KB |
Output is correct |
61 |
Correct |
12 ms |
5676 KB |
Output is correct |
62 |
Correct |
51 ms |
9836 KB |
Output is correct |
63 |
Correct |
60 ms |
9944 KB |
Output is correct |
64 |
Correct |
65 ms |
9940 KB |
Output is correct |
65 |
Correct |
78 ms |
10716 KB |
Output is correct |
66 |
Correct |
141 ms |
14408 KB |
Output is correct |
67 |
Correct |
103 ms |
14436 KB |
Output is correct |