#include <bits/stdc++.h>
using namespace std;
vector <int> g[30010];
const int magic = 173;
int n;
int table[180][30010];
int p[30010];
int b[30010];
class vertex {
public:
int position;
int power;
vertex (int position, int power) : position (position), power (power) {}
vertex (int position) : position (position) {
power = 0;
}
vertex () {}
int distance() {
return table[power][position];
}
void updateDist(int dist) {
table[power][position] = min(table[power][position], dist);
}
bool good(int dist) {
if(0 > position or position >= n) return false;
if(this -> distance() <= dist) return false;
else {
this -> updateDist(dist);
return true;
}
}
void out() {
cerr << position;
if(power != 0) cerr << " " << power;
cerr << endl;
}
};
class event {
public:
vertex v;
int dist;
event () {}
event (vertex v, int dist) : v(v), dist(dist) {}
bool operator < (event e) const {
return dist > e.dist;
}
};
void shortest_path() {
const int inf = table[0][0];
priority_queue <event> Q;
table[0][b[0]] = 0;
Q.push(event(vertex(b[0]), 0));
while(not Q.empty()) {
event e = Q.top();
Q.pop();
// e.v.out();
int dist = e.dist;
vertex v = e.v;
int cost;
vertex u;
if(v.power == 0) {
for(int i : g[v.position]) {
if(i <= magic) {
cost = 1;
u = vertex (v.position + i, i);
if(u.good(cost + dist)) Q.push(event(u, cost + dist));
u = vertex (v.position - i, i);
if(u.good(cost + dist)) Q.push(event(u, cost + dist));
} else {
cost = 0;
for(int pos = v.position + i; pos < n; pos += i) {
u = vertex (pos);
cost += 1;
if(u.good(dist + cost)) Q.push(event(u, dist + cost));
}
cost = 0;
for(int pos = v.position - i; pos >= 0; pos -= i) {
u = vertex (pos);
cost += 1;
if(u.good(dist + cost)) Q.push(event(u, dist + cost));
}
}
}
} else {
cost = 1;
u = vertex (v.position + v.power, v.power);
if(u.good(cost + dist)) Q.push(event(u, cost + dist));
u = vertex (v.position - v.power, v.power);
if(u.good(cost + dist)) Q.push(event(u, cost + dist));
u = vertex (v.position);
if(u.good(dist)) Q.push(event(u, dist));
}
}
int ans = table[0][b[1]];
ans = (ans == inf) ? -1 : ans;
printf("%d\n", ans);
}
int main(int argc, char const *argv[])
{
memset(table, 63, sizeof table);
int m;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) {
scanf("%d %d", b + i, p + i);
g[b[i]].push_back(p[i]);
}
shortest_path ();
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main(int, const char**)':
skyscraper.cpp:120:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
^
skyscraper.cpp:122:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", b + i, p + i);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
24060 KB |
Output is correct |
2 |
Correct |
0 ms |
24060 KB |
Output is correct |
3 |
Correct |
0 ms |
24060 KB |
Output is correct |
4 |
Correct |
3 ms |
24060 KB |
Output is correct |
5 |
Correct |
0 ms |
24060 KB |
Output is correct |
6 |
Correct |
0 ms |
24060 KB |
Output is correct |
7 |
Correct |
9 ms |
24060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
24060 KB |
Output is correct |
2 |
Correct |
0 ms |
24060 KB |
Output is correct |
3 |
Correct |
0 ms |
24060 KB |
Output is correct |
4 |
Correct |
3 ms |
24060 KB |
Output is correct |
5 |
Correct |
3 ms |
24060 KB |
Output is correct |
6 |
Correct |
3 ms |
24060 KB |
Output is correct |
7 |
Correct |
3 ms |
24060 KB |
Output is correct |
8 |
Correct |
0 ms |
24060 KB |
Output is correct |
9 |
Correct |
3 ms |
24060 KB |
Output is correct |
10 |
Correct |
0 ms |
24060 KB |
Output is correct |
11 |
Correct |
3 ms |
24200 KB |
Output is correct |
12 |
Correct |
3 ms |
24060 KB |
Output is correct |
13 |
Correct |
9 ms |
24060 KB |
Output is correct |
14 |
Correct |
3 ms |
24200 KB |
Output is correct |
15 |
Correct |
3 ms |
24200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
24060 KB |
Output is correct |
2 |
Correct |
0 ms |
24060 KB |
Output is correct |
3 |
Correct |
3 ms |
24060 KB |
Output is correct |
4 |
Correct |
0 ms |
24060 KB |
Output is correct |
5 |
Correct |
3 ms |
24060 KB |
Output is correct |
6 |
Correct |
0 ms |
24060 KB |
Output is correct |
7 |
Correct |
3 ms |
24060 KB |
Output is correct |
8 |
Correct |
3 ms |
24060 KB |
Output is correct |
9 |
Correct |
3 ms |
24060 KB |
Output is correct |
10 |
Correct |
0 ms |
24060 KB |
Output is correct |
11 |
Correct |
0 ms |
24200 KB |
Output is correct |
12 |
Correct |
6 ms |
24060 KB |
Output is correct |
13 |
Correct |
6 ms |
24060 KB |
Output is correct |
14 |
Correct |
3 ms |
24200 KB |
Output is correct |
15 |
Correct |
0 ms |
24200 KB |
Output is correct |
16 |
Correct |
6 ms |
24060 KB |
Output is correct |
17 |
Correct |
0 ms |
24208 KB |
Output is correct |
18 |
Correct |
0 ms |
24060 KB |
Output is correct |
19 |
Correct |
0 ms |
24060 KB |
Output is correct |
20 |
Correct |
3 ms |
24192 KB |
Output is correct |
21 |
Correct |
3 ms |
24060 KB |
Output is correct |
22 |
Correct |
0 ms |
24060 KB |
Output is correct |
23 |
Correct |
0 ms |
24060 KB |
Output is correct |
24 |
Correct |
6 ms |
24060 KB |
Output is correct |
25 |
Correct |
3 ms |
24060 KB |
Output is correct |
26 |
Correct |
3 ms |
24060 KB |
Output is correct |
27 |
Correct |
0 ms |
24060 KB |
Output is correct |
28 |
Correct |
3 ms |
24192 KB |
Output is correct |
29 |
Correct |
6 ms |
24192 KB |
Output is correct |
30 |
Correct |
0 ms |
24060 KB |
Output is correct |
31 |
Correct |
0 ms |
24060 KB |
Output is correct |
32 |
Correct |
3 ms |
24060 KB |
Output is correct |
33 |
Correct |
16 ms |
24200 KB |
Output is correct |
34 |
Correct |
16 ms |
24200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
24060 KB |
Output is correct |
2 |
Correct |
6 ms |
24060 KB |
Output is correct |
3 |
Correct |
0 ms |
24060 KB |
Output is correct |
4 |
Correct |
3 ms |
24060 KB |
Output is correct |
5 |
Correct |
0 ms |
24060 KB |
Output is correct |
6 |
Correct |
3 ms |
24060 KB |
Output is correct |
7 |
Correct |
6 ms |
24060 KB |
Output is correct |
8 |
Correct |
0 ms |
24060 KB |
Output is correct |
9 |
Correct |
3 ms |
24060 KB |
Output is correct |
10 |
Correct |
0 ms |
24060 KB |
Output is correct |
11 |
Correct |
9 ms |
24200 KB |
Output is correct |
12 |
Correct |
0 ms |
24060 KB |
Output is correct |
13 |
Correct |
3 ms |
24060 KB |
Output is correct |
14 |
Correct |
0 ms |
24200 KB |
Output is correct |
15 |
Correct |
6 ms |
24200 KB |
Output is correct |
16 |
Correct |
3 ms |
24060 KB |
Output is correct |
17 |
Correct |
6 ms |
24208 KB |
Output is correct |
18 |
Correct |
0 ms |
24060 KB |
Output is correct |
19 |
Correct |
3 ms |
24060 KB |
Output is correct |
20 |
Correct |
0 ms |
24192 KB |
Output is correct |
21 |
Correct |
6 ms |
24060 KB |
Output is correct |
22 |
Correct |
0 ms |
24060 KB |
Output is correct |
23 |
Correct |
3 ms |
24060 KB |
Output is correct |
24 |
Correct |
3 ms |
24060 KB |
Output is correct |
25 |
Correct |
0 ms |
24060 KB |
Output is correct |
26 |
Correct |
6 ms |
24060 KB |
Output is correct |
27 |
Correct |
3 ms |
24060 KB |
Output is correct |
28 |
Correct |
3 ms |
24192 KB |
Output is correct |
29 |
Correct |
9 ms |
24192 KB |
Output is correct |
30 |
Correct |
3 ms |
24060 KB |
Output is correct |
31 |
Correct |
3 ms |
24060 KB |
Output is correct |
32 |
Correct |
6 ms |
24060 KB |
Output is correct |
33 |
Correct |
6 ms |
24200 KB |
Output is correct |
34 |
Correct |
13 ms |
24200 KB |
Output is correct |
35 |
Correct |
16 ms |
24528 KB |
Output is correct |
36 |
Correct |
3 ms |
24192 KB |
Output is correct |
37 |
Correct |
13 ms |
24708 KB |
Output is correct |
38 |
Correct |
19 ms |
24476 KB |
Output is correct |
39 |
Correct |
16 ms |
24472 KB |
Output is correct |
40 |
Correct |
23 ms |
24476 KB |
Output is correct |
41 |
Correct |
19 ms |
24476 KB |
Output is correct |
42 |
Correct |
6 ms |
24336 KB |
Output is correct |
43 |
Correct |
9 ms |
24336 KB |
Output is correct |
44 |
Correct |
6 ms |
24332 KB |
Output is correct |
45 |
Correct |
43 ms |
24896 KB |
Output is correct |
46 |
Correct |
43 ms |
24900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
24060 KB |
Output is correct |
2 |
Correct |
3 ms |
24060 KB |
Output is correct |
3 |
Correct |
0 ms |
24060 KB |
Output is correct |
4 |
Correct |
3 ms |
24060 KB |
Output is correct |
5 |
Correct |
0 ms |
24060 KB |
Output is correct |
6 |
Correct |
0 ms |
24060 KB |
Output is correct |
7 |
Correct |
0 ms |
24060 KB |
Output is correct |
8 |
Correct |
0 ms |
24060 KB |
Output is correct |
9 |
Correct |
0 ms |
24060 KB |
Output is correct |
10 |
Correct |
0 ms |
24060 KB |
Output is correct |
11 |
Correct |
3 ms |
24200 KB |
Output is correct |
12 |
Correct |
0 ms |
24060 KB |
Output is correct |
13 |
Correct |
3 ms |
24060 KB |
Output is correct |
14 |
Correct |
0 ms |
24200 KB |
Output is correct |
15 |
Correct |
3 ms |
24200 KB |
Output is correct |
16 |
Correct |
0 ms |
24060 KB |
Output is correct |
17 |
Correct |
0 ms |
24208 KB |
Output is correct |
18 |
Correct |
3 ms |
24060 KB |
Output is correct |
19 |
Correct |
0 ms |
24060 KB |
Output is correct |
20 |
Correct |
0 ms |
24192 KB |
Output is correct |
21 |
Correct |
3 ms |
24060 KB |
Output is correct |
22 |
Correct |
3 ms |
24060 KB |
Output is correct |
23 |
Correct |
0 ms |
24060 KB |
Output is correct |
24 |
Correct |
0 ms |
24060 KB |
Output is correct |
25 |
Correct |
3 ms |
24060 KB |
Output is correct |
26 |
Correct |
0 ms |
24060 KB |
Output is correct |
27 |
Correct |
9 ms |
24060 KB |
Output is correct |
28 |
Correct |
6 ms |
24192 KB |
Output is correct |
29 |
Correct |
9 ms |
24192 KB |
Output is correct |
30 |
Correct |
9 ms |
24060 KB |
Output is correct |
31 |
Correct |
3 ms |
24060 KB |
Output is correct |
32 |
Correct |
0 ms |
24060 KB |
Output is correct |
33 |
Correct |
9 ms |
24200 KB |
Output is correct |
34 |
Correct |
6 ms |
24200 KB |
Output is correct |
35 |
Correct |
19 ms |
24528 KB |
Output is correct |
36 |
Correct |
6 ms |
24192 KB |
Output is correct |
37 |
Correct |
19 ms |
24708 KB |
Output is correct |
38 |
Correct |
23 ms |
24476 KB |
Output is correct |
39 |
Correct |
16 ms |
24472 KB |
Output is correct |
40 |
Correct |
26 ms |
24476 KB |
Output is correct |
41 |
Correct |
23 ms |
24476 KB |
Output is correct |
42 |
Correct |
9 ms |
24336 KB |
Output is correct |
43 |
Correct |
3 ms |
24336 KB |
Output is correct |
44 |
Correct |
16 ms |
24332 KB |
Output is correct |
45 |
Correct |
46 ms |
24896 KB |
Output is correct |
46 |
Correct |
43 ms |
24900 KB |
Output is correct |
47 |
Correct |
83 ms |
25064 KB |
Output is correct |
48 |
Correct |
9 ms |
24588 KB |
Output is correct |
49 |
Correct |
13 ms |
24588 KB |
Output is correct |
50 |
Correct |
9 ms |
24456 KB |
Output is correct |
51 |
Correct |
43 ms |
25312 KB |
Output is correct |
52 |
Correct |
39 ms |
25316 KB |
Output is correct |
53 |
Correct |
19 ms |
24720 KB |
Output is correct |
54 |
Correct |
3 ms |
24060 KB |
Output is correct |
55 |
Correct |
0 ms |
24060 KB |
Output is correct |
56 |
Correct |
16 ms |
24984 KB |
Output is correct |
57 |
Correct |
49 ms |
24192 KB |
Output is correct |
58 |
Correct |
9 ms |
24332 KB |
Output is correct |
59 |
Correct |
9 ms |
24332 KB |
Output is correct |
60 |
Correct |
19 ms |
24340 KB |
Output is correct |
61 |
Correct |
13 ms |
24348 KB |
Output is correct |
62 |
Correct |
23 ms |
24984 KB |
Output is correct |
63 |
Correct |
29 ms |
26680 KB |
Output is correct |
64 |
Correct |
23 ms |
26644 KB |
Output is correct |
65 |
Correct |
153 ms |
26668 KB |
Output is correct |
66 |
Correct |
536 ms |
26624 KB |
Output is correct |
67 |
Correct |
523 ms |
26624 KB |
Output is correct |