#include <cstdio>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>
#include <limits>
using namespace std;
vector<int> ps[30000];
int dis[30000];
int main() {
int n, m;
scanf("%d %d", &n, &m);
int s, t;
for(int i = 0; i < m; i++) {
int b, p;
scanf("%d %d", &b, &p);
ps[b].push_back(p);
if(i == 0)
s = b;
else if(i == 1)
t = b;
}
for(int i = 0; i < n; i++) {
vector<int>& v = ps[i];
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
fill_n(dis, n, numeric_limits<int>::max());
dis[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(!pq.empty()) {
int cost, where;
tie(cost, where) = pq.top();
pq.pop();
if(where == t) {
printf("%d\n", cost);
return 0;
}
if(dis[where] < cost)
continue;
for(int p : ps[where]) {
for(int i = 1; where + i * p < n; i++) {
if(dis[where + i * p] > cost + i)
pq.push({dis[where + i * p] = cost + i, where + i * p});
//if(binary_search(ps[where + i * p].begin(), ps[where + i * p].end(), p))
//break;
}
for(int i = 1; where - i * p >= 0; i++) {
if(dis[where - i * p] > cost + i)
pq.push({dis[where - i * p] = cost + i, where - i * p});
//if(binary_search(ps[where - i * p].begin(), ps[where - i * p].end(), p))
//break;
}
}
}
puts("-1");
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:14:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:18:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &b, &p);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:41:9: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(where == t) {
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
1056 KB |
Output is correct |
4 |
Correct |
3 ms |
1136 KB |
Output is correct |
5 |
Correct |
2 ms |
1344 KB |
Output is correct |
6 |
Correct |
2 ms |
1344 KB |
Output is correct |
7 |
Correct |
2 ms |
1344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1344 KB |
Output is correct |
2 |
Correct |
3 ms |
1344 KB |
Output is correct |
3 |
Correct |
2 ms |
1344 KB |
Output is correct |
4 |
Correct |
2 ms |
1344 KB |
Output is correct |
5 |
Correct |
2 ms |
1344 KB |
Output is correct |
6 |
Correct |
2 ms |
1344 KB |
Output is correct |
7 |
Correct |
2 ms |
1344 KB |
Output is correct |
8 |
Correct |
2 ms |
1344 KB |
Output is correct |
9 |
Correct |
2 ms |
1344 KB |
Output is correct |
10 |
Correct |
2 ms |
1344 KB |
Output is correct |
11 |
Correct |
3 ms |
1344 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
3 ms |
1500 KB |
Output is correct |
14 |
Correct |
3 ms |
1500 KB |
Output is correct |
15 |
Correct |
3 ms |
1500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1500 KB |
Output is correct |
2 |
Correct |
2 ms |
1500 KB |
Output is correct |
3 |
Correct |
2 ms |
1500 KB |
Output is correct |
4 |
Correct |
2 ms |
1500 KB |
Output is correct |
5 |
Correct |
2 ms |
1500 KB |
Output is correct |
6 |
Correct |
2 ms |
1500 KB |
Output is correct |
7 |
Correct |
2 ms |
1500 KB |
Output is correct |
8 |
Correct |
2 ms |
1500 KB |
Output is correct |
9 |
Correct |
3 ms |
1500 KB |
Output is correct |
10 |
Correct |
2 ms |
1500 KB |
Output is correct |
11 |
Correct |
4 ms |
1500 KB |
Output is correct |
12 |
Correct |
2 ms |
1500 KB |
Output is correct |
13 |
Correct |
2 ms |
1500 KB |
Output is correct |
14 |
Correct |
4 ms |
1500 KB |
Output is correct |
15 |
Correct |
3 ms |
1500 KB |
Output is correct |
16 |
Correct |
2 ms |
1500 KB |
Output is correct |
17 |
Correct |
3 ms |
1500 KB |
Output is correct |
18 |
Correct |
2 ms |
1500 KB |
Output is correct |
19 |
Correct |
3 ms |
1500 KB |
Output is correct |
20 |
Correct |
11 ms |
1544 KB |
Output is correct |
21 |
Correct |
3 ms |
1544 KB |
Output is correct |
22 |
Correct |
2 ms |
1544 KB |
Output is correct |
23 |
Correct |
3 ms |
1544 KB |
Output is correct |
24 |
Correct |
4 ms |
1544 KB |
Output is correct |
25 |
Correct |
3 ms |
1544 KB |
Output is correct |
26 |
Correct |
3 ms |
1544 KB |
Output is correct |
27 |
Correct |
3 ms |
1544 KB |
Output is correct |
28 |
Correct |
3 ms |
1544 KB |
Output is correct |
29 |
Correct |
5 ms |
1668 KB |
Output is correct |
30 |
Correct |
3 ms |
1668 KB |
Output is correct |
31 |
Correct |
4 ms |
1668 KB |
Output is correct |
32 |
Correct |
3 ms |
1668 KB |
Output is correct |
33 |
Correct |
6 ms |
1796 KB |
Output is correct |
34 |
Correct |
5 ms |
1796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1796 KB |
Output is correct |
2 |
Correct |
2 ms |
1796 KB |
Output is correct |
3 |
Correct |
2 ms |
1796 KB |
Output is correct |
4 |
Correct |
2 ms |
1796 KB |
Output is correct |
5 |
Correct |
2 ms |
1796 KB |
Output is correct |
6 |
Correct |
2 ms |
1796 KB |
Output is correct |
7 |
Correct |
2 ms |
1796 KB |
Output is correct |
8 |
Correct |
2 ms |
1796 KB |
Output is correct |
9 |
Correct |
2 ms |
1796 KB |
Output is correct |
10 |
Correct |
3 ms |
1796 KB |
Output is correct |
11 |
Correct |
3 ms |
1796 KB |
Output is correct |
12 |
Correct |
2 ms |
1796 KB |
Output is correct |
13 |
Correct |
3 ms |
1796 KB |
Output is correct |
14 |
Correct |
3 ms |
1796 KB |
Output is correct |
15 |
Correct |
3 ms |
1796 KB |
Output is correct |
16 |
Correct |
2 ms |
1796 KB |
Output is correct |
17 |
Correct |
3 ms |
1796 KB |
Output is correct |
18 |
Correct |
2 ms |
1796 KB |
Output is correct |
19 |
Correct |
3 ms |
1796 KB |
Output is correct |
20 |
Correct |
7 ms |
1796 KB |
Output is correct |
21 |
Correct |
3 ms |
1796 KB |
Output is correct |
22 |
Correct |
2 ms |
1796 KB |
Output is correct |
23 |
Correct |
4 ms |
1796 KB |
Output is correct |
24 |
Correct |
3 ms |
1796 KB |
Output is correct |
25 |
Correct |
3 ms |
1796 KB |
Output is correct |
26 |
Correct |
3 ms |
1796 KB |
Output is correct |
27 |
Correct |
3 ms |
1796 KB |
Output is correct |
28 |
Correct |
3 ms |
1796 KB |
Output is correct |
29 |
Correct |
4 ms |
1796 KB |
Output is correct |
30 |
Correct |
3 ms |
1796 KB |
Output is correct |
31 |
Correct |
3 ms |
1796 KB |
Output is correct |
32 |
Correct |
3 ms |
1796 KB |
Output is correct |
33 |
Correct |
7 ms |
1864 KB |
Output is correct |
34 |
Correct |
5 ms |
1888 KB |
Output is correct |
35 |
Correct |
12 ms |
1888 KB |
Output is correct |
36 |
Correct |
4 ms |
1888 KB |
Output is correct |
37 |
Correct |
9 ms |
1888 KB |
Output is correct |
38 |
Correct |
15 ms |
1888 KB |
Output is correct |
39 |
Correct |
12 ms |
1888 KB |
Output is correct |
40 |
Correct |
16 ms |
1888 KB |
Output is correct |
41 |
Correct |
12 ms |
1944 KB |
Output is correct |
42 |
Correct |
8 ms |
1944 KB |
Output is correct |
43 |
Correct |
9 ms |
1944 KB |
Output is correct |
44 |
Correct |
22 ms |
1944 KB |
Output is correct |
45 |
Correct |
14 ms |
2016 KB |
Output is correct |
46 |
Correct |
12 ms |
2016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2016 KB |
Output is correct |
2 |
Correct |
2 ms |
2016 KB |
Output is correct |
3 |
Correct |
2 ms |
2016 KB |
Output is correct |
4 |
Correct |
2 ms |
2016 KB |
Output is correct |
5 |
Correct |
2 ms |
2016 KB |
Output is correct |
6 |
Correct |
2 ms |
2016 KB |
Output is correct |
7 |
Correct |
2 ms |
2016 KB |
Output is correct |
8 |
Correct |
2 ms |
2016 KB |
Output is correct |
9 |
Correct |
2 ms |
2016 KB |
Output is correct |
10 |
Correct |
2 ms |
2016 KB |
Output is correct |
11 |
Correct |
3 ms |
2016 KB |
Output is correct |
12 |
Correct |
2 ms |
2016 KB |
Output is correct |
13 |
Correct |
3 ms |
2016 KB |
Output is correct |
14 |
Correct |
3 ms |
2016 KB |
Output is correct |
15 |
Correct |
3 ms |
2016 KB |
Output is correct |
16 |
Correct |
2 ms |
2016 KB |
Output is correct |
17 |
Correct |
3 ms |
2016 KB |
Output is correct |
18 |
Correct |
2 ms |
2016 KB |
Output is correct |
19 |
Correct |
3 ms |
2016 KB |
Output is correct |
20 |
Correct |
7 ms |
2016 KB |
Output is correct |
21 |
Correct |
3 ms |
2016 KB |
Output is correct |
22 |
Correct |
2 ms |
2016 KB |
Output is correct |
23 |
Correct |
3 ms |
2016 KB |
Output is correct |
24 |
Correct |
4 ms |
2016 KB |
Output is correct |
25 |
Correct |
4 ms |
2016 KB |
Output is correct |
26 |
Correct |
4 ms |
2016 KB |
Output is correct |
27 |
Correct |
3 ms |
2016 KB |
Output is correct |
28 |
Correct |
3 ms |
2016 KB |
Output is correct |
29 |
Correct |
5 ms |
2016 KB |
Output is correct |
30 |
Correct |
3 ms |
2016 KB |
Output is correct |
31 |
Correct |
4 ms |
2016 KB |
Output is correct |
32 |
Correct |
3 ms |
2016 KB |
Output is correct |
33 |
Correct |
6 ms |
2016 KB |
Output is correct |
34 |
Correct |
5 ms |
2016 KB |
Output is correct |
35 |
Correct |
11 ms |
2016 KB |
Output is correct |
36 |
Correct |
4 ms |
2016 KB |
Output is correct |
37 |
Correct |
9 ms |
2016 KB |
Output is correct |
38 |
Correct |
21 ms |
2016 KB |
Output is correct |
39 |
Correct |
19 ms |
2016 KB |
Output is correct |
40 |
Correct |
12 ms |
2016 KB |
Output is correct |
41 |
Correct |
20 ms |
2016 KB |
Output is correct |
42 |
Correct |
15 ms |
2016 KB |
Output is correct |
43 |
Correct |
11 ms |
2016 KB |
Output is correct |
44 |
Correct |
13 ms |
2016 KB |
Output is correct |
45 |
Correct |
22 ms |
2116 KB |
Output is correct |
46 |
Correct |
12 ms |
2116 KB |
Output is correct |
47 |
Correct |
15 ms |
2528 KB |
Output is correct |
48 |
Correct |
11 ms |
2528 KB |
Output is correct |
49 |
Correct |
11 ms |
2528 KB |
Output is correct |
50 |
Correct |
8 ms |
2528 KB |
Output is correct |
51 |
Correct |
29 ms |
3420 KB |
Output is correct |
52 |
Correct |
23 ms |
3420 KB |
Output is correct |
53 |
Correct |
14 ms |
3420 KB |
Output is correct |
54 |
Correct |
4 ms |
3420 KB |
Output is correct |
55 |
Correct |
4 ms |
3420 KB |
Output is correct |
56 |
Correct |
852 ms |
3420 KB |
Output is correct |
57 |
Correct |
5 ms |
3420 KB |
Output is correct |
58 |
Correct |
11 ms |
3420 KB |
Output is correct |
59 |
Correct |
11 ms |
3420 KB |
Output is correct |
60 |
Correct |
12 ms |
3420 KB |
Output is correct |
61 |
Correct |
13 ms |
3420 KB |
Output is correct |
62 |
Correct |
15 ms |
3420 KB |
Output is correct |
63 |
Correct |
43 ms |
4060 KB |
Output is correct |
64 |
Correct |
45 ms |
4060 KB |
Output is correct |
65 |
Correct |
57 ms |
4060 KB |
Output is correct |
66 |
Correct |
114 ms |
5964 KB |
Output is correct |
67 |
Correct |
67 ms |
5964 KB |
Output is correct |