#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (1LL << 55)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 30111
#define SQRT 150
typedef long long ll;
typedef long double ld;
vector<pair<pair<int, int>, int> > adj[maxn][SQRT + 10];
vector<int> doge[maxn];
ll d[maxn][SQRT + 10];
priority_queue<pair<int, pair<int, int> > > pq;
int main(){
for(int i = 0; i < maxn; i++)
for(int j = 0; j < SQRT; j++)
d[i][j] = INF;
int n, m, z, k;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++){
int b, p;
scanf("%d%d", &b, &p);
if(i == 0)
z = b;
if(i == 1)
k = b;
if(p < SQRT)
adj[b][0].pb(mp(mp(b, p), 0));
if(p >= SQRT)
doge[b].pb(p);
}
if(m == 1){
printf("-1\n");
return 0;
}
d[z][0] = 0;
pq.push(mp(0, mp(z, 0)));
while(!pq.empty()){
int x = pq.top().se.fi;
int y = pq.top().se.se;
int val = -pq.top().fi;
pq.pop();
if(val > d[x][y])
continue;
if(x == k)
break;
if(x + y < n){
if(d[x + y][y] > d[x][y] + 1){
d[x + y][y] = d[x][y] + 1;
pq.push(mp(-d[x + y][y], mp(x + y, y)));
}
if(d[x + y][0] > d[x][y] + 1){
d[x + y][0] = d[x][y] + 1;
pq.push(mp(-d[x + y][0], mp(x + y, 0)));
}
}
if(x - y >= 0){
if(d[x - y][y] > d[x][y] + 1){
d[x - y][y] = d[x][y] + 1;
pq.push(mp(-d[x - y][y], mp(x - y, y)));
}
if(d[x - y][0] > d[x][y] + 1){
d[x - y][0] = d[x][y] + 1;
pq.push(mp(-d[x - y][0], mp(x - y, 0)));
}
}
for(auto v : adj[x][y]){
int a = v.fi.fi;
int b = v.fi.se;
int t = v.se;
if(d[a][b] > d[x][y] + t){
d[a][b] = d[x][y] + t;
pq.push(mp(-d[a][b], mp(a, b)));
}
}
if(y == 0){
for(int v : doge[x]){
int p = v;
for(int j = 1; x + j * p < n; j++){
int a = x + j * p;
if(d[a][0] > d[x][y] + j){
d[a][0] = d[x][y] + j;
pq.push(mp(-d[a][0], mp(a, 0)));
}
}
for(int j = 1; x - j * p >= 0; j++){
int a = x - j * p;
if(d[a][0] > d[x][y] + j){
d[a][0] = d[x][y] + j;
pq.push(mp(-d[a][0], mp(a, 0)));
}
}
}
}
}
if(d[k][0] == INF)
d[k][0] = -1;
printf("%lld\n", d[k][0]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:30: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:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &b, &p);
~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:64:3: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(x == k)
^~
skyscraper.cpp:54:12: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
pq.push(mp(0, mp(z, 0)));
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
151928 KB |
Output is correct |
2 |
Correct |
120 ms |
152168 KB |
Output is correct |
3 |
Correct |
116 ms |
152168 KB |
Output is correct |
4 |
Correct |
116 ms |
152300 KB |
Output is correct |
5 |
Correct |
116 ms |
152300 KB |
Output is correct |
6 |
Correct |
114 ms |
152300 KB |
Output is correct |
7 |
Correct |
116 ms |
152432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
152432 KB |
Output is correct |
2 |
Correct |
118 ms |
152432 KB |
Output is correct |
3 |
Correct |
117 ms |
152564 KB |
Output is correct |
4 |
Correct |
123 ms |
152564 KB |
Output is correct |
5 |
Correct |
118 ms |
152564 KB |
Output is correct |
6 |
Correct |
148 ms |
152564 KB |
Output is correct |
7 |
Correct |
124 ms |
152564 KB |
Output is correct |
8 |
Correct |
143 ms |
152604 KB |
Output is correct |
9 |
Correct |
126 ms |
152604 KB |
Output is correct |
10 |
Correct |
146 ms |
152604 KB |
Output is correct |
11 |
Correct |
120 ms |
152604 KB |
Output is correct |
12 |
Correct |
122 ms |
152604 KB |
Output is correct |
13 |
Correct |
117 ms |
152604 KB |
Output is correct |
14 |
Correct |
118 ms |
152604 KB |
Output is correct |
15 |
Correct |
124 ms |
152704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
152704 KB |
Output is correct |
2 |
Correct |
119 ms |
152704 KB |
Output is correct |
3 |
Correct |
122 ms |
152704 KB |
Output is correct |
4 |
Correct |
118 ms |
152716 KB |
Output is correct |
5 |
Correct |
120 ms |
152716 KB |
Output is correct |
6 |
Correct |
116 ms |
152752 KB |
Output is correct |
7 |
Correct |
118 ms |
152752 KB |
Output is correct |
8 |
Correct |
118 ms |
152752 KB |
Output is correct |
9 |
Correct |
122 ms |
152752 KB |
Output is correct |
10 |
Correct |
118 ms |
152768 KB |
Output is correct |
11 |
Correct |
116 ms |
153068 KB |
Output is correct |
12 |
Correct |
116 ms |
153068 KB |
Output is correct |
13 |
Correct |
118 ms |
153068 KB |
Output is correct |
14 |
Correct |
117 ms |
153068 KB |
Output is correct |
15 |
Correct |
118 ms |
153068 KB |
Output is correct |
16 |
Correct |
115 ms |
153068 KB |
Output is correct |
17 |
Correct |
116 ms |
153068 KB |
Output is correct |
18 |
Correct |
115 ms |
153068 KB |
Output is correct |
19 |
Correct |
116 ms |
153068 KB |
Output is correct |
20 |
Correct |
123 ms |
153068 KB |
Output is correct |
21 |
Correct |
116 ms |
153068 KB |
Output is correct |
22 |
Correct |
122 ms |
153068 KB |
Output is correct |
23 |
Correct |
120 ms |
153068 KB |
Output is correct |
24 |
Correct |
136 ms |
153068 KB |
Output is correct |
25 |
Correct |
124 ms |
153068 KB |
Output is correct |
26 |
Correct |
123 ms |
153096 KB |
Output is correct |
27 |
Correct |
122 ms |
153096 KB |
Output is correct |
28 |
Correct |
120 ms |
153096 KB |
Output is correct |
29 |
Correct |
128 ms |
153096 KB |
Output is correct |
30 |
Correct |
121 ms |
153108 KB |
Output is correct |
31 |
Correct |
128 ms |
153108 KB |
Output is correct |
32 |
Correct |
124 ms |
153108 KB |
Output is correct |
33 |
Correct |
135 ms |
153108 KB |
Output is correct |
34 |
Correct |
130 ms |
153184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
153184 KB |
Output is correct |
2 |
Correct |
133 ms |
153184 KB |
Output is correct |
3 |
Correct |
117 ms |
153184 KB |
Output is correct |
4 |
Correct |
118 ms |
153184 KB |
Output is correct |
5 |
Correct |
116 ms |
153184 KB |
Output is correct |
6 |
Correct |
119 ms |
153184 KB |
Output is correct |
7 |
Correct |
119 ms |
153184 KB |
Output is correct |
8 |
Correct |
121 ms |
153184 KB |
Output is correct |
9 |
Correct |
117 ms |
153184 KB |
Output is correct |
10 |
Correct |
119 ms |
153184 KB |
Output is correct |
11 |
Correct |
118 ms |
153192 KB |
Output is correct |
12 |
Correct |
119 ms |
153200 KB |
Output is correct |
13 |
Correct |
118 ms |
153200 KB |
Output is correct |
14 |
Correct |
121 ms |
153308 KB |
Output is correct |
15 |
Correct |
121 ms |
153308 KB |
Output is correct |
16 |
Correct |
122 ms |
153308 KB |
Output is correct |
17 |
Correct |
121 ms |
153308 KB |
Output is correct |
18 |
Correct |
119 ms |
153308 KB |
Output is correct |
19 |
Correct |
119 ms |
153308 KB |
Output is correct |
20 |
Correct |
120 ms |
153316 KB |
Output is correct |
21 |
Correct |
147 ms |
153316 KB |
Output is correct |
22 |
Correct |
129 ms |
153316 KB |
Output is correct |
23 |
Correct |
121 ms |
153316 KB |
Output is correct |
24 |
Correct |
138 ms |
153384 KB |
Output is correct |
25 |
Correct |
120 ms |
153384 KB |
Output is correct |
26 |
Correct |
122 ms |
153424 KB |
Output is correct |
27 |
Correct |
122 ms |
153424 KB |
Output is correct |
28 |
Correct |
121 ms |
153496 KB |
Output is correct |
29 |
Correct |
130 ms |
153496 KB |
Output is correct |
30 |
Correct |
133 ms |
153496 KB |
Output is correct |
31 |
Correct |
137 ms |
153496 KB |
Output is correct |
32 |
Correct |
125 ms |
153496 KB |
Output is correct |
33 |
Correct |
147 ms |
153496 KB |
Output is correct |
34 |
Correct |
132 ms |
153496 KB |
Output is correct |
35 |
Correct |
135 ms |
153952 KB |
Output is correct |
36 |
Correct |
129 ms |
153952 KB |
Output is correct |
37 |
Correct |
125 ms |
154204 KB |
Output is correct |
38 |
Correct |
133 ms |
154524 KB |
Output is correct |
39 |
Correct |
129 ms |
154628 KB |
Output is correct |
40 |
Correct |
130 ms |
154892 KB |
Output is correct |
41 |
Correct |
134 ms |
155276 KB |
Output is correct |
42 |
Correct |
131 ms |
155632 KB |
Output is correct |
43 |
Correct |
130 ms |
155820 KB |
Output is correct |
44 |
Correct |
128 ms |
156048 KB |
Output is correct |
45 |
Correct |
179 ms |
156348 KB |
Output is correct |
46 |
Correct |
147 ms |
156528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
156528 KB |
Output is correct |
2 |
Correct |
119 ms |
156528 KB |
Output is correct |
3 |
Correct |
120 ms |
156528 KB |
Output is correct |
4 |
Correct |
120 ms |
156528 KB |
Output is correct |
5 |
Correct |
117 ms |
156528 KB |
Output is correct |
6 |
Correct |
120 ms |
156528 KB |
Output is correct |
7 |
Correct |
116 ms |
156528 KB |
Output is correct |
8 |
Correct |
118 ms |
156528 KB |
Output is correct |
9 |
Correct |
119 ms |
156528 KB |
Output is correct |
10 |
Correct |
122 ms |
156528 KB |
Output is correct |
11 |
Correct |
117 ms |
156528 KB |
Output is correct |
12 |
Correct |
121 ms |
156528 KB |
Output is correct |
13 |
Correct |
118 ms |
156528 KB |
Output is correct |
14 |
Correct |
122 ms |
156528 KB |
Output is correct |
15 |
Correct |
119 ms |
156528 KB |
Output is correct |
16 |
Correct |
121 ms |
156528 KB |
Output is correct |
17 |
Correct |
118 ms |
156528 KB |
Output is correct |
18 |
Correct |
117 ms |
156528 KB |
Output is correct |
19 |
Correct |
115 ms |
156528 KB |
Output is correct |
20 |
Correct |
119 ms |
156528 KB |
Output is correct |
21 |
Correct |
119 ms |
156528 KB |
Output is correct |
22 |
Correct |
119 ms |
156528 KB |
Output is correct |
23 |
Correct |
120 ms |
156528 KB |
Output is correct |
24 |
Correct |
119 ms |
156528 KB |
Output is correct |
25 |
Correct |
122 ms |
156528 KB |
Output is correct |
26 |
Correct |
120 ms |
156528 KB |
Output is correct |
27 |
Correct |
119 ms |
156528 KB |
Output is correct |
28 |
Correct |
130 ms |
156528 KB |
Output is correct |
29 |
Correct |
167 ms |
156528 KB |
Output is correct |
30 |
Correct |
123 ms |
156528 KB |
Output is correct |
31 |
Correct |
124 ms |
156528 KB |
Output is correct |
32 |
Correct |
128 ms |
156528 KB |
Output is correct |
33 |
Correct |
138 ms |
156528 KB |
Output is correct |
34 |
Correct |
132 ms |
156528 KB |
Output is correct |
35 |
Correct |
126 ms |
156852 KB |
Output is correct |
36 |
Correct |
117 ms |
156852 KB |
Output is correct |
37 |
Correct |
123 ms |
156984 KB |
Output is correct |
38 |
Correct |
129 ms |
157388 KB |
Output is correct |
39 |
Correct |
141 ms |
157512 KB |
Output is correct |
40 |
Correct |
136 ms |
157860 KB |
Output is correct |
41 |
Correct |
130 ms |
158192 KB |
Output is correct |
42 |
Correct |
141 ms |
158528 KB |
Output is correct |
43 |
Correct |
128 ms |
158720 KB |
Output is correct |
44 |
Correct |
128 ms |
158956 KB |
Output is correct |
45 |
Correct |
181 ms |
159232 KB |
Output is correct |
46 |
Correct |
150 ms |
159424 KB |
Output is correct |
47 |
Correct |
134 ms |
159956 KB |
Output is correct |
48 |
Correct |
123 ms |
159956 KB |
Output is correct |
49 |
Correct |
124 ms |
159980 KB |
Output is correct |
50 |
Correct |
123 ms |
159988 KB |
Output is correct |
51 |
Correct |
142 ms |
161088 KB |
Output is correct |
52 |
Correct |
146 ms |
161596 KB |
Output is correct |
53 |
Correct |
131 ms |
161596 KB |
Output is correct |
54 |
Correct |
123 ms |
161596 KB |
Output is correct |
55 |
Correct |
125 ms |
161596 KB |
Output is correct |
56 |
Correct |
146 ms |
161812 KB |
Output is correct |
57 |
Correct |
122 ms |
161812 KB |
Output is correct |
58 |
Correct |
138 ms |
161812 KB |
Output is correct |
59 |
Correct |
137 ms |
161904 KB |
Output is correct |
60 |
Correct |
138 ms |
162096 KB |
Output is correct |
61 |
Correct |
135 ms |
162292 KB |
Output is correct |
62 |
Correct |
183 ms |
163220 KB |
Output is correct |
63 |
Correct |
169 ms |
163584 KB |
Output is correct |
64 |
Correct |
177 ms |
163704 KB |
Output is correct |
65 |
Correct |
417 ms |
164660 KB |
Output is correct |
66 |
Execution timed out |
1088 ms |
165028 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |