#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
const int maxn = 3e4+5;
const int half = 173;
int n, m;
int dist[maxn*180*2];
vector< ii > adj[maxn];
bool ok(int x)
{
return 0<= x && x< n;
}
int N;
priority_queue< pair<int, int> > q;
int conv(int u, int md, int isleft)
{
if(isleft) return (half+1)*n+(md-1)*n+u;
return md*n+u;
}
void bfs(int s)
{
for(int i = 0; i< N; i++) dist[i] = 1e9;
dist[s] = 0; q.push({0, s});
while(!q.empty())
{
auto kk = q.top(); q.pop();
int u = kk.Y, d = -kk.X;
// printf("%d\n", u);
if(d> dist[u]) continue;
if(u< n)
{
for(auto k2 : adj[u])
{
int v = k2.X, w = k2.Y;
if(dist[v]> dist[u]+w)
{
dist[v] = dist[u]+w;
q.push({-dist[v], v});
}
}
}
if(u>= n)
{
bool isleft = false;
int tmp;
if(u< (half+1)*n)
{
isleft = false;
tmp = u-n;
}
else
{
isleft = true;
tmp = u-(half+1)*n;
}
int md = tmp/n+1;
int sub = tmp%n;
// assert(conv(sub, md, isleft) == u);
if(isleft && ok(sub-md))
{
int nxt = conv(sub-md, md, isleft);
if(dist[nxt]> dist[u]+1)
{
dist[nxt] = dist[u]+1;
q.push({-dist[nxt], nxt});
}
if(dist[sub-md]> dist[u]+1)
{
dist[sub-md] = dist[u]+1;
q.push({-dist[sub-md], sub-md});
}
}
if(!isleft && ok(sub+md))
{
int nxt = conv(sub+md, md, isleft);
if(dist[nxt]> dist[u]+1)
{
dist[nxt] = dist[u]+1;
q.push({-dist[nxt], nxt});
}
if(dist[sub+md]> dist[u]+1)
{
dist[sub+md] = dist[u]+1;
q.push({-dist[sub+md], sub+md});
}
}
}
}
}
int hom[maxn], mana[maxn];
int main()
{
scanf("%d %d", &n, &m);
N = conv(n-1, half, 1);
for(int i = 0; i< m; i++)
{
scanf("%d %d", &hom[i], &mana[i]);
}
// printf("okay\n");
for(int i = 0; i< m; i++)
{
int pw = mana[i];
if(pw> half)
{
int cur = hom[i]+pw;
while(cur< n)
{
adj[hom[i]].pb({cur, abs(cur-hom[i])/pw});
cur += pw;
}
cur = hom[i]-pw;
while(cur>= 0)
{
adj[hom[i]].pb({cur, abs(cur-hom[i])/pw});
cur -= pw;
}
}
else
{
int x = hom[i];
adj[x].pb({conv(x, pw, 0), 0});
adj[x].pb({conv(x, pw, 1), 0});
}
}
bfs(hom[0]);
if(dist[hom[1]] == 1e9) printf("-1\n");
else printf("%d\n", dist[hom[1]]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &hom[i], &mana[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1076 KB |
Output is correct |
2 |
Correct |
3 ms |
1080 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1080 KB |
Output is correct |
2 |
Correct |
3 ms |
1012 KB |
Output is correct |
3 |
Correct |
2 ms |
1016 KB |
Output is correct |
4 |
Correct |
3 ms |
1060 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
3 ms |
1076 KB |
Output is correct |
9 |
Correct |
3 ms |
1144 KB |
Output is correct |
10 |
Correct |
3 ms |
1144 KB |
Output is correct |
11 |
Correct |
4 ms |
1272 KB |
Output is correct |
12 |
Correct |
2 ms |
1272 KB |
Output is correct |
13 |
Correct |
3 ms |
1272 KB |
Output is correct |
14 |
Correct |
4 ms |
1272 KB |
Output is correct |
15 |
Correct |
4 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1088 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
988 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1144 KB |
Output is correct |
6 |
Correct |
3 ms |
1088 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
3 ms |
1144 KB |
Output is correct |
9 |
Correct |
3 ms |
1144 KB |
Output is correct |
10 |
Correct |
3 ms |
1208 KB |
Output is correct |
11 |
Correct |
5 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
1244 KB |
Output is correct |
13 |
Correct |
3 ms |
1272 KB |
Output is correct |
14 |
Correct |
4 ms |
1320 KB |
Output is correct |
15 |
Correct |
4 ms |
1272 KB |
Output is correct |
16 |
Correct |
3 ms |
1400 KB |
Output is correct |
17 |
Correct |
6 ms |
2168 KB |
Output is correct |
18 |
Correct |
5 ms |
3388 KB |
Output is correct |
19 |
Correct |
5 ms |
3708 KB |
Output is correct |
20 |
Correct |
6 ms |
3832 KB |
Output is correct |
21 |
Correct |
3 ms |
1528 KB |
Output is correct |
22 |
Correct |
5 ms |
3448 KB |
Output is correct |
23 |
Correct |
5 ms |
3292 KB |
Output is correct |
24 |
Correct |
7 ms |
3708 KB |
Output is correct |
25 |
Correct |
6 ms |
3832 KB |
Output is correct |
26 |
Correct |
6 ms |
3816 KB |
Output is correct |
27 |
Correct |
6 ms |
3832 KB |
Output is correct |
28 |
Correct |
6 ms |
3832 KB |
Output is correct |
29 |
Correct |
11 ms |
3832 KB |
Output is correct |
30 |
Correct |
7 ms |
3704 KB |
Output is correct |
31 |
Correct |
9 ms |
3832 KB |
Output is correct |
32 |
Correct |
8 ms |
3804 KB |
Output is correct |
33 |
Correct |
18 ms |
3832 KB |
Output is correct |
34 |
Correct |
18 ms |
3832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1060 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1092 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1020 KB |
Output is correct |
8 |
Correct |
3 ms |
1016 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
3 ms |
1272 KB |
Output is correct |
11 |
Correct |
4 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
1272 KB |
Output is correct |
13 |
Correct |
3 ms |
1272 KB |
Output is correct |
14 |
Correct |
4 ms |
1272 KB |
Output is correct |
15 |
Correct |
5 ms |
1272 KB |
Output is correct |
16 |
Correct |
3 ms |
1272 KB |
Output is correct |
17 |
Correct |
6 ms |
2168 KB |
Output is correct |
18 |
Correct |
5 ms |
3448 KB |
Output is correct |
19 |
Correct |
5 ms |
3832 KB |
Output is correct |
20 |
Correct |
7 ms |
3832 KB |
Output is correct |
21 |
Correct |
4 ms |
1528 KB |
Output is correct |
22 |
Correct |
5 ms |
3448 KB |
Output is correct |
23 |
Correct |
5 ms |
3192 KB |
Output is correct |
24 |
Correct |
7 ms |
3704 KB |
Output is correct |
25 |
Correct |
6 ms |
3832 KB |
Output is correct |
26 |
Correct |
6 ms |
3832 KB |
Output is correct |
27 |
Correct |
6 ms |
3836 KB |
Output is correct |
28 |
Correct |
6 ms |
3832 KB |
Output is correct |
29 |
Correct |
11 ms |
3832 KB |
Output is correct |
30 |
Correct |
7 ms |
3704 KB |
Output is correct |
31 |
Correct |
8 ms |
3832 KB |
Output is correct |
32 |
Correct |
8 ms |
3772 KB |
Output is correct |
33 |
Correct |
18 ms |
3924 KB |
Output is correct |
34 |
Correct |
41 ms |
3832 KB |
Output is correct |
35 |
Correct |
20 ms |
3820 KB |
Output is correct |
36 |
Correct |
6 ms |
2680 KB |
Output is correct |
37 |
Correct |
28 ms |
4920 KB |
Output is correct |
38 |
Correct |
27 ms |
4984 KB |
Output is correct |
39 |
Correct |
29 ms |
4968 KB |
Output is correct |
40 |
Correct |
28 ms |
5092 KB |
Output is correct |
41 |
Correct |
27 ms |
4988 KB |
Output is correct |
42 |
Correct |
12 ms |
4848 KB |
Output is correct |
43 |
Correct |
12 ms |
4720 KB |
Output is correct |
44 |
Correct |
7 ms |
4804 KB |
Output is correct |
45 |
Correct |
61 ms |
6236 KB |
Output is correct |
46 |
Correct |
61 ms |
6376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1120 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1064 KB |
Output is correct |
4 |
Correct |
3 ms |
1016 KB |
Output is correct |
5 |
Correct |
3 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
3 ms |
1144 KB |
Output is correct |
9 |
Correct |
3 ms |
1144 KB |
Output is correct |
10 |
Correct |
3 ms |
1208 KB |
Output is correct |
11 |
Correct |
4 ms |
1272 KB |
Output is correct |
12 |
Correct |
3 ms |
1272 KB |
Output is correct |
13 |
Correct |
3 ms |
1272 KB |
Output is correct |
14 |
Correct |
4 ms |
1300 KB |
Output is correct |
15 |
Correct |
4 ms |
1216 KB |
Output is correct |
16 |
Correct |
3 ms |
1272 KB |
Output is correct |
17 |
Correct |
6 ms |
2168 KB |
Output is correct |
18 |
Correct |
5 ms |
3448 KB |
Output is correct |
19 |
Correct |
5 ms |
3832 KB |
Output is correct |
20 |
Correct |
6 ms |
3832 KB |
Output is correct |
21 |
Correct |
3 ms |
1528 KB |
Output is correct |
22 |
Correct |
5 ms |
3448 KB |
Output is correct |
23 |
Correct |
5 ms |
3192 KB |
Output is correct |
24 |
Correct |
7 ms |
3704 KB |
Output is correct |
25 |
Correct |
6 ms |
3804 KB |
Output is correct |
26 |
Correct |
6 ms |
3948 KB |
Output is correct |
27 |
Correct |
6 ms |
3892 KB |
Output is correct |
28 |
Correct |
6 ms |
3832 KB |
Output is correct |
29 |
Correct |
11 ms |
3832 KB |
Output is correct |
30 |
Correct |
6 ms |
3704 KB |
Output is correct |
31 |
Correct |
9 ms |
3832 KB |
Output is correct |
32 |
Correct |
8 ms |
3832 KB |
Output is correct |
33 |
Correct |
19 ms |
3832 KB |
Output is correct |
34 |
Correct |
18 ms |
3832 KB |
Output is correct |
35 |
Correct |
20 ms |
3832 KB |
Output is correct |
36 |
Correct |
7 ms |
2680 KB |
Output is correct |
37 |
Correct |
28 ms |
4868 KB |
Output is correct |
38 |
Correct |
28 ms |
4984 KB |
Output is correct |
39 |
Correct |
29 ms |
4984 KB |
Output is correct |
40 |
Correct |
28 ms |
5084 KB |
Output is correct |
41 |
Correct |
28 ms |
4984 KB |
Output is correct |
42 |
Correct |
12 ms |
4720 KB |
Output is correct |
43 |
Correct |
12 ms |
4800 KB |
Output is correct |
44 |
Correct |
12 ms |
4720 KB |
Output is correct |
45 |
Correct |
61 ms |
6264 KB |
Output is correct |
46 |
Correct |
61 ms |
6328 KB |
Output is correct |
47 |
Correct |
117 ms |
22392 KB |
Output is correct |
48 |
Correct |
39 ms |
34040 KB |
Output is correct |
49 |
Correct |
41 ms |
36896 KB |
Output is correct |
50 |
Correct |
41 ms |
40028 KB |
Output is correct |
51 |
Correct |
84 ms |
44380 KB |
Output is correct |
52 |
Correct |
90 ms |
44372 KB |
Output is correct |
53 |
Correct |
51 ms |
42508 KB |
Output is correct |
54 |
Correct |
36 ms |
41792 KB |
Output is correct |
55 |
Correct |
36 ms |
41780 KB |
Output is correct |
56 |
Correct |
49 ms |
43208 KB |
Output is correct |
57 |
Correct |
127 ms |
41964 KB |
Output is correct |
58 |
Correct |
45 ms |
42732 KB |
Output is correct |
59 |
Correct |
48 ms |
42804 KB |
Output is correct |
60 |
Correct |
54 ms |
43120 KB |
Output is correct |
61 |
Correct |
51 ms |
43064 KB |
Output is correct |
62 |
Correct |
67 ms |
44704 KB |
Output is correct |
63 |
Correct |
121 ms |
66028 KB |
Output is correct |
64 |
Correct |
132 ms |
74548 KB |
Output is correct |
65 |
Correct |
294 ms |
72856 KB |
Output is correct |
66 |
Correct |
801 ms |
67632 KB |
Output is correct |
67 |
Correct |
810 ms |
67184 KB |
Output is correct |