#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second
int n, m;
vector<int> b[30010];
const int Lim = 80;
const int V = 30000*(Lim + 1);
int dist[V];
vector<pp> edge[V];
vector<int> usable[30010];
int main()
{
read(n, m);
int tg = 0, sp = 0;
for(int i=0; i<m; ++i){
int b, p;
read(b, p);
if(i == 0) sp = b;
if(i == 1) tg = b;
if(p <= Lim) ::b[p].pb(b);
else usable[b].pb(p);
}
for(int p=1; p<=Lim; ++p) if(b[p].size()){
for(int a:b[p]) edge[a].eb(p*n+a, 0);
for(int i=0; i<n; ++i){
if(i+p < n) edge[p*n+i].eb(p*n+(i+p), 1);
if(i-p >=0) edge[p*n+i].eb(p*n+(i-p), 1);
edge[p*n+i].eb(i, 0);
}
}
fill(dist, dist+V, 1e9);
priority_queue<pp> pq;
dist[sp] = 0;
pq.emplace(0, sp);
while(pq.size()){
int md, x; tie(md, x) = pq.top(); pq.pop();
md = -md;
if(md != dist[x]) continue;
auto relax = [&](int y, int d){
if(dist[y] > d){
dist[y] = d;
pq.emplace(-d, y);
}
};
for(auto et:edge[x]) relax(et.x, md+et.y);
if(x < n){
for(int p:usable[x]){
for(int i=1;;++i) if(x-p*i>=0) relax(x-p*i, md+i); else break;
for(int i=1;;++i) if(x+p*i<n) relax(x+p*i, md+i); else break;
}
}
}
int ans = dist[tg];
if(ans == 1e9) ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message
skyscraper.cpp: In function 'void read(int&)':
skyscraper.cpp:6:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
void read(int& x){ scanf("%d",&x); }
~~~~~^~~~~~~~~
skyscraper.cpp: In function 'void read(ll&)':
skyscraper.cpp:7:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
void read(ll& x){ scanf("%lld",&x); }
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
68216 KB |
Output is correct |
2 |
Correct |
59 ms |
68588 KB |
Output is correct |
3 |
Correct |
57 ms |
68632 KB |
Output is correct |
4 |
Correct |
63 ms |
68632 KB |
Output is correct |
5 |
Correct |
120 ms |
68632 KB |
Output is correct |
6 |
Correct |
63 ms |
68632 KB |
Output is correct |
7 |
Correct |
62 ms |
68632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
68632 KB |
Output is correct |
2 |
Correct |
66 ms |
68632 KB |
Output is correct |
3 |
Correct |
60 ms |
68632 KB |
Output is correct |
4 |
Correct |
72 ms |
68632 KB |
Output is correct |
5 |
Correct |
62 ms |
68632 KB |
Output is correct |
6 |
Correct |
63 ms |
68632 KB |
Output is correct |
7 |
Correct |
61 ms |
68632 KB |
Output is correct |
8 |
Correct |
60 ms |
68632 KB |
Output is correct |
9 |
Correct |
61 ms |
68632 KB |
Output is correct |
10 |
Correct |
61 ms |
68856 KB |
Output is correct |
11 |
Correct |
65 ms |
68856 KB |
Output is correct |
12 |
Correct |
65 ms |
68856 KB |
Output is correct |
13 |
Correct |
63 ms |
68856 KB |
Output is correct |
14 |
Correct |
69 ms |
69016 KB |
Output is correct |
15 |
Correct |
66 ms |
69016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
69016 KB |
Output is correct |
2 |
Correct |
58 ms |
69016 KB |
Output is correct |
3 |
Correct |
58 ms |
69016 KB |
Output is correct |
4 |
Correct |
64 ms |
69016 KB |
Output is correct |
5 |
Correct |
63 ms |
69016 KB |
Output is correct |
6 |
Correct |
60 ms |
69016 KB |
Output is correct |
7 |
Correct |
55 ms |
69016 KB |
Output is correct |
8 |
Correct |
57 ms |
69016 KB |
Output is correct |
9 |
Correct |
57 ms |
69016 KB |
Output is correct |
10 |
Correct |
58 ms |
69016 KB |
Output is correct |
11 |
Correct |
59 ms |
69016 KB |
Output is correct |
12 |
Correct |
57 ms |
69016 KB |
Output is correct |
13 |
Correct |
63 ms |
69016 KB |
Output is correct |
14 |
Correct |
70 ms |
69016 KB |
Output is correct |
15 |
Correct |
63 ms |
69016 KB |
Output is correct |
16 |
Correct |
67 ms |
69244 KB |
Output is correct |
17 |
Correct |
70 ms |
71292 KB |
Output is correct |
18 |
Correct |
68 ms |
71548 KB |
Output is correct |
19 |
Correct |
90 ms |
71548 KB |
Output is correct |
20 |
Correct |
65 ms |
71548 KB |
Output is correct |
21 |
Correct |
77 ms |
71548 KB |
Output is correct |
22 |
Correct |
76 ms |
71548 KB |
Output is correct |
23 |
Correct |
87 ms |
71548 KB |
Output is correct |
24 |
Correct |
101 ms |
73412 KB |
Output is correct |
25 |
Correct |
64 ms |
73412 KB |
Output is correct |
26 |
Correct |
97 ms |
73412 KB |
Output is correct |
27 |
Correct |
71 ms |
73412 KB |
Output is correct |
28 |
Correct |
82 ms |
73412 KB |
Output is correct |
29 |
Correct |
85 ms |
73412 KB |
Output is correct |
30 |
Correct |
66 ms |
73412 KB |
Output is correct |
31 |
Correct |
76 ms |
73412 KB |
Output is correct |
32 |
Correct |
70 ms |
73412 KB |
Output is correct |
33 |
Correct |
110 ms |
74556 KB |
Output is correct |
34 |
Correct |
111 ms |
74748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
74748 KB |
Output is correct |
2 |
Correct |
69 ms |
74748 KB |
Output is correct |
3 |
Correct |
68 ms |
74748 KB |
Output is correct |
4 |
Correct |
63 ms |
74748 KB |
Output is correct |
5 |
Correct |
61 ms |
74748 KB |
Output is correct |
6 |
Correct |
66 ms |
74748 KB |
Output is correct |
7 |
Correct |
69 ms |
74748 KB |
Output is correct |
8 |
Correct |
65 ms |
74748 KB |
Output is correct |
9 |
Correct |
58 ms |
74748 KB |
Output is correct |
10 |
Correct |
60 ms |
74748 KB |
Output is correct |
11 |
Correct |
59 ms |
74748 KB |
Output is correct |
12 |
Correct |
57 ms |
74748 KB |
Output is correct |
13 |
Correct |
64 ms |
74748 KB |
Output is correct |
14 |
Correct |
69 ms |
74748 KB |
Output is correct |
15 |
Correct |
66 ms |
74748 KB |
Output is correct |
16 |
Correct |
67 ms |
74748 KB |
Output is correct |
17 |
Correct |
72 ms |
74748 KB |
Output is correct |
18 |
Correct |
71 ms |
74748 KB |
Output is correct |
19 |
Correct |
65 ms |
74748 KB |
Output is correct |
20 |
Correct |
59 ms |
74748 KB |
Output is correct |
21 |
Correct |
63 ms |
74748 KB |
Output is correct |
22 |
Correct |
71 ms |
74748 KB |
Output is correct |
23 |
Correct |
87 ms |
74748 KB |
Output is correct |
24 |
Correct |
96 ms |
74748 KB |
Output is correct |
25 |
Correct |
60 ms |
74748 KB |
Output is correct |
26 |
Correct |
101 ms |
74748 KB |
Output is correct |
27 |
Correct |
71 ms |
74748 KB |
Output is correct |
28 |
Correct |
81 ms |
74748 KB |
Output is correct |
29 |
Correct |
90 ms |
74748 KB |
Output is correct |
30 |
Correct |
71 ms |
74748 KB |
Output is correct |
31 |
Correct |
91 ms |
74748 KB |
Output is correct |
32 |
Correct |
71 ms |
74748 KB |
Output is correct |
33 |
Correct |
113 ms |
74748 KB |
Output is correct |
34 |
Correct |
130 ms |
74748 KB |
Output is correct |
35 |
Correct |
117 ms |
74748 KB |
Output is correct |
36 |
Correct |
79 ms |
74748 KB |
Output is correct |
37 |
Correct |
133 ms |
76128 KB |
Output is correct |
38 |
Correct |
135 ms |
76368 KB |
Output is correct |
39 |
Correct |
127 ms |
76368 KB |
Output is correct |
40 |
Correct |
141 ms |
76368 KB |
Output is correct |
41 |
Correct |
156 ms |
76368 KB |
Output is correct |
42 |
Correct |
109 ms |
76368 KB |
Output is correct |
43 |
Correct |
89 ms |
76368 KB |
Output is correct |
44 |
Correct |
71 ms |
76368 KB |
Output is correct |
45 |
Correct |
140 ms |
76368 KB |
Output is correct |
46 |
Correct |
288 ms |
76536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
76536 KB |
Output is correct |
2 |
Correct |
63 ms |
76536 KB |
Output is correct |
3 |
Correct |
75 ms |
76536 KB |
Output is correct |
4 |
Correct |
63 ms |
76536 KB |
Output is correct |
5 |
Correct |
58 ms |
76536 KB |
Output is correct |
6 |
Correct |
65 ms |
76536 KB |
Output is correct |
7 |
Correct |
58 ms |
76536 KB |
Output is correct |
8 |
Correct |
61 ms |
76536 KB |
Output is correct |
9 |
Correct |
64 ms |
76536 KB |
Output is correct |
10 |
Correct |
63 ms |
76536 KB |
Output is correct |
11 |
Correct |
67 ms |
76536 KB |
Output is correct |
12 |
Correct |
64 ms |
76536 KB |
Output is correct |
13 |
Correct |
61 ms |
76536 KB |
Output is correct |
14 |
Correct |
62 ms |
76536 KB |
Output is correct |
15 |
Correct |
63 ms |
76536 KB |
Output is correct |
16 |
Correct |
62 ms |
76536 KB |
Output is correct |
17 |
Correct |
74 ms |
76536 KB |
Output is correct |
18 |
Correct |
76 ms |
76536 KB |
Output is correct |
19 |
Correct |
64 ms |
76536 KB |
Output is correct |
20 |
Correct |
64 ms |
76536 KB |
Output is correct |
21 |
Correct |
59 ms |
76536 KB |
Output is correct |
22 |
Correct |
72 ms |
76536 KB |
Output is correct |
23 |
Correct |
67 ms |
76536 KB |
Output is correct |
24 |
Correct |
82 ms |
76536 KB |
Output is correct |
25 |
Correct |
59 ms |
76536 KB |
Output is correct |
26 |
Correct |
74 ms |
76536 KB |
Output is correct |
27 |
Correct |
64 ms |
76536 KB |
Output is correct |
28 |
Correct |
81 ms |
76536 KB |
Output is correct |
29 |
Correct |
79 ms |
76536 KB |
Output is correct |
30 |
Correct |
64 ms |
76536 KB |
Output is correct |
31 |
Correct |
71 ms |
76536 KB |
Output is correct |
32 |
Correct |
67 ms |
76536 KB |
Output is correct |
33 |
Correct |
96 ms |
76536 KB |
Output is correct |
34 |
Correct |
103 ms |
76536 KB |
Output is correct |
35 |
Correct |
97 ms |
76536 KB |
Output is correct |
36 |
Correct |
75 ms |
76536 KB |
Output is correct |
37 |
Correct |
116 ms |
76536 KB |
Output is correct |
38 |
Correct |
119 ms |
76536 KB |
Output is correct |
39 |
Correct |
131 ms |
76536 KB |
Output is correct |
40 |
Correct |
125 ms |
76536 KB |
Output is correct |
41 |
Correct |
123 ms |
76536 KB |
Output is correct |
42 |
Correct |
88 ms |
76536 KB |
Output is correct |
43 |
Correct |
78 ms |
76536 KB |
Output is correct |
44 |
Correct |
68 ms |
76536 KB |
Output is correct |
45 |
Correct |
131 ms |
76536 KB |
Output is correct |
46 |
Correct |
134 ms |
76536 KB |
Output is correct |
47 |
Correct |
416 ms |
115580 KB |
Output is correct |
48 |
Correct |
270 ms |
125708 KB |
Output is correct |
49 |
Correct |
201 ms |
125708 KB |
Output is correct |
50 |
Correct |
235 ms |
125708 KB |
Output is correct |
51 |
Correct |
349 ms |
132872 KB |
Output is correct |
52 |
Correct |
482 ms |
148412 KB |
Output is correct |
53 |
Correct |
91 ms |
148412 KB |
Output is correct |
54 |
Correct |
69 ms |
148412 KB |
Output is correct |
55 |
Correct |
80 ms |
148412 KB |
Output is correct |
56 |
Correct |
110 ms |
148412 KB |
Output is correct |
57 |
Correct |
317 ms |
148412 KB |
Output is correct |
58 |
Correct |
132 ms |
148412 KB |
Output is correct |
59 |
Correct |
239 ms |
148412 KB |
Output is correct |
60 |
Correct |
343 ms |
148412 KB |
Output is correct |
61 |
Correct |
215 ms |
148412 KB |
Output is correct |
62 |
Correct |
470 ms |
148412 KB |
Output is correct |
63 |
Correct |
93 ms |
148412 KB |
Output is correct |
64 |
Correct |
102 ms |
148412 KB |
Output is correct |
65 |
Correct |
96 ms |
148412 KB |
Output is correct |
66 |
Correct |
946 ms |
182668 KB |
Output is correct |
67 |
Correct |
884 ms |
182936 KB |
Output is correct |