#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
const int MX_N = 30000;
const int MX_M = 30000;
const int MX_P = 30000;
const int SQ = 20;
int N, M;
vector<ii> al[MX_N*(SQ+1)];
vector<int> doge[MX_M];
int dist[MX_N*(SQ+1)];
priority_queue<ii, vector<ii>, greater<ii> > pq;
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int N, M; cin >> N >> M;
FOR(x,1,SQ){
FOR(i,0,N-1){
if (i+x < N) {
al[x*N + i].emplace_back(x*N + i+x, 1);
al[x*N + i+x].emplace_back(x*N + i, 1);
//cout << "JOIN " << x << " :: " << i << " " << i+x << endl;
}
al[x*N + i].emplace_back(i, 0);
}
}
int S, T;
FOR(i,0,M-1){
int B, P; cin >> B >> P;
if (i == 0) S = B;
else if (i == 1) T = B;
if (P <= SQ) {
al[B].emplace_back(P*N + B, 0);
} else {
doge[B].push_back(P);
}
}
memset(dist,-1,sizeof dist);
dist[S] = 0;
pq.emplace(0,S);
while (!pq.empty()) {
int u = pq.top().sc, w = pq.top().fi; pq.pop();
if (w > dist[u]) continue;
//cout << "u " << u/N << " : " << u%N << " w " << w << endl;
if (u == T) break;
for (auto e : al[u]) {
int v = e.fi, w = dist[u] + e.sc;
if (dist[v] == -1 || w < dist[v]) {
dist[v] = w;
pq.emplace(dist[v], v);
}
}
if (u < N) {
for (auto p : doge[u]) {
for (int i = 1; u - i*p >= 0; ++i) {
int v = u - i*p, w = dist[u] + i;
if (dist[v] == -1 || w < dist[v]) {
dist[v] = w;
pq.emplace(dist[v], v);
}
}
for (int i = 1; u + i*p < N; ++i) {
int v = u + i*p, w = dist[u] + i;
if (dist[v] == -1 || w < dist[v]) {
dist[v] = w;
pq.emplace(dist[v], v);
}
}
}
}
}
cout << dist[T] << '\n';
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:68:9: warning: 'T' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (u == T) break;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
18296 KB |
Output is correct |
2 |
Correct |
18 ms |
18296 KB |
Output is correct |
3 |
Correct |
18 ms |
18296 KB |
Output is correct |
4 |
Correct |
19 ms |
18296 KB |
Output is correct |
5 |
Correct |
19 ms |
18296 KB |
Output is correct |
6 |
Correct |
20 ms |
18296 KB |
Output is correct |
7 |
Correct |
19 ms |
18296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
18296 KB |
Output is correct |
2 |
Correct |
19 ms |
18244 KB |
Output is correct |
3 |
Correct |
19 ms |
18268 KB |
Output is correct |
4 |
Correct |
20 ms |
18296 KB |
Output is correct |
5 |
Correct |
19 ms |
18296 KB |
Output is correct |
6 |
Correct |
20 ms |
18296 KB |
Output is correct |
7 |
Correct |
19 ms |
18296 KB |
Output is correct |
8 |
Correct |
19 ms |
18296 KB |
Output is correct |
9 |
Correct |
19 ms |
18424 KB |
Output is correct |
10 |
Correct |
21 ms |
18424 KB |
Output is correct |
11 |
Correct |
20 ms |
18424 KB |
Output is correct |
12 |
Correct |
20 ms |
18424 KB |
Output is correct |
13 |
Correct |
19 ms |
18424 KB |
Output is correct |
14 |
Correct |
20 ms |
18424 KB |
Output is correct |
15 |
Correct |
20 ms |
18424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
18268 KB |
Output is correct |
2 |
Correct |
19 ms |
18300 KB |
Output is correct |
3 |
Correct |
18 ms |
18296 KB |
Output is correct |
4 |
Correct |
20 ms |
18296 KB |
Output is correct |
5 |
Correct |
19 ms |
18296 KB |
Output is correct |
6 |
Correct |
20 ms |
18296 KB |
Output is correct |
7 |
Correct |
20 ms |
18296 KB |
Output is correct |
8 |
Correct |
19 ms |
18296 KB |
Output is correct |
9 |
Correct |
19 ms |
18296 KB |
Output is correct |
10 |
Correct |
20 ms |
18552 KB |
Output is correct |
11 |
Correct |
21 ms |
18424 KB |
Output is correct |
12 |
Correct |
20 ms |
18424 KB |
Output is correct |
13 |
Correct |
20 ms |
18424 KB |
Output is correct |
14 |
Correct |
20 ms |
18424 KB |
Output is correct |
15 |
Correct |
20 ms |
18424 KB |
Output is correct |
16 |
Correct |
21 ms |
18424 KB |
Output is correct |
17 |
Correct |
22 ms |
19064 KB |
Output is correct |
18 |
Correct |
26 ms |
19960 KB |
Output is correct |
19 |
Correct |
27 ms |
20260 KB |
Output is correct |
20 |
Correct |
28 ms |
20216 KB |
Output is correct |
21 |
Correct |
20 ms |
18680 KB |
Output is correct |
22 |
Correct |
25 ms |
19960 KB |
Output is correct |
23 |
Correct |
25 ms |
19832 KB |
Output is correct |
24 |
Correct |
26 ms |
20216 KB |
Output is correct |
25 |
Correct |
27 ms |
20344 KB |
Output is correct |
26 |
Correct |
26 ms |
20220 KB |
Output is correct |
27 |
Correct |
27 ms |
20216 KB |
Output is correct |
28 |
Correct |
27 ms |
20216 KB |
Output is correct |
29 |
Correct |
31 ms |
20216 KB |
Output is correct |
30 |
Correct |
27 ms |
20216 KB |
Output is correct |
31 |
Correct |
27 ms |
20344 KB |
Output is correct |
32 |
Correct |
26 ms |
20216 KB |
Output is correct |
33 |
Correct |
33 ms |
20344 KB |
Output is correct |
34 |
Correct |
28 ms |
20344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
18424 KB |
Output is correct |
2 |
Correct |
19 ms |
18296 KB |
Output is correct |
3 |
Correct |
21 ms |
18296 KB |
Output is correct |
4 |
Correct |
19 ms |
18296 KB |
Output is correct |
5 |
Correct |
18 ms |
18268 KB |
Output is correct |
6 |
Correct |
19 ms |
18296 KB |
Output is correct |
7 |
Correct |
19 ms |
18296 KB |
Output is correct |
8 |
Correct |
19 ms |
18296 KB |
Output is correct |
9 |
Correct |
18 ms |
18424 KB |
Output is correct |
10 |
Correct |
19 ms |
18424 KB |
Output is correct |
11 |
Correct |
19 ms |
18424 KB |
Output is correct |
12 |
Correct |
20 ms |
18424 KB |
Output is correct |
13 |
Correct |
21 ms |
18424 KB |
Output is correct |
14 |
Correct |
19 ms |
18424 KB |
Output is correct |
15 |
Correct |
19 ms |
18396 KB |
Output is correct |
16 |
Correct |
21 ms |
18552 KB |
Output is correct |
17 |
Correct |
22 ms |
19064 KB |
Output is correct |
18 |
Correct |
25 ms |
19960 KB |
Output is correct |
19 |
Correct |
27 ms |
20216 KB |
Output is correct |
20 |
Correct |
26 ms |
20188 KB |
Output is correct |
21 |
Correct |
22 ms |
18680 KB |
Output is correct |
22 |
Correct |
27 ms |
19960 KB |
Output is correct |
23 |
Correct |
26 ms |
19832 KB |
Output is correct |
24 |
Correct |
27 ms |
20216 KB |
Output is correct |
25 |
Correct |
28 ms |
20284 KB |
Output is correct |
26 |
Correct |
28 ms |
20216 KB |
Output is correct |
27 |
Correct |
27 ms |
20216 KB |
Output is correct |
28 |
Correct |
27 ms |
20216 KB |
Output is correct |
29 |
Correct |
30 ms |
20344 KB |
Output is correct |
30 |
Correct |
37 ms |
20188 KB |
Output is correct |
31 |
Correct |
29 ms |
20216 KB |
Output is correct |
32 |
Correct |
27 ms |
20216 KB |
Output is correct |
33 |
Correct |
32 ms |
20472 KB |
Output is correct |
34 |
Correct |
29 ms |
20344 KB |
Output is correct |
35 |
Correct |
32 ms |
20088 KB |
Output is correct |
36 |
Correct |
24 ms |
19448 KB |
Output is correct |
37 |
Correct |
32 ms |
20472 KB |
Output is correct |
38 |
Correct |
34 ms |
20800 KB |
Output is correct |
39 |
Correct |
34 ms |
20732 KB |
Output is correct |
40 |
Correct |
37 ms |
20700 KB |
Output is correct |
41 |
Correct |
35 ms |
20972 KB |
Output is correct |
42 |
Correct |
33 ms |
20732 KB |
Output is correct |
43 |
Correct |
33 ms |
20724 KB |
Output is correct |
44 |
Correct |
33 ms |
20764 KB |
Output is correct |
45 |
Correct |
39 ms |
20728 KB |
Output is correct |
46 |
Correct |
36 ms |
20728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
18296 KB |
Output is correct |
2 |
Correct |
19 ms |
18296 KB |
Output is correct |
3 |
Correct |
19 ms |
18296 KB |
Output is correct |
4 |
Correct |
19 ms |
18296 KB |
Output is correct |
5 |
Correct |
19 ms |
18296 KB |
Output is correct |
6 |
Correct |
20 ms |
18296 KB |
Output is correct |
7 |
Correct |
20 ms |
18296 KB |
Output is correct |
8 |
Correct |
19 ms |
18296 KB |
Output is correct |
9 |
Correct |
21 ms |
18424 KB |
Output is correct |
10 |
Correct |
39 ms |
18424 KB |
Output is correct |
11 |
Correct |
21 ms |
18424 KB |
Output is correct |
12 |
Correct |
20 ms |
18424 KB |
Output is correct |
13 |
Correct |
21 ms |
18424 KB |
Output is correct |
14 |
Correct |
20 ms |
18428 KB |
Output is correct |
15 |
Correct |
20 ms |
18424 KB |
Output is correct |
16 |
Correct |
20 ms |
18552 KB |
Output is correct |
17 |
Correct |
25 ms |
19064 KB |
Output is correct |
18 |
Correct |
27 ms |
19960 KB |
Output is correct |
19 |
Correct |
27 ms |
20216 KB |
Output is correct |
20 |
Correct |
28 ms |
20216 KB |
Output is correct |
21 |
Correct |
22 ms |
18680 KB |
Output is correct |
22 |
Correct |
27 ms |
19964 KB |
Output is correct |
23 |
Correct |
27 ms |
19860 KB |
Output is correct |
24 |
Correct |
27 ms |
20216 KB |
Output is correct |
25 |
Correct |
28 ms |
20216 KB |
Output is correct |
26 |
Correct |
27 ms |
20216 KB |
Output is correct |
27 |
Correct |
27 ms |
20216 KB |
Output is correct |
28 |
Correct |
28 ms |
20216 KB |
Output is correct |
29 |
Correct |
30 ms |
20344 KB |
Output is correct |
30 |
Correct |
28 ms |
20188 KB |
Output is correct |
31 |
Correct |
27 ms |
20344 KB |
Output is correct |
32 |
Correct |
27 ms |
20216 KB |
Output is correct |
33 |
Correct |
31 ms |
20344 KB |
Output is correct |
34 |
Correct |
29 ms |
20344 KB |
Output is correct |
35 |
Correct |
32 ms |
20152 KB |
Output is correct |
36 |
Correct |
24 ms |
19448 KB |
Output is correct |
37 |
Correct |
31 ms |
20600 KB |
Output is correct |
38 |
Correct |
75 ms |
20700 KB |
Output is correct |
39 |
Correct |
34 ms |
20728 KB |
Output is correct |
40 |
Correct |
34 ms |
20728 KB |
Output is correct |
41 |
Correct |
34 ms |
20728 KB |
Output is correct |
42 |
Correct |
32 ms |
20728 KB |
Output is correct |
43 |
Correct |
32 ms |
20696 KB |
Output is correct |
44 |
Correct |
33 ms |
20764 KB |
Output is correct |
45 |
Correct |
38 ms |
20728 KB |
Output is correct |
46 |
Correct |
35 ms |
20728 KB |
Output is correct |
47 |
Correct |
78 ms |
30840 KB |
Output is correct |
48 |
Correct |
108 ms |
40824 KB |
Output is correct |
49 |
Correct |
113 ms |
42872 KB |
Output is correct |
50 |
Correct |
128 ms |
45176 KB |
Output is correct |
51 |
Correct |
142 ms |
47880 KB |
Output is correct |
52 |
Correct |
144 ms |
47964 KB |
Output is correct |
53 |
Correct |
131 ms |
47560 KB |
Output is correct |
54 |
Correct |
129 ms |
46428 KB |
Output is correct |
55 |
Correct |
126 ms |
46456 KB |
Output is correct |
56 |
Correct |
136 ms |
47692 KB |
Output is correct |
57 |
Correct |
122 ms |
46840 KB |
Output is correct |
58 |
Correct |
129 ms |
46940 KB |
Output is correct |
59 |
Correct |
131 ms |
47096 KB |
Output is correct |
60 |
Correct |
130 ms |
47068 KB |
Output is correct |
61 |
Correct |
132 ms |
47172 KB |
Output is correct |
62 |
Correct |
142 ms |
47736 KB |
Output is correct |
63 |
Correct |
152 ms |
48112 KB |
Output is correct |
64 |
Correct |
162 ms |
48120 KB |
Output is correct |
65 |
Correct |
210 ms |
48236 KB |
Output is correct |
66 |
Correct |
257 ms |
49128 KB |
Output is correct |
67 |
Correct |
183 ms |
49104 KB |
Output is correct |