#include <iostream>
#include <bits/stdc++.h>
#define ll int
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
const int B = 174;
vector<ll> dijkstra(int n, int s, vector<vector<pair<int, short>>> &adj){
vector<ll> dist(n+1, INF);
dist[s] = 0;
vector<bool> vis(n+1, false);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
while(!pq.empty()){
ll d_u = pq.top().first;
int u = pq.top().second;
pq.pop();
if(vis[u]){
continue;
}
vis[u] = true;
for(auto it : adj[u]){
int v = it.first;
ll w = it.second;
if(vis[v]) continue;
if(dist[v] > d_u+w){
dist[v] = d_u+w;
pq.push({dist[v], v});
}
}
}
return dist;
}
void solve(){
int n, m;
cin >> n >> m;
vector<ll> b(m+1), p(m+1);
for(int i = 1; i <= m; i++){
cin >> b[i] >> p[i];
b[i]++;
}
int c = 0;
vector<vector<int>> cv(n+1, vector<int>(B+1));
vector<vector<bool>> have_doge(n+1, vector<bool>(B+1, false));
for(int i = 1; i <= m; i++){
if(p[i] <= B){
have_doge[b[i]][p[i]] = true;
}
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= B; j++){
c++;
cv[i][j] = c;
}
}
int nb = c;
auto valid = [&](int x) -> bool{
return (x >= 1 && x <= n);
};
vector<vector<pair<int, short>>> adj(nb+1);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= B; j++){
int u, v;
u = cv[i][0];
v = cv[i][j];
if(have_doge[i][j]){
adj[u].push_back({v, 0});
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= B; j++){
int u, v;
if(valid(i+j)){
u = cv[i][j];
v = cv[i+j][0];
adj[u].push_back({v, 1});
v = cv[i+j][j];
adj[u].push_back({v, 1});
}
if(valid(i-j)){
u = cv[i][j];
v = cv[i-j][0];
adj[u].push_back({v, 1});
v = cv[i-j][j];
adj[u].push_back({v, 1});
}
}
}
for(int i = 1; i <= m; i++){
int pos, u, v;
pos = b[i];
if(p[i] <= B) continue;
for(int j = -B; j <= B; j++){
if(valid(pos+j*p[i]) && j != 0){
u = cv[pos][0];
v = cv[pos+j*p[i]][0];
adj[u].push_back({v, abs(j)});
}
}
}
for(int j = 1; j <= n; j++){
if(abs(b[1]-j)%p[1] == 0){
ll w = abs(b[1]-j)/p[1];
adj[0].push_back({cv[j][0], w});
}
}
int S = 0, T = cv[b[2]][0];
vector<ll> dist = dijkstra(nb, S, adj);
ll ans = dist[T];
if(ans == INF){
ans = -1;
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
Compilation message
skyscraper.cpp:5:36: warning: overflow in conversion from 'double' to 'int' changes value from '4.0e+18' to '2147483647' [-Woverflow]
5 | const ll maxn = 2*1e5+5, INF = 4e18+9;
| ~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1220 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
2 ms |
1376 KB |
Output is correct |
16 |
Correct |
3 ms |
2652 KB |
Output is correct |
17 |
Correct |
18 ms |
10220 KB |
Output is correct |
18 |
Correct |
33 ms |
23940 KB |
Output is correct |
19 |
Correct |
37 ms |
27472 KB |
Output is correct |
20 |
Correct |
47 ms |
27560 KB |
Output is correct |
21 |
Correct |
7 ms |
5208 KB |
Output is correct |
22 |
Correct |
39 ms |
24412 KB |
Output is correct |
23 |
Correct |
29 ms |
21844 KB |
Output is correct |
24 |
Correct |
38 ms |
26224 KB |
Output is correct |
25 |
Correct |
40 ms |
27424 KB |
Output is correct |
26 |
Correct |
37 ms |
27480 KB |
Output is correct |
27 |
Correct |
39 ms |
27472 KB |
Output is correct |
28 |
Correct |
39 ms |
27484 KB |
Output is correct |
29 |
Correct |
47 ms |
27604 KB |
Output is correct |
30 |
Correct |
41 ms |
27488 KB |
Output is correct |
31 |
Correct |
43 ms |
27484 KB |
Output is correct |
32 |
Correct |
44 ms |
27480 KB |
Output is correct |
33 |
Correct |
48 ms |
27480 KB |
Output is correct |
34 |
Correct |
49 ms |
27480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
2 ms |
1372 KB |
Output is correct |
16 |
Correct |
3 ms |
2652 KB |
Output is correct |
17 |
Correct |
16 ms |
10276 KB |
Output is correct |
18 |
Correct |
32 ms |
23872 KB |
Output is correct |
19 |
Correct |
37 ms |
27472 KB |
Output is correct |
20 |
Correct |
39 ms |
27532 KB |
Output is correct |
21 |
Correct |
7 ms |
5212 KB |
Output is correct |
22 |
Correct |
35 ms |
24388 KB |
Output is correct |
23 |
Correct |
31 ms |
21852 KB |
Output is correct |
24 |
Correct |
37 ms |
26196 KB |
Output is correct |
25 |
Correct |
38 ms |
27472 KB |
Output is correct |
26 |
Correct |
41 ms |
27484 KB |
Output is correct |
27 |
Correct |
39 ms |
27472 KB |
Output is correct |
28 |
Correct |
41 ms |
27472 KB |
Output is correct |
29 |
Correct |
50 ms |
27472 KB |
Output is correct |
30 |
Correct |
43 ms |
27476 KB |
Output is correct |
31 |
Correct |
44 ms |
27616 KB |
Output is correct |
32 |
Correct |
42 ms |
27580 KB |
Output is correct |
33 |
Correct |
54 ms |
27476 KB |
Output is correct |
34 |
Correct |
58 ms |
27476 KB |
Output is correct |
35 |
Correct |
44 ms |
20560 KB |
Output is correct |
36 |
Correct |
23 ms |
14936 KB |
Output is correct |
37 |
Correct |
69 ms |
27732 KB |
Output is correct |
38 |
Correct |
67 ms |
28576 KB |
Output is correct |
39 |
Correct |
62 ms |
28832 KB |
Output is correct |
40 |
Correct |
64 ms |
28780 KB |
Output is correct |
41 |
Correct |
70 ms |
28756 KB |
Output is correct |
42 |
Correct |
42 ms |
27948 KB |
Output is correct |
43 |
Correct |
41 ms |
27996 KB |
Output is correct |
44 |
Correct |
39 ms |
28052 KB |
Output is correct |
45 |
Correct |
103 ms |
30032 KB |
Output is correct |
46 |
Correct |
86 ms |
30136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1372 KB |
Output is correct |
16 |
Correct |
3 ms |
2652 KB |
Output is correct |
17 |
Correct |
15 ms |
10180 KB |
Output is correct |
18 |
Correct |
31 ms |
24060 KB |
Output is correct |
19 |
Correct |
34 ms |
27480 KB |
Output is correct |
20 |
Correct |
36 ms |
27476 KB |
Output is correct |
21 |
Correct |
7 ms |
5208 KB |
Output is correct |
22 |
Correct |
31 ms |
24412 KB |
Output is correct |
23 |
Correct |
32 ms |
21980 KB |
Output is correct |
24 |
Correct |
40 ms |
26204 KB |
Output is correct |
25 |
Correct |
39 ms |
27472 KB |
Output is correct |
26 |
Correct |
36 ms |
27484 KB |
Output is correct |
27 |
Correct |
36 ms |
27484 KB |
Output is correct |
28 |
Correct |
38 ms |
27540 KB |
Output is correct |
29 |
Correct |
47 ms |
27484 KB |
Output is correct |
30 |
Correct |
39 ms |
27532 KB |
Output is correct |
31 |
Correct |
42 ms |
27484 KB |
Output is correct |
32 |
Correct |
37 ms |
27472 KB |
Output is correct |
33 |
Correct |
47 ms |
27480 KB |
Output is correct |
34 |
Correct |
47 ms |
27640 KB |
Output is correct |
35 |
Correct |
43 ms |
20564 KB |
Output is correct |
36 |
Correct |
23 ms |
15060 KB |
Output is correct |
37 |
Correct |
55 ms |
27860 KB |
Output is correct |
38 |
Correct |
65 ms |
28752 KB |
Output is correct |
39 |
Correct |
69 ms |
28752 KB |
Output is correct |
40 |
Correct |
64 ms |
28844 KB |
Output is correct |
41 |
Correct |
74 ms |
28820 KB |
Output is correct |
42 |
Correct |
38 ms |
27988 KB |
Output is correct |
43 |
Correct |
37 ms |
27984 KB |
Output is correct |
44 |
Correct |
37 ms |
27984 KB |
Output is correct |
45 |
Correct |
78 ms |
30036 KB |
Output is correct |
46 |
Correct |
79 ms |
30140 KB |
Output is correct |
47 |
Correct |
403 ms |
174640 KB |
Output is correct |
48 |
Runtime error |
299 ms |
262144 KB |
Execution killed with signal 9 |
49 |
Halted |
0 ms |
0 KB |
- |