#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
int n, m; cin >> n >> m;
vector <pair<int, int>> a(m);
set <int> st[n];
int target;
vector <int> d(n, INF);
priority_queue <pair<int, int>> pq;
for (int i = 0; i < m; i++){
cin >> a[i].f >> a[i].s;
if (i == 0) {
d[a[i].f] = 0;
pq.push({0, a[i].f});
}
if (i != 1) st[a[i].f].insert(a[i].s);
else target = a[i].f;
}
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n; i++){
for (auto x : st[i]){
for (int j = 1; ; j++){
int go = x * j + i;
if (go >= n || go < 0) break;
adj[i].push_back({go, j});
if (st[go].find(x) != st[go].end()) break;
}
for (int j = 1; ; j++){
int go = i - x * j;
if (go < 0 || go >= n) break;
adj[i].push_back({go, j});
if (st[go].find(x) != st[go].end()) break;
}
}
}
while (!pq.empty()){
auto pi = pq.top();
pq.pop();
int u = pi.second;
int ds = -pi.first;
if (d[u] != ds) continue;
for (auto [v, w] : adj[u]){
if (d[v] > d[u] + w){
d[v] = d[u] + w;
pq.push({-d[v], v});
}
}
}
// cout << target << "\n";
if (d[target] == INF) cout << -1 << "\n";
else cout << d[target] << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
skyscraper.cpp: In function 'void Solve()':
skyscraper.cpp:67:17: warning: 'target' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | if (d[target] == INF) cout << -1 << "\n";
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
848 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
580 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
900 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
720 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Correct |
3 ms |
1988 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1092 KB |
Output is correct |
33 |
Correct |
4 ms |
3020 KB |
Output is correct |
34 |
Correct |
4 ms |
3024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
844 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Correct |
3 ms |
2004 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
1108 KB |
Output is correct |
33 |
Correct |
4 ms |
3012 KB |
Output is correct |
34 |
Correct |
4 ms |
3024 KB |
Output is correct |
35 |
Correct |
11 ms |
4180 KB |
Output is correct |
36 |
Correct |
2 ms |
1052 KB |
Output is correct |
37 |
Correct |
13 ms |
5836 KB |
Output is correct |
38 |
Correct |
16 ms |
5968 KB |
Output is correct |
39 |
Correct |
16 ms |
5972 KB |
Output is correct |
40 |
Correct |
15 ms |
5984 KB |
Output is correct |
41 |
Correct |
15 ms |
5844 KB |
Output is correct |
42 |
Correct |
4 ms |
1232 KB |
Output is correct |
43 |
Correct |
5 ms |
1240 KB |
Output is correct |
44 |
Correct |
4 ms |
1236 KB |
Output is correct |
45 |
Correct |
17 ms |
10700 KB |
Output is correct |
46 |
Correct |
22 ms |
10696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
560 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
584 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
580 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
2 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
976 KB |
Output is correct |
29 |
Correct |
3 ms |
2004 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
4 ms |
3024 KB |
Output is correct |
34 |
Correct |
4 ms |
3024 KB |
Output is correct |
35 |
Correct |
12 ms |
4172 KB |
Output is correct |
36 |
Correct |
2 ms |
1100 KB |
Output is correct |
37 |
Correct |
13 ms |
5896 KB |
Output is correct |
38 |
Correct |
16 ms |
5964 KB |
Output is correct |
39 |
Correct |
16 ms |
5964 KB |
Output is correct |
40 |
Correct |
18 ms |
5892 KB |
Output is correct |
41 |
Correct |
16 ms |
5932 KB |
Output is correct |
42 |
Correct |
4 ms |
1236 KB |
Output is correct |
43 |
Correct |
4 ms |
1200 KB |
Output is correct |
44 |
Correct |
5 ms |
1240 KB |
Output is correct |
45 |
Correct |
22 ms |
10700 KB |
Output is correct |
46 |
Correct |
18 ms |
10636 KB |
Output is correct |
47 |
Correct |
40 ms |
20484 KB |
Output is correct |
48 |
Correct |
12 ms |
8016 KB |
Output is correct |
49 |
Correct |
10 ms |
6356 KB |
Output is correct |
50 |
Correct |
8 ms |
6244 KB |
Output is correct |
51 |
Correct |
30 ms |
11980 KB |
Output is correct |
52 |
Correct |
31 ms |
12908 KB |
Output is correct |
53 |
Correct |
13 ms |
6196 KB |
Output is correct |
54 |
Correct |
3 ms |
3272 KB |
Output is correct |
55 |
Correct |
4 ms |
3924 KB |
Output is correct |
56 |
Correct |
9 ms |
6184 KB |
Output is correct |
57 |
Correct |
40 ms |
19792 KB |
Output is correct |
58 |
Correct |
10 ms |
4812 KB |
Output is correct |
59 |
Correct |
9 ms |
5904 KB |
Output is correct |
60 |
Correct |
10 ms |
6888 KB |
Output is correct |
61 |
Correct |
10 ms |
5920 KB |
Output is correct |
62 |
Correct |
17 ms |
14048 KB |
Output is correct |
63 |
Correct |
61 ms |
44356 KB |
Output is correct |
64 |
Correct |
71 ms |
52168 KB |
Output is correct |
65 |
Correct |
91 ms |
70848 KB |
Output is correct |
66 |
Correct |
172 ms |
124400 KB |
Output is correct |
67 |
Correct |
162 ms |
124448 KB |
Output is correct |