#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 30000 + 10;
const int inf = 1e9;
const int T = 10;
int b[maxn], p[maxn], dp[maxn * T];
int n, m;
vector<pair<int, int> > g[maxn * T];
inline int id(int v, int x){
return v * T + x;
}
int dijkstra(int v){
for (int i = 0; i < n * T; i++)
dp[i] = inf;
dp[v] = 0;
set<pair<int, int> > s;
for (int i = 0; i < n * T; i++)
s.insert({dp[i], i});
while (!s.empty()){
int v = (*s.begin()).second;
s.erase(s.begin());
if (dp[v] == inf)
return -1;
if (v == id(b[1], 0))
return dp[v];
if (v % T != 0){
int i = v / T, j = v % T;
g[id(i, j)].push_back({id(i, 0), 0});
if (i + j < n)
g[id(i, j)].push_back({id(i + j, j), 1});
if (i - j >= 0)
g[id(i, j)].push_back({id(i - j, j), 1});
}
for (auto edge : g[v]){
int u = edge.first, w = edge.second;
if (dp[u] > dp[v] + w){
s.erase({dp[u], u});
dp[u] = dp[v] + w;
s.insert({dp[u], u});
}
}
g[v].clear();
}
}
int main(){
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++){
cin >> b[i] >> p[i];
if (p[i] < T)
g[id(b[i], 0)].push_back({id(b[i], p[i]), 0});
else{
for (int u = b[i] + p[i]; u < n; u += p[i])
g[id(b[i], 0)].push_back({id(u, 0), (u - b[i]) / p[i]});
for (int u = b[i] - p[i]; u >= 0; u -= p[i])
g[id(b[i], 0)].push_back({id(u, 0), (b[i] - u) / p[i]});
}
}
cout << dijkstra(id(b[0], 0)) << '\n';
}
Compilation message
skyscraper.cpp: In function 'int dijkstra(int)':
skyscraper.cpp:50:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Correct |
11 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7396 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
8 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
8 ms |
7424 KB |
Output is correct |
9 |
Correct |
7 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7552 KB |
Output is correct |
12 |
Correct |
9 ms |
7424 KB |
Output is correct |
13 |
Correct |
9 ms |
7552 KB |
Output is correct |
14 |
Correct |
11 ms |
7524 KB |
Output is correct |
15 |
Correct |
9 ms |
7552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Correct |
9 ms |
7424 KB |
Output is correct |
4 |
Correct |
11 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7424 KB |
Output is correct |
6 |
Correct |
10 ms |
7380 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
11 ms |
7424 KB |
Output is correct |
9 |
Correct |
10 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
11 ms |
7552 KB |
Output is correct |
12 |
Correct |
11 ms |
7424 KB |
Output is correct |
13 |
Correct |
11 ms |
7552 KB |
Output is correct |
14 |
Correct |
11 ms |
7552 KB |
Output is correct |
15 |
Correct |
11 ms |
7552 KB |
Output is correct |
16 |
Correct |
9 ms |
7580 KB |
Output is correct |
17 |
Correct |
13 ms |
7936 KB |
Output is correct |
18 |
Correct |
13 ms |
8320 KB |
Output is correct |
19 |
Correct |
13 ms |
8448 KB |
Output is correct |
20 |
Correct |
13 ms |
8448 KB |
Output is correct |
21 |
Correct |
10 ms |
7680 KB |
Output is correct |
22 |
Correct |
14 ms |
8272 KB |
Output is correct |
23 |
Correct |
16 ms |
8192 KB |
Output is correct |
24 |
Correct |
17 ms |
8448 KB |
Output is correct |
25 |
Correct |
14 ms |
8448 KB |
Output is correct |
26 |
Correct |
16 ms |
8448 KB |
Output is correct |
27 |
Correct |
15 ms |
8448 KB |
Output is correct |
28 |
Correct |
19 ms |
8576 KB |
Output is correct |
29 |
Correct |
20 ms |
8960 KB |
Output is correct |
30 |
Correct |
16 ms |
8576 KB |
Output is correct |
31 |
Correct |
17 ms |
8704 KB |
Output is correct |
32 |
Correct |
20 ms |
8576 KB |
Output is correct |
33 |
Correct |
28 ms |
9528 KB |
Output is correct |
34 |
Correct |
23 ms |
9472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
9 ms |
7424 KB |
Output is correct |
3 |
Correct |
10 ms |
7424 KB |
Output is correct |
4 |
Correct |
10 ms |
7424 KB |
Output is correct |
5 |
Correct |
10 ms |
7424 KB |
Output is correct |
6 |
Correct |
9 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
10 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
9 ms |
7552 KB |
Output is correct |
11 |
Correct |
11 ms |
7552 KB |
Output is correct |
12 |
Correct |
9 ms |
7424 KB |
Output is correct |
13 |
Correct |
9 ms |
7552 KB |
Output is correct |
14 |
Correct |
10 ms |
7552 KB |
Output is correct |
15 |
Correct |
9 ms |
7552 KB |
Output is correct |
16 |
Correct |
9 ms |
7552 KB |
Output is correct |
17 |
Correct |
12 ms |
7936 KB |
Output is correct |
18 |
Correct |
15 ms |
8320 KB |
Output is correct |
19 |
Correct |
14 ms |
8448 KB |
Output is correct |
20 |
Correct |
14 ms |
8448 KB |
Output is correct |
21 |
Correct |
12 ms |
7552 KB |
Output is correct |
22 |
Correct |
13 ms |
8320 KB |
Output is correct |
23 |
Correct |
15 ms |
8320 KB |
Output is correct |
24 |
Correct |
18 ms |
8448 KB |
Output is correct |
25 |
Correct |
18 ms |
8448 KB |
Output is correct |
26 |
Correct |
15 ms |
8624 KB |
Output is correct |
27 |
Correct |
14 ms |
8448 KB |
Output is correct |
28 |
Correct |
23 ms |
8704 KB |
Output is correct |
29 |
Correct |
21 ms |
9088 KB |
Output is correct |
30 |
Correct |
16 ms |
8576 KB |
Output is correct |
31 |
Correct |
15 ms |
8704 KB |
Output is correct |
32 |
Correct |
15 ms |
8576 KB |
Output is correct |
33 |
Correct |
24 ms |
9472 KB |
Output is correct |
34 |
Correct |
19 ms |
9472 KB |
Output is correct |
35 |
Correct |
22 ms |
9216 KB |
Output is correct |
36 |
Correct |
13 ms |
8064 KB |
Output is correct |
37 |
Correct |
26 ms |
10492 KB |
Output is correct |
38 |
Correct |
27 ms |
10192 KB |
Output is correct |
39 |
Correct |
22 ms |
10240 KB |
Output is correct |
40 |
Correct |
25 ms |
10240 KB |
Output is correct |
41 |
Correct |
29 ms |
10136 KB |
Output is correct |
42 |
Correct |
22 ms |
9084 KB |
Output is correct |
43 |
Correct |
22 ms |
8960 KB |
Output is correct |
44 |
Correct |
20 ms |
9032 KB |
Output is correct |
45 |
Correct |
35 ms |
12920 KB |
Output is correct |
46 |
Correct |
31 ms |
12968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7396 KB |
Output is correct |
3 |
Correct |
10 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
10 ms |
7424 KB |
Output is correct |
6 |
Correct |
8 ms |
7424 KB |
Output is correct |
7 |
Correct |
9 ms |
7424 KB |
Output is correct |
8 |
Correct |
8 ms |
7424 KB |
Output is correct |
9 |
Correct |
9 ms |
7424 KB |
Output is correct |
10 |
Correct |
9 ms |
7424 KB |
Output is correct |
11 |
Correct |
10 ms |
7552 KB |
Output is correct |
12 |
Correct |
12 ms |
7424 KB |
Output is correct |
13 |
Correct |
11 ms |
7552 KB |
Output is correct |
14 |
Correct |
12 ms |
7524 KB |
Output is correct |
15 |
Correct |
10 ms |
7552 KB |
Output is correct |
16 |
Correct |
10 ms |
7552 KB |
Output is correct |
17 |
Correct |
11 ms |
7936 KB |
Output is correct |
18 |
Correct |
13 ms |
8320 KB |
Output is correct |
19 |
Correct |
14 ms |
8448 KB |
Output is correct |
20 |
Correct |
21 ms |
8576 KB |
Output is correct |
21 |
Correct |
11 ms |
7680 KB |
Output is correct |
22 |
Correct |
15 ms |
8320 KB |
Output is correct |
23 |
Correct |
13 ms |
8320 KB |
Output is correct |
24 |
Correct |
16 ms |
8448 KB |
Output is correct |
25 |
Correct |
13 ms |
8448 KB |
Output is correct |
26 |
Correct |
16 ms |
8576 KB |
Output is correct |
27 |
Correct |
17 ms |
8448 KB |
Output is correct |
28 |
Correct |
20 ms |
8576 KB |
Output is correct |
29 |
Correct |
18 ms |
8960 KB |
Output is correct |
30 |
Correct |
15 ms |
8616 KB |
Output is correct |
31 |
Correct |
20 ms |
8704 KB |
Output is correct |
32 |
Correct |
16 ms |
8576 KB |
Output is correct |
33 |
Correct |
28 ms |
9396 KB |
Output is correct |
34 |
Correct |
21 ms |
9472 KB |
Output is correct |
35 |
Correct |
25 ms |
9216 KB |
Output is correct |
36 |
Correct |
14 ms |
8064 KB |
Output is correct |
37 |
Correct |
39 ms |
10500 KB |
Output is correct |
38 |
Correct |
31 ms |
10360 KB |
Output is correct |
39 |
Correct |
29 ms |
10232 KB |
Output is correct |
40 |
Correct |
88 ms |
10104 KB |
Output is correct |
41 |
Correct |
31 ms |
10104 KB |
Output is correct |
42 |
Correct |
23 ms |
8956 KB |
Output is correct |
43 |
Correct |
26 ms |
8936 KB |
Output is correct |
44 |
Correct |
27 ms |
8988 KB |
Output is correct |
45 |
Correct |
45 ms |
12920 KB |
Output is correct |
46 |
Correct |
35 ms |
12920 KB |
Output is correct |
47 |
Correct |
128 ms |
21468 KB |
Output is correct |
48 |
Correct |
93 ms |
21248 KB |
Output is correct |
49 |
Correct |
117 ms |
21892 KB |
Output is correct |
50 |
Correct |
105 ms |
22936 KB |
Output is correct |
51 |
Correct |
230 ms |
25408 KB |
Output is correct |
52 |
Correct |
213 ms |
25592 KB |
Output is correct |
53 |
Correct |
103 ms |
23056 KB |
Output is correct |
54 |
Correct |
115 ms |
22680 KB |
Output is correct |
55 |
Correct |
129 ms |
22748 KB |
Output is correct |
56 |
Correct |
163 ms |
23872 KB |
Output is correct |
57 |
Correct |
214 ms |
48016 KB |
Output is correct |
58 |
Correct |
164 ms |
23284 KB |
Output is correct |
59 |
Correct |
176 ms |
23676 KB |
Output is correct |
60 |
Correct |
172 ms |
24320 KB |
Output is correct |
61 |
Correct |
166 ms |
23540 KB |
Output is correct |
62 |
Correct |
221 ms |
26872 KB |
Output is correct |
63 |
Correct |
357 ms |
44536 KB |
Output is correct |
64 |
Correct |
395 ms |
49920 KB |
Output is correct |
65 |
Correct |
552 ms |
58320 KB |
Output is correct |
66 |
Correct |
814 ms |
84272 KB |
Output is correct |
67 |
Correct |
585 ms |
85040 KB |
Output is correct |