#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 3e4+5;
const ll INF = 1e18;
vector<pii> adj[N];
ll dis[N];
int main(){
int n, m;
scanf("%d%d", &n, &m);
int s, t;
for(int i = 1; i <= m; ++i){
int x, p;
scanf("%d%d", &x, &p), x++;
if (i == 1)
s = x;
if (i == 2)
t = x;
for(int ad = 1;; ++ad){
if(x-ad*p < 1 and x+ad*p > n)
break;
if(x-ad*p >= 1)
adj[x].pb({x-ad*p, ad});
if(x+ad*p <= n)
adj[x].pb({x+ad*p, ad});
}
}
for(int i = 1; i <= n; ++i)
dis[i] = INF;
priority_queue<pli, vector<pli>, greater<pli>> pq;
pq.push({0, s});
dis[s] = 0;
while(!pq.empty()){
pli P = pq.top();
pq.pop();
ll val = P.ff;
int x = P.ss;
if(dis[x] != val)
continue;
tr(it, adj[x]){
int u = it->ff, w = it->ss;
if(umin(dis[u], dis[x]+w))
pq.push({dis[u], u});
}
}
if(dis[t] == INF)
puts("-1");
else
printf("%lld\n", dis[t]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:26:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d%d", &x, &p), x++;
| ~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:44:12: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
44 | dis[s] = 0;
| ~~~~~~~^~~
skyscraper.cpp:58:13: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
58 | if(dis[t] == INF)
| ~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1148 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1120 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
4 ms |
3280 KB |
Output is correct |
13 |
Correct |
4 ms |
3276 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
2 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1148 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
5 ms |
3276 KB |
Output is correct |
13 |
Correct |
5 ms |
3280 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1120 KB |
Output is correct |
20 |
Correct |
52 ms |
33308 KB |
Output is correct |
21 |
Correct |
1 ms |
1120 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
49 ms |
34172 KB |
Output is correct |
27 |
Correct |
56 ms |
34164 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
2140 KB |
Output is correct |
30 |
Correct |
2 ms |
1408 KB |
Output is correct |
31 |
Correct |
2 ms |
1628 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
2780 KB |
Output is correct |
34 |
Correct |
6 ms |
2776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
3 ms |
3288 KB |
Output is correct |
13 |
Correct |
4 ms |
3284 KB |
Output is correct |
14 |
Correct |
1 ms |
1120 KB |
Output is correct |
15 |
Correct |
1 ms |
1124 KB |
Output is correct |
16 |
Correct |
1 ms |
1124 KB |
Output is correct |
17 |
Correct |
1 ms |
1380 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
47 ms |
33076 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
50 ms |
34104 KB |
Output is correct |
27 |
Correct |
51 ms |
33984 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
2140 KB |
Output is correct |
30 |
Correct |
2 ms |
1408 KB |
Output is correct |
31 |
Correct |
2 ms |
1628 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
4 ms |
2776 KB |
Output is correct |
34 |
Correct |
4 ms |
2784 KB |
Output is correct |
35 |
Correct |
11 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
10 ms |
4644 KB |
Output is correct |
38 |
Correct |
13 ms |
4232 KB |
Output is correct |
39 |
Correct |
11 ms |
4188 KB |
Output is correct |
40 |
Correct |
12 ms |
4036 KB |
Output is correct |
41 |
Correct |
12 ms |
3932 KB |
Output is correct |
42 |
Runtime error |
292 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1144 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
5 ms |
3280 KB |
Output is correct |
13 |
Correct |
5 ms |
3280 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1400 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
48 ms |
33156 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
53 ms |
34256 KB |
Output is correct |
27 |
Correct |
52 ms |
33956 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
2140 KB |
Output is correct |
30 |
Correct |
1 ms |
1628 KB |
Output is correct |
31 |
Correct |
2 ms |
1628 KB |
Output is correct |
32 |
Correct |
1 ms |
1412 KB |
Output is correct |
33 |
Correct |
4 ms |
2776 KB |
Output is correct |
34 |
Correct |
4 ms |
2776 KB |
Output is correct |
35 |
Correct |
6 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
8 ms |
4696 KB |
Output is correct |
38 |
Correct |
9 ms |
4072 KB |
Output is correct |
39 |
Correct |
8 ms |
4188 KB |
Output is correct |
40 |
Correct |
9 ms |
4188 KB |
Output is correct |
41 |
Correct |
9 ms |
4036 KB |
Output is correct |
42 |
Runtime error |
314 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |