#include <bits/stdc++.h>
using namespace std;
#define Task "SKY"
#define ll long long
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define F first
#define S second
typedef pair<int, int> ii;
const int N = 5e4 + 6;
const int INF = 1e9;
const int MOD = 1e9 + 7;
template<class X, class Y>
bool minimize(X &x, Y y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, Y y) {
if (x < y) {
x = y;
return true;
} else return false;
}
int n, m;
vector<set<int>> powers;
int dist[N];
vector<pair<int, int>> adj[N];
int s, e;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(Task".INP", "r")) {
freopen(Task".INP", "r", stdin);
freopen(Task".OUT", "w", stdout);
}
cin >> n >> m;
powers.resize(n + 1);
FOR(i, 0, m - 1) {
int building, power;
cin >> building >> power;
powers[building].insert(power);
if (i == 0) s = building;
if (i == 1) e = building;
}
FOR(i, 0, n - 1) {
for (int power : powers[i]) {
FOR(j, 1, (n - i) / power) {
int nxt = i + j * power;
adj[i].push_back({nxt, j});
if (powers[nxt].count(power)) break;
}
FOR(j, 1, i / power) {
int nxt = i - j * power;
if (nxt < 0) break;
adj[i].push_back({nxt, j});
if (powers[nxt].count(power)) break;
}
}
}
priority_queue<ii, vector<ii>, greater<ii>> pq;
memset(dist, 0x3f, sizeof(dist));
pq.push({0, s});
dist[s] = 0;
while(!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (u == e) {
cout << d;
return 0;
}
if (d > dist[u]) continue;
for (auto [v, w] : adj[u]) if (minimize(dist[v], dist[u] + w)) {
pq.push({dist[v], v});
}
}
cout << -1;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:44:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen(Task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:45:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen(Task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1616 KB |
Output is correct |
2 |
Correct |
2 ms |
1616 KB |
Output is correct |
3 |
Correct |
2 ms |
1616 KB |
Output is correct |
4 |
Correct |
2 ms |
1616 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
1 ms |
1616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1616 KB |
Output is correct |
2 |
Correct |
2 ms |
1616 KB |
Output is correct |
3 |
Correct |
2 ms |
1616 KB |
Output is correct |
4 |
Correct |
1 ms |
1616 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1616 KB |
Output is correct |
8 |
Correct |
1 ms |
1616 KB |
Output is correct |
9 |
Correct |
2 ms |
1616 KB |
Output is correct |
10 |
Correct |
2 ms |
1616 KB |
Output is correct |
11 |
Correct |
2 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1616 KB |
Output is correct |
13 |
Correct |
2 ms |
1616 KB |
Output is correct |
14 |
Correct |
2 ms |
1872 KB |
Output is correct |
15 |
Correct |
3 ms |
1872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1616 KB |
Output is correct |
2 |
Correct |
2 ms |
1616 KB |
Output is correct |
3 |
Correct |
2 ms |
1616 KB |
Output is correct |
4 |
Correct |
2 ms |
1616 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1616 KB |
Output is correct |
8 |
Correct |
1 ms |
1616 KB |
Output is correct |
9 |
Correct |
2 ms |
1616 KB |
Output is correct |
10 |
Correct |
2 ms |
1872 KB |
Output is correct |
11 |
Correct |
3 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1616 KB |
Output is correct |
13 |
Correct |
2 ms |
1616 KB |
Output is correct |
14 |
Correct |
3 ms |
2044 KB |
Output is correct |
15 |
Correct |
3 ms |
2040 KB |
Output is correct |
16 |
Correct |
2 ms |
1872 KB |
Output is correct |
17 |
Correct |
3 ms |
2128 KB |
Output is correct |
18 |
Correct |
2 ms |
1872 KB |
Output is correct |
19 |
Correct |
2 ms |
1872 KB |
Output is correct |
20 |
Correct |
3 ms |
1848 KB |
Output is correct |
21 |
Correct |
2 ms |
1872 KB |
Output is correct |
22 |
Correct |
2 ms |
1872 KB |
Output is correct |
23 |
Correct |
2 ms |
1872 KB |
Output is correct |
24 |
Correct |
3 ms |
2128 KB |
Output is correct |
25 |
Correct |
2 ms |
2128 KB |
Output is correct |
26 |
Correct |
2 ms |
1872 KB |
Output is correct |
27 |
Correct |
2 ms |
1872 KB |
Output is correct |
28 |
Correct |
3 ms |
2300 KB |
Output is correct |
29 |
Correct |
4 ms |
2812 KB |
Output is correct |
30 |
Correct |
3 ms |
2128 KB |
Output is correct |
31 |
Correct |
3 ms |
2384 KB |
Output is correct |
32 |
Correct |
2 ms |
2128 KB |
Output is correct |
33 |
Correct |
6 ms |
3408 KB |
Output is correct |
34 |
Correct |
6 ms |
3408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1616 KB |
Output is correct |
2 |
Correct |
2 ms |
1616 KB |
Output is correct |
3 |
Correct |
2 ms |
1616 KB |
Output is correct |
4 |
Correct |
2 ms |
1616 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
1 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1616 KB |
Output is correct |
8 |
Correct |
1 ms |
1616 KB |
Output is correct |
9 |
Correct |
1 ms |
1616 KB |
Output is correct |
10 |
Correct |
2 ms |
1616 KB |
Output is correct |
11 |
Correct |
2 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1616 KB |
Output is correct |
13 |
Correct |
2 ms |
1616 KB |
Output is correct |
14 |
Correct |
2 ms |
1872 KB |
Output is correct |
15 |
Correct |
2 ms |
1872 KB |
Output is correct |
16 |
Correct |
3 ms |
1872 KB |
Output is correct |
17 |
Correct |
2 ms |
2128 KB |
Output is correct |
18 |
Correct |
2 ms |
1872 KB |
Output is correct |
19 |
Correct |
2 ms |
1872 KB |
Output is correct |
20 |
Correct |
2 ms |
1872 KB |
Output is correct |
21 |
Correct |
2 ms |
1872 KB |
Output is correct |
22 |
Correct |
2 ms |
1872 KB |
Output is correct |
23 |
Correct |
3 ms |
1872 KB |
Output is correct |
24 |
Correct |
3 ms |
2128 KB |
Output is correct |
25 |
Correct |
3 ms |
2128 KB |
Output is correct |
26 |
Correct |
2 ms |
1872 KB |
Output is correct |
27 |
Correct |
2 ms |
1792 KB |
Output is correct |
28 |
Correct |
3 ms |
2128 KB |
Output is correct |
29 |
Correct |
4 ms |
2640 KB |
Output is correct |
30 |
Correct |
4 ms |
2004 KB |
Output is correct |
31 |
Correct |
3 ms |
2552 KB |
Output is correct |
32 |
Correct |
3 ms |
2128 KB |
Output is correct |
33 |
Correct |
6 ms |
3408 KB |
Output is correct |
34 |
Correct |
5 ms |
3408 KB |
Output is correct |
35 |
Correct |
14 ms |
3920 KB |
Output is correct |
36 |
Correct |
3 ms |
2128 KB |
Output is correct |
37 |
Correct |
20 ms |
4964 KB |
Output is correct |
38 |
Correct |
18 ms |
4956 KB |
Output is correct |
39 |
Correct |
19 ms |
4856 KB |
Output is correct |
40 |
Correct |
18 ms |
4944 KB |
Output is correct |
41 |
Correct |
19 ms |
5144 KB |
Output is correct |
42 |
Correct |
5 ms |
2128 KB |
Output is correct |
43 |
Correct |
5 ms |
1872 KB |
Output is correct |
44 |
Correct |
5 ms |
2128 KB |
Output is correct |
45 |
Correct |
26 ms |
8016 KB |
Output is correct |
46 |
Correct |
22 ms |
7756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1616 KB |
Output is correct |
2 |
Correct |
1 ms |
1616 KB |
Output is correct |
3 |
Correct |
1 ms |
1616 KB |
Output is correct |
4 |
Correct |
2 ms |
1616 KB |
Output is correct |
5 |
Correct |
2 ms |
1616 KB |
Output is correct |
6 |
Correct |
2 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1616 KB |
Output is correct |
8 |
Correct |
1 ms |
1792 KB |
Output is correct |
9 |
Correct |
2 ms |
1616 KB |
Output is correct |
10 |
Correct |
2 ms |
1616 KB |
Output is correct |
11 |
Correct |
2 ms |
1872 KB |
Output is correct |
12 |
Correct |
2 ms |
1616 KB |
Output is correct |
13 |
Correct |
2 ms |
1616 KB |
Output is correct |
14 |
Correct |
4 ms |
1872 KB |
Output is correct |
15 |
Correct |
2 ms |
1872 KB |
Output is correct |
16 |
Correct |
2 ms |
1872 KB |
Output is correct |
17 |
Correct |
3 ms |
2080 KB |
Output is correct |
18 |
Correct |
2 ms |
1872 KB |
Output is correct |
19 |
Correct |
2 ms |
1924 KB |
Output is correct |
20 |
Correct |
2 ms |
1872 KB |
Output is correct |
21 |
Correct |
2 ms |
1872 KB |
Output is correct |
22 |
Correct |
2 ms |
1872 KB |
Output is correct |
23 |
Correct |
2 ms |
1872 KB |
Output is correct |
24 |
Correct |
2 ms |
2128 KB |
Output is correct |
25 |
Correct |
2 ms |
2044 KB |
Output is correct |
26 |
Correct |
2 ms |
1872 KB |
Output is correct |
27 |
Correct |
2 ms |
2040 KB |
Output is correct |
28 |
Correct |
3 ms |
2128 KB |
Output is correct |
29 |
Correct |
4 ms |
2640 KB |
Output is correct |
30 |
Correct |
3 ms |
2128 KB |
Output is correct |
31 |
Correct |
3 ms |
2384 KB |
Output is correct |
32 |
Correct |
3 ms |
2128 KB |
Output is correct |
33 |
Correct |
8 ms |
3408 KB |
Output is correct |
34 |
Correct |
5 ms |
3408 KB |
Output is correct |
35 |
Correct |
14 ms |
3920 KB |
Output is correct |
36 |
Correct |
3 ms |
2300 KB |
Output is correct |
37 |
Correct |
15 ms |
4688 KB |
Output is correct |
38 |
Correct |
17 ms |
4944 KB |
Output is correct |
39 |
Correct |
17 ms |
4824 KB |
Output is correct |
40 |
Correct |
24 ms |
4944 KB |
Output is correct |
41 |
Correct |
18 ms |
4936 KB |
Output is correct |
42 |
Correct |
4 ms |
1872 KB |
Output is correct |
43 |
Correct |
4 ms |
1872 KB |
Output is correct |
44 |
Correct |
5 ms |
2060 KB |
Output is correct |
45 |
Correct |
19 ms |
8016 KB |
Output is correct |
46 |
Correct |
20 ms |
7760 KB |
Output is correct |
47 |
Correct |
34 ms |
12684 KB |
Output is correct |
48 |
Correct |
14 ms |
6360 KB |
Output is correct |
49 |
Correct |
12 ms |
5436 KB |
Output is correct |
50 |
Correct |
10 ms |
5200 KB |
Output is correct |
51 |
Correct |
28 ms |
8796 KB |
Output is correct |
52 |
Correct |
25 ms |
9292 KB |
Output is correct |
53 |
Correct |
11 ms |
5588 KB |
Output is correct |
54 |
Correct |
4 ms |
3664 KB |
Output is correct |
55 |
Correct |
5 ms |
4064 KB |
Output is correct |
56 |
Correct |
10 ms |
5456 KB |
Output is correct |
57 |
Correct |
17 ms |
11856 KB |
Output is correct |
58 |
Correct |
9 ms |
4468 KB |
Output is correct |
59 |
Correct |
9 ms |
4456 KB |
Output is correct |
60 |
Correct |
10 ms |
5324 KB |
Output is correct |
61 |
Correct |
10 ms |
4560 KB |
Output is correct |
62 |
Correct |
17 ms |
9728 KB |
Output is correct |
63 |
Correct |
66 ms |
28348 KB |
Output is correct |
64 |
Correct |
70 ms |
31260 KB |
Output is correct |
65 |
Correct |
91 ms |
41148 KB |
Output is correct |
66 |
Correct |
161 ms |
71732 KB |
Output is correct |
67 |
Correct |
142 ms |
71988 KB |
Output is correct |