#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T>
bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
};
template <typename T>
bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
};
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
for(int i = 0; i < v.size(); i++) {
os << v[i] << (i + 1 == v.size() ? "\n" : " ");
}
return os;
}
template <typename T>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
for(auto& e : t) {
fill_v(e, v);
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
struct edge {
ll to, cost;
};
vector<vector<edge>> G(n);
vector<set<int>> st(n);
int s, g;
for(int i = 0; i < m; i++) {
int b, p;
cin >> b >> p;
if(i == 0) {
s = b;
} else if(i == 1) {
g = b;
continue;
}
int k = 0;
if(st[b].count(p)) {
continue;
}
for(int j = b + p; j < n; j += p) {
k++;
G[b].push_back({j, k});
if(st[j].count(p)) {
break;
}
}
k = 0;
for(int j = b - p; j >= 0; j -= p) {
k++;
G[b].push_back({j, k});
if(st[j].count(p)) {
break;
}
}
st[b].insert(p);
}
priority_queue<P, vector<P>, greater<P>> q;
vector<ll> d(n, LINF);
d[s] = 0;
q.push(P(0, s));
while(!q.empty()) {
int v = q.top().second;
q.pop();
for(auto e : G[v]) {
if(d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
q.push(P(d[e.to], e.to));
}
}
}
if(d[g] == LINF) {
cout << -1 << endl;
} else {
cout << d[g] << endl;
}
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:94:8: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
d[s] = 0;
^
skyscraper.cpp:106:11: warning: 'g' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(d[g] == LINF) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
356 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
640 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
896 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
7 ms |
1280 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
896 KB |
Output is correct |
25 |
Correct |
5 ms |
896 KB |
Output is correct |
26 |
Correct |
4 ms |
768 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
1024 KB |
Output is correct |
29 |
Correct |
8 ms |
2212 KB |
Output is correct |
30 |
Correct |
6 ms |
1152 KB |
Output is correct |
31 |
Correct |
6 ms |
1380 KB |
Output is correct |
32 |
Correct |
4 ms |
1152 KB |
Output is correct |
33 |
Correct |
12 ms |
3316 KB |
Output is correct |
34 |
Correct |
10 ms |
3388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
4 ms |
1280 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
896 KB |
Output is correct |
25 |
Correct |
3 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
5 ms |
768 KB |
Output is correct |
28 |
Correct |
4 ms |
1024 KB |
Output is correct |
29 |
Correct |
7 ms |
2176 KB |
Output is correct |
30 |
Correct |
4 ms |
1152 KB |
Output is correct |
31 |
Correct |
5 ms |
1408 KB |
Output is correct |
32 |
Correct |
6 ms |
1152 KB |
Output is correct |
33 |
Correct |
10 ms |
3324 KB |
Output is correct |
34 |
Correct |
10 ms |
3388 KB |
Output is correct |
35 |
Correct |
27 ms |
4216 KB |
Output is correct |
36 |
Correct |
6 ms |
1024 KB |
Output is correct |
37 |
Correct |
40 ms |
6392 KB |
Output is correct |
38 |
Correct |
33 ms |
6236 KB |
Output is correct |
39 |
Correct |
36 ms |
6392 KB |
Output is correct |
40 |
Correct |
37 ms |
6264 KB |
Output is correct |
41 |
Correct |
43 ms |
6236 KB |
Output is correct |
42 |
Correct |
7 ms |
1024 KB |
Output is correct |
43 |
Correct |
9 ms |
1024 KB |
Output is correct |
44 |
Correct |
9 ms |
1664 KB |
Output is correct |
45 |
Correct |
50 ms |
11124 KB |
Output is correct |
46 |
Correct |
52 ms |
10996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
388 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
4 ms |
1408 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
896 KB |
Output is correct |
25 |
Correct |
4 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
896 KB |
Output is correct |
27 |
Correct |
3 ms |
768 KB |
Output is correct |
28 |
Correct |
4 ms |
1024 KB |
Output is correct |
29 |
Correct |
7 ms |
2176 KB |
Output is correct |
30 |
Correct |
4 ms |
1152 KB |
Output is correct |
31 |
Correct |
6 ms |
1408 KB |
Output is correct |
32 |
Correct |
4 ms |
1152 KB |
Output is correct |
33 |
Correct |
12 ms |
3376 KB |
Output is correct |
34 |
Correct |
13 ms |
3388 KB |
Output is correct |
35 |
Correct |
29 ms |
4240 KB |
Output is correct |
36 |
Correct |
8 ms |
1024 KB |
Output is correct |
37 |
Correct |
29 ms |
6276 KB |
Output is correct |
38 |
Correct |
39 ms |
6264 KB |
Output is correct |
39 |
Correct |
33 ms |
6392 KB |
Output is correct |
40 |
Correct |
42 ms |
6264 KB |
Output is correct |
41 |
Correct |
39 ms |
6264 KB |
Output is correct |
42 |
Correct |
11 ms |
1024 KB |
Output is correct |
43 |
Correct |
8 ms |
896 KB |
Output is correct |
44 |
Correct |
9 ms |
1664 KB |
Output is correct |
45 |
Correct |
49 ms |
11176 KB |
Output is correct |
46 |
Correct |
46 ms |
10996 KB |
Output is correct |
47 |
Correct |
95 ms |
21880 KB |
Output is correct |
48 |
Correct |
41 ms |
7808 KB |
Output is correct |
49 |
Correct |
32 ms |
6272 KB |
Output is correct |
50 |
Correct |
18 ms |
6164 KB |
Output is correct |
51 |
Correct |
83 ms |
12776 KB |
Output is correct |
52 |
Correct |
69 ms |
13104 KB |
Output is correct |
53 |
Correct |
21 ms |
6000 KB |
Output is correct |
54 |
Correct |
6 ms |
3328 KB |
Output is correct |
55 |
Correct |
8 ms |
4088 KB |
Output is correct |
56 |
Correct |
66 ms |
17312 KB |
Output is correct |
57 |
Correct |
117 ms |
38284 KB |
Output is correct |
58 |
Correct |
12 ms |
4324 KB |
Output is correct |
59 |
Correct |
14 ms |
5496 KB |
Output is correct |
60 |
Correct |
16 ms |
6408 KB |
Output is correct |
61 |
Correct |
16 ms |
5624 KB |
Output is correct |
62 |
Correct |
48 ms |
13540 KB |
Output is correct |
63 |
Correct |
189 ms |
52240 KB |
Output is correct |
64 |
Correct |
254 ms |
72368 KB |
Output is correct |
65 |
Correct |
346 ms |
96364 KB |
Output is correct |
66 |
Correct |
569 ms |
154328 KB |
Output is correct |
67 |
Correct |
587 ms |
153888 KB |
Output is correct |