#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()
using ll = long long;
using pii = pair<int,int>;
const int maxn = (int)3e4+10;
const ll LINF = (ll)1e18;
pii a[maxn];
int n, m, ans;
int dis[maxn];
bool vis[maxn];
vector<pii> adj[maxn];
vector<int> doge[maxn];
void dijkstra(int s){
fill(dis,dis+maxn,LINF); fill(vis, vis+maxn,false);
priority_queue<pii,vector<pii>,greater<pii>> pq;
pq.push({0,s}); dis[s]=0;
while(!pq.empty()){
int a = pq.top().se; pq.pop();
if(vis[a]) continue; vis[a]=1;
for(auto u : adj[a]){
int b = u.fi, w = u.se;
if(dis[a]+w<dis[b]){
dis[b]=dis[a]+w;
pq.push({dis[b],b});
}
}
}
}
int32_t main() {
cin >> n >> m; ans = LINF;
for(int i = 0; i < m; i++) cin >> a[i].fi >> a[i].se, doge[a[i].fi].pb(a[i].se);
for(int i = 0; i < n; i++)
sort(all(doge[i])), doge[i].erase(unique(all(doge[i])), doge[i].end());
for(int i = 0; i < n; i++){
for(auto p : doge[i]){
for(int j = 1; ;j++){
int b = i+j*p; if(b>=n) break;
auto itr = lower_bound(doge[b].begin(), doge[b].end(), p)-doge[b].begin();
adj[i].pb({b,j});if(itr!=doge[b].size() and doge[b][itr]==p) break;
}
for(int j = -1; ; j--){
int b = i+j*p; if(b<0) break;
auto itr = lower_bound(doge[b].begin(), doge[b].end(), p)-doge[b].begin();
adj[i].pb({b,-j}); if(itr!=doge[b].size() and doge[b][itr]==p) break;
}
}
}
dijkstra(a[0].fi); cout << (dis[a[1].fi]==LINF?-1:dis[a[1].fi]);
}
Compilation message
skyscraper.cpp: In function 'void dijkstra(long long int)':
skyscraper.cpp:26:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
26 | if(vis[a]) continue; vis[a]=1;
| ^~
skyscraper.cpp:26:24: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
26 | if(vis[a]) continue; vis[a]=1;
| ^~~
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:47:28: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | adj[i].pb({b,j});if(itr!=doge[b].size() and doge[b][itr]==p) break;
| ~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:52:30: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | adj[i].pb({b,-j}); if(itr!=doge[b].size() and doge[b][itr]==p) break;
| ~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1920 KB |
Output is correct |
6 |
Correct |
1 ms |
1972 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1968 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1972 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2084 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
2 ms |
2028 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1972 KB |
Output is correct |
5 |
Correct |
1 ms |
1976 KB |
Output is correct |
6 |
Correct |
1 ms |
1972 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1976 KB |
Output is correct |
11 |
Correct |
3 ms |
2108 KB |
Output is correct |
12 |
Correct |
2 ms |
1980 KB |
Output is correct |
13 |
Correct |
2 ms |
2004 KB |
Output is correct |
14 |
Correct |
2 ms |
2104 KB |
Output is correct |
15 |
Correct |
2 ms |
2156 KB |
Output is correct |
16 |
Correct |
2 ms |
2004 KB |
Output is correct |
17 |
Correct |
3 ms |
2388 KB |
Output is correct |
18 |
Correct |
2 ms |
2108 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2132 KB |
Output is correct |
21 |
Correct |
2 ms |
1980 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2368 KB |
Output is correct |
25 |
Correct |
3 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
3 ms |
2132 KB |
Output is correct |
28 |
Correct |
3 ms |
2516 KB |
Output is correct |
29 |
Correct |
4 ms |
3412 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
3 ms |
2900 KB |
Output is correct |
32 |
Correct |
2 ms |
2608 KB |
Output is correct |
33 |
Correct |
7 ms |
4944 KB |
Output is correct |
34 |
Correct |
7 ms |
4816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1972 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1980 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
2 ms |
1924 KB |
Output is correct |
14 |
Correct |
2 ms |
2132 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
1972 KB |
Output is correct |
17 |
Correct |
3 ms |
2388 KB |
Output is correct |
18 |
Correct |
2 ms |
2056 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2132 KB |
Output is correct |
21 |
Correct |
2 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2364 KB |
Output is correct |
25 |
Correct |
3 ms |
2240 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
5 ms |
2492 KB |
Output is correct |
29 |
Correct |
4 ms |
3424 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
3 ms |
2900 KB |
Output is correct |
32 |
Correct |
3 ms |
2644 KB |
Output is correct |
33 |
Correct |
7 ms |
4944 KB |
Output is correct |
34 |
Correct |
7 ms |
4816 KB |
Output is correct |
35 |
Correct |
17 ms |
4868 KB |
Output is correct |
36 |
Correct |
4 ms |
2448 KB |
Output is correct |
37 |
Correct |
17 ms |
6808 KB |
Output is correct |
38 |
Correct |
22 ms |
6476 KB |
Output is correct |
39 |
Correct |
22 ms |
6568 KB |
Output is correct |
40 |
Correct |
25 ms |
6408 KB |
Output is correct |
41 |
Correct |
22 ms |
6440 KB |
Output is correct |
42 |
Correct |
14 ms |
2968 KB |
Output is correct |
43 |
Correct |
13 ms |
2956 KB |
Output is correct |
44 |
Correct |
14 ms |
2916 KB |
Output is correct |
45 |
Correct |
24 ms |
11536 KB |
Output is correct |
46 |
Correct |
25 ms |
11544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1968 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
1 ms |
1972 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1972 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2108 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
3 ms |
2004 KB |
Output is correct |
14 |
Correct |
2 ms |
2108 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
2004 KB |
Output is correct |
17 |
Correct |
3 ms |
2388 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2112 KB |
Output is correct |
21 |
Correct |
2 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2104 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2260 KB |
Output is correct |
25 |
Correct |
3 ms |
2260 KB |
Output is correct |
26 |
Correct |
2 ms |
2260 KB |
Output is correct |
27 |
Correct |
2 ms |
2132 KB |
Output is correct |
28 |
Correct |
3 ms |
2448 KB |
Output is correct |
29 |
Correct |
4 ms |
3412 KB |
Output is correct |
30 |
Correct |
3 ms |
2516 KB |
Output is correct |
31 |
Correct |
3 ms |
2868 KB |
Output is correct |
32 |
Correct |
2 ms |
2616 KB |
Output is correct |
33 |
Correct |
7 ms |
4944 KB |
Output is correct |
34 |
Correct |
7 ms |
4784 KB |
Output is correct |
35 |
Correct |
17 ms |
4920 KB |
Output is correct |
36 |
Correct |
4 ms |
2500 KB |
Output is correct |
37 |
Correct |
17 ms |
6732 KB |
Output is correct |
38 |
Correct |
22 ms |
6428 KB |
Output is correct |
39 |
Correct |
22 ms |
6524 KB |
Output is correct |
40 |
Correct |
22 ms |
6496 KB |
Output is correct |
41 |
Correct |
22 ms |
6476 KB |
Output is correct |
42 |
Correct |
13 ms |
3032 KB |
Output is correct |
43 |
Correct |
13 ms |
2896 KB |
Output is correct |
44 |
Correct |
15 ms |
2932 KB |
Output is correct |
45 |
Correct |
26 ms |
11524 KB |
Output is correct |
46 |
Correct |
25 ms |
11580 KB |
Output is correct |
47 |
Correct |
47 ms |
20400 KB |
Output is correct |
48 |
Correct |
22 ms |
7256 KB |
Output is correct |
49 |
Correct |
19 ms |
5576 KB |
Output is correct |
50 |
Correct |
15 ms |
5348 KB |
Output is correct |
51 |
Correct |
48 ms |
10440 KB |
Output is correct |
52 |
Correct |
47 ms |
11548 KB |
Output is correct |
53 |
Correct |
27 ms |
6216 KB |
Output is correct |
54 |
Correct |
3 ms |
2776 KB |
Output is correct |
55 |
Correct |
4 ms |
3792 KB |
Output is correct |
56 |
Correct |
20 ms |
4820 KB |
Output is correct |
57 |
Correct |
36 ms |
19160 KB |
Output is correct |
58 |
Correct |
13 ms |
4780 KB |
Output is correct |
59 |
Correct |
18 ms |
5556 KB |
Output is correct |
60 |
Correct |
19 ms |
6776 KB |
Output is correct |
61 |
Correct |
18 ms |
5308 KB |
Output is correct |
62 |
Correct |
32 ms |
13420 KB |
Output is correct |
63 |
Correct |
76 ms |
46172 KB |
Output is correct |
64 |
Correct |
84 ms |
52856 KB |
Output is correct |
65 |
Correct |
113 ms |
72628 KB |
Output is correct |
66 |
Correct |
206 ms |
129324 KB |
Output is correct |
67 |
Correct |
204 ms |
129304 KB |
Output is correct |