#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
constexpr int INF = 1e9+100;
struct my_bitset{
unsigned long long bit[500];
bool find(int x){return bit[x/64]&(1LL<<(x&63));}
void insert(int x){bit[x/64] |= (1LL<<(x&63));}
};
int dist[30030], visited[30030];
my_bitset st[30030];
vector<int> a[30030];
struct Vertex{
int s, p;
Vertex(){}
Vertex(int _s, int _p): s(_s), p(_p) {}
//bool operator < (const Vertex &V) const{return d > V.d;}
};
int main(){
int n, m, s, e;
scanf("%d %d", &n, &m);
for (int i=1;i<=m;i++){
int x, p;
scanf("%d %d", &x, &p);
++x;
a[x].push_back(p);
if (i==1) s = x;
if (i==2) e = x;
}
fill(dist+1, dist+n+1, INF);
fill(visited+1, visited+n+1, 0);
dist[s] = 0;
for (int i=1;i<=n;i++){
sort(a[i].begin(), a[i].end());
a[i].erase(unique(a[i].begin(), a[i].end()), a[i].end());
}
stack<Vertex, vector<Vertex>> pq[2];
pq[0].emplace(s, 0);
int d = 0;
while(true){
if (pq[d&1].empty() && pq[(d+1)&1].empty()) break;
if (pq[d&1].empty()) {d++; continue;}
auto [s, p] = pq[d&1].top(); pq[d&1].pop();
dist[s] = min(dist[s], d);
if (p){
if (st[s].find(p)) continue;
st[s].insert(p);
if (s-p > 0) pq[(d+1)&1].emplace(s-p, p);
if (s+p <= n) pq[(d+1)&1].emplace(s+p, p);
}
if (!visited[s]){
visited[s] = 1;
for (auto &p2:a[s]){
st[s].insert(p2);
if (s-p2 > 0) pq[(d+1)&1].emplace(s-p2, p2);
if (s+p2 <= n) pq[(d+1)&1].emplace(s+p2, p2);
}
}
}
/*for (int z=1;z<=n;z++){
int mn = INF, idx = -1;
for (int i=1;i<=n;i++) if (!visited[i] && mn > dist[i]){
mn = dist[i];
idx = i;
}
if (idx==-1) break;
visited[idx] = 1;
for (auto &p:a[idx]){
for (int i=idx-p, j=1;i>0;i-=p, j++) dist[i] = min(dist[i], dist[idx] + j);
for (int i=idx+p, j=1;i<=n;i+=p, j++) dist[i] = min(dist[i], dist[idx] + j);
}
}*/
if (dist[e]==INF) printf("-1\n");
else printf("%d\n", dist[e]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:29:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d %d", &x, &p);
| ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:20:40: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
20 | Vertex(int _s, int _p): s(_s), p(_p) {}
| ^
skyscraper.cpp:25:15: note: 's' was declared here
25 | int n, m, s, e;
| ^
skyscraper.cpp:88:15: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
88 | if (dist[e]==INF) printf("-1\n");
| ~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1016 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1020 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1140 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1408 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1016 KB |
Output is correct |
4 |
Correct |
1 ms |
1012 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1140 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
2 ms |
1408 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1492 KB |
Output is correct |
15 |
Correct |
2 ms |
1408 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
4 ms |
3968 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
6 ms |
8916 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
5 ms |
7124 KB |
Output is correct |
24 |
Correct |
6 ms |
8532 KB |
Output is correct |
25 |
Correct |
6 ms |
8868 KB |
Output is correct |
26 |
Correct |
6 ms |
8788 KB |
Output is correct |
27 |
Correct |
6 ms |
8836 KB |
Output is correct |
28 |
Correct |
6 ms |
8916 KB |
Output is correct |
29 |
Correct |
6 ms |
8820 KB |
Output is correct |
30 |
Correct |
5 ms |
8788 KB |
Output is correct |
31 |
Correct |
6 ms |
8788 KB |
Output is correct |
32 |
Correct |
6 ms |
8824 KB |
Output is correct |
33 |
Correct |
8 ms |
8916 KB |
Output is correct |
34 |
Correct |
8 ms |
8956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1016 KB |
Output is correct |
4 |
Correct |
1 ms |
1020 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1144 KB |
Output is correct |
9 |
Correct |
1 ms |
1144 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
2 ms |
1500 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1492 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
4052 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
1020 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
4 ms |
7124 KB |
Output is correct |
24 |
Correct |
6 ms |
8532 KB |
Output is correct |
25 |
Correct |
6 ms |
8916 KB |
Output is correct |
26 |
Correct |
6 ms |
8792 KB |
Output is correct |
27 |
Correct |
6 ms |
8788 KB |
Output is correct |
28 |
Correct |
6 ms |
8944 KB |
Output is correct |
29 |
Correct |
7 ms |
8916 KB |
Output is correct |
30 |
Correct |
5 ms |
8788 KB |
Output is correct |
31 |
Correct |
6 ms |
8916 KB |
Output is correct |
32 |
Correct |
5 ms |
8788 KB |
Output is correct |
33 |
Correct |
8 ms |
8916 KB |
Output is correct |
34 |
Correct |
8 ms |
8912 KB |
Output is correct |
35 |
Correct |
13 ms |
7552 KB |
Output is correct |
36 |
Correct |
5 ms |
5460 KB |
Output is correct |
37 |
Correct |
15 ms |
9900 KB |
Output is correct |
38 |
Correct |
17 ms |
10372 KB |
Output is correct |
39 |
Correct |
17 ms |
10192 KB |
Output is correct |
40 |
Correct |
16 ms |
10240 KB |
Output is correct |
41 |
Correct |
16 ms |
10192 KB |
Output is correct |
42 |
Correct |
11 ms |
9300 KB |
Output is correct |
43 |
Correct |
10 ms |
9300 KB |
Output is correct |
44 |
Correct |
10 ms |
9216 KB |
Output is correct |
45 |
Correct |
21 ms |
10312 KB |
Output is correct |
46 |
Correct |
22 ms |
10304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1012 KB |
Output is correct |
2 |
Correct |
1 ms |
1016 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1404 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1404 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
4052 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
6 ms |
8916 KB |
Output is correct |
21 |
Correct |
1 ms |
1024 KB |
Output is correct |
22 |
Correct |
1 ms |
1020 KB |
Output is correct |
23 |
Correct |
5 ms |
7124 KB |
Output is correct |
24 |
Correct |
6 ms |
8524 KB |
Output is correct |
25 |
Correct |
6 ms |
8812 KB |
Output is correct |
26 |
Correct |
6 ms |
8788 KB |
Output is correct |
27 |
Correct |
6 ms |
8852 KB |
Output is correct |
28 |
Correct |
7 ms |
8916 KB |
Output is correct |
29 |
Correct |
7 ms |
8916 KB |
Output is correct |
30 |
Correct |
6 ms |
8788 KB |
Output is correct |
31 |
Correct |
6 ms |
8916 KB |
Output is correct |
32 |
Correct |
5 ms |
8788 KB |
Output is correct |
33 |
Correct |
8 ms |
8916 KB |
Output is correct |
34 |
Correct |
8 ms |
9000 KB |
Output is correct |
35 |
Correct |
12 ms |
7552 KB |
Output is correct |
36 |
Correct |
5 ms |
5460 KB |
Output is correct |
37 |
Correct |
14 ms |
9900 KB |
Output is correct |
38 |
Correct |
20 ms |
10336 KB |
Output is correct |
39 |
Correct |
20 ms |
10396 KB |
Output is correct |
40 |
Correct |
16 ms |
10212 KB |
Output is correct |
41 |
Correct |
17 ms |
10240 KB |
Output is correct |
42 |
Correct |
10 ms |
9300 KB |
Output is correct |
43 |
Correct |
12 ms |
9316 KB |
Output is correct |
44 |
Correct |
10 ms |
9300 KB |
Output is correct |
45 |
Correct |
22 ms |
10312 KB |
Output is correct |
46 |
Correct |
22 ms |
10312 KB |
Output is correct |
47 |
Correct |
62 ms |
51620 KB |
Output is correct |
48 |
Correct |
8 ms |
1924 KB |
Output is correct |
49 |
Correct |
7 ms |
1792 KB |
Output is correct |
50 |
Correct |
5 ms |
1748 KB |
Output is correct |
51 |
Correct |
95 ms |
119856 KB |
Output is correct |
52 |
Correct |
86 ms |
119896 KB |
Output is correct |
53 |
Correct |
68 ms |
119488 KB |
Output is correct |
54 |
Correct |
33 ms |
61416 KB |
Output is correct |
55 |
Correct |
44 ms |
80512 KB |
Output is correct |
56 |
Correct |
67 ms |
119736 KB |
Output is correct |
57 |
Correct |
79 ms |
118648 KB |
Output is correct |
58 |
Correct |
71 ms |
118808 KB |
Output is correct |
59 |
Correct |
86 ms |
118816 KB |
Output is correct |
60 |
Correct |
75 ms |
118868 KB |
Output is correct |
61 |
Correct |
73 ms |
118888 KB |
Output is correct |
62 |
Correct |
102 ms |
119576 KB |
Output is correct |
63 |
Correct |
142 ms |
119580 KB |
Output is correct |
64 |
Correct |
164 ms |
119488 KB |
Output is correct |
65 |
Correct |
190 ms |
120000 KB |
Output is correct |
66 |
Correct |
293 ms |
120140 KB |
Output is correct |
67 |
Correct |
285 ms |
120128 KB |
Output is correct |