/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author win11905
*/
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define iii tuple<int, int, int>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 3e4+5;
const int SZ = 175;
class skyscraper {
private:
int n, m, s, t;
set<int> g[N];
int pre[SZ][N];
int d[N];
bool check[N];
public:
void solve(istream& cin, ostream& cout) {
cin >> n >> m;
for(int i = 0, a, b; i < m; ++i) {
cin >> a >> b;
g[a].emplace(b);
if(i == 0) s = a;
if(i == 1) t = a;
}
fill_n(d, N, INF);
fill_n(pre[0], SZ*N, INF);
priority_queue<pii, vector<pii>, greater<pii>> Q;
Q.emplace(d[s] = 0, s);
while(!Q.empty()) {
int u = Q.top().y, x = Q.top().x; Q.pop();
if(check[u]) continue; check[u] = true;
if(u == t) {
cout << x << endl;
return;
}
for(int p : g[u]) {
if(p < SZ) pre[p][u] = x;
for(int i = 1, v = u-p; v >= 0; v -= p, ++i) {
if(p < SZ) {
if(pre[p][v] <= x+i) break;
pre[p][v] = x+i;
}
if(d[v] > x+i) Q.emplace(d[v] = x+i, v);
}
for(int i = 1, v = u+p; v < n; v += p, ++i) {
if(p < SZ) {
if(pre[p][v] <= x+i) break;
pre[p][v] = x+1;
}
if(d[v] > x+i) Q.emplace(d[v] = x+i, v);
}
}
}
cout << -1 << endl;
}
};
class Solver {
public:
void solve(std::istream& in, std::ostream& out) {
skyscraper *obj = new skyscraper();
obj->solve(in, out);
}
};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solver solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
Compilation message
skyscraper.cpp: In member function 'void skyscraper::solve(std::istream&, std::ostream&)':
skyscraper.cpp:46:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(check[u]) continue; check[u] = true;
^~
skyscraper.cpp:46:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(check[u]) continue; check[u] = true;
^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22400 KB |
Output is correct |
2 |
Correct |
29 ms |
22476 KB |
Output is correct |
3 |
Correct |
24 ms |
22400 KB |
Output is correct |
4 |
Correct |
26 ms |
22400 KB |
Output is correct |
5 |
Correct |
29 ms |
22392 KB |
Output is correct |
6 |
Correct |
28 ms |
22448 KB |
Output is correct |
7 |
Correct |
27 ms |
22528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22364 KB |
Output is correct |
2 |
Correct |
25 ms |
22400 KB |
Output is correct |
3 |
Correct |
25 ms |
22392 KB |
Output is correct |
4 |
Correct |
26 ms |
22400 KB |
Output is correct |
5 |
Correct |
30 ms |
22492 KB |
Output is correct |
6 |
Correct |
29 ms |
22400 KB |
Output is correct |
7 |
Correct |
29 ms |
22528 KB |
Output is correct |
8 |
Correct |
27 ms |
22432 KB |
Output is correct |
9 |
Correct |
29 ms |
22500 KB |
Output is correct |
10 |
Correct |
30 ms |
22528 KB |
Output is correct |
11 |
Correct |
26 ms |
22528 KB |
Output is correct |
12 |
Correct |
22 ms |
22492 KB |
Output is correct |
13 |
Correct |
32 ms |
22400 KB |
Output is correct |
14 |
Correct |
30 ms |
22520 KB |
Output is correct |
15 |
Correct |
30 ms |
22528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
22400 KB |
Output is correct |
2 |
Correct |
25 ms |
22392 KB |
Output is correct |
3 |
Correct |
28 ms |
22392 KB |
Output is correct |
4 |
Correct |
28 ms |
22400 KB |
Output is correct |
5 |
Correct |
28 ms |
22432 KB |
Output is correct |
6 |
Correct |
25 ms |
22528 KB |
Output is correct |
7 |
Correct |
28 ms |
22400 KB |
Output is correct |
8 |
Correct |
23 ms |
22400 KB |
Output is correct |
9 |
Correct |
26 ms |
22500 KB |
Output is correct |
10 |
Correct |
34 ms |
22528 KB |
Output is correct |
11 |
Correct |
26 ms |
22520 KB |
Output is correct |
12 |
Correct |
25 ms |
22400 KB |
Output is correct |
13 |
Correct |
27 ms |
22520 KB |
Output is correct |
14 |
Correct |
27 ms |
22528 KB |
Output is correct |
15 |
Correct |
29 ms |
22528 KB |
Output is correct |
16 |
Correct |
26 ms |
22528 KB |
Output is correct |
17 |
Correct |
25 ms |
22656 KB |
Output is correct |
18 |
Correct |
31 ms |
22528 KB |
Output is correct |
19 |
Correct |
27 ms |
22520 KB |
Output is correct |
20 |
Correct |
30 ms |
22648 KB |
Output is correct |
21 |
Correct |
27 ms |
22520 KB |
Output is correct |
22 |
Correct |
25 ms |
22524 KB |
Output is correct |
23 |
Correct |
26 ms |
22580 KB |
Output is correct |
24 |
Correct |
26 ms |
22648 KB |
Output is correct |
25 |
Correct |
26 ms |
22648 KB |
Output is correct |
26 |
Correct |
24 ms |
22528 KB |
Output is correct |
27 |
Correct |
26 ms |
22528 KB |
Output is correct |
28 |
Correct |
28 ms |
22656 KB |
Output is correct |
29 |
Correct |
27 ms |
22648 KB |
Output is correct |
30 |
Correct |
28 ms |
22608 KB |
Output is correct |
31 |
Correct |
28 ms |
22524 KB |
Output is correct |
32 |
Correct |
29 ms |
22528 KB |
Output is correct |
33 |
Correct |
35 ms |
22872 KB |
Output is correct |
34 |
Correct |
31 ms |
22896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22468 KB |
Output is correct |
2 |
Correct |
28 ms |
22400 KB |
Output is correct |
3 |
Correct |
25 ms |
22400 KB |
Output is correct |
4 |
Correct |
26 ms |
22520 KB |
Output is correct |
5 |
Correct |
26 ms |
22400 KB |
Output is correct |
6 |
Correct |
29 ms |
22528 KB |
Output is correct |
7 |
Correct |
28 ms |
22528 KB |
Output is correct |
8 |
Correct |
27 ms |
22400 KB |
Output is correct |
9 |
Correct |
24 ms |
22500 KB |
Output is correct |
10 |
Correct |
25 ms |
22528 KB |
Output is correct |
11 |
Correct |
31 ms |
22520 KB |
Output is correct |
12 |
Correct |
27 ms |
22520 KB |
Output is correct |
13 |
Correct |
29 ms |
22528 KB |
Output is correct |
14 |
Correct |
28 ms |
22528 KB |
Output is correct |
15 |
Correct |
28 ms |
22696 KB |
Output is correct |
16 |
Correct |
24 ms |
22528 KB |
Output is correct |
17 |
Correct |
30 ms |
22648 KB |
Output is correct |
18 |
Correct |
28 ms |
22528 KB |
Output is correct |
19 |
Correct |
26 ms |
22528 KB |
Output is correct |
20 |
Correct |
27 ms |
22628 KB |
Output is correct |
21 |
Correct |
29 ms |
22520 KB |
Output is correct |
22 |
Correct |
29 ms |
22528 KB |
Output is correct |
23 |
Correct |
25 ms |
22528 KB |
Output is correct |
24 |
Correct |
28 ms |
22648 KB |
Output is correct |
25 |
Correct |
26 ms |
22528 KB |
Output is correct |
26 |
Correct |
26 ms |
22528 KB |
Output is correct |
27 |
Correct |
29 ms |
22520 KB |
Output is correct |
28 |
Correct |
28 ms |
22520 KB |
Output is correct |
29 |
Correct |
31 ms |
22776 KB |
Output is correct |
30 |
Correct |
27 ms |
22520 KB |
Output is correct |
31 |
Correct |
26 ms |
22648 KB |
Output is correct |
32 |
Correct |
27 ms |
22648 KB |
Output is correct |
33 |
Correct |
35 ms |
22904 KB |
Output is correct |
34 |
Correct |
33 ms |
22908 KB |
Output is correct |
35 |
Correct |
41 ms |
23928 KB |
Output is correct |
36 |
Correct |
33 ms |
22776 KB |
Output is correct |
37 |
Correct |
35 ms |
23672 KB |
Output is correct |
38 |
Correct |
43 ms |
24312 KB |
Output is correct |
39 |
Correct |
37 ms |
24184 KB |
Output is correct |
40 |
Correct |
44 ms |
24184 KB |
Output is correct |
41 |
Correct |
51 ms |
24184 KB |
Output is correct |
42 |
Correct |
35 ms |
22656 KB |
Output is correct |
43 |
Correct |
36 ms |
22648 KB |
Output is correct |
44 |
Correct |
36 ms |
22776 KB |
Output is correct |
45 |
Correct |
46 ms |
24440 KB |
Output is correct |
46 |
Correct |
46 ms |
24444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
22532 KB |
Output is correct |
2 |
Correct |
25 ms |
22528 KB |
Output is correct |
3 |
Correct |
28 ms |
22528 KB |
Output is correct |
4 |
Correct |
29 ms |
22400 KB |
Output is correct |
5 |
Correct |
26 ms |
22528 KB |
Output is correct |
6 |
Correct |
26 ms |
22520 KB |
Output is correct |
7 |
Correct |
29 ms |
22392 KB |
Output is correct |
8 |
Correct |
28 ms |
22400 KB |
Output is correct |
9 |
Correct |
24 ms |
22400 KB |
Output is correct |
10 |
Correct |
25 ms |
22500 KB |
Output is correct |
11 |
Correct |
28 ms |
22528 KB |
Output is correct |
12 |
Correct |
25 ms |
22392 KB |
Output is correct |
13 |
Correct |
29 ms |
22500 KB |
Output is correct |
14 |
Correct |
27 ms |
22528 KB |
Output is correct |
15 |
Correct |
26 ms |
22528 KB |
Output is correct |
16 |
Correct |
24 ms |
22520 KB |
Output is correct |
17 |
Correct |
28 ms |
22628 KB |
Output is correct |
18 |
Correct |
30 ms |
22528 KB |
Output is correct |
19 |
Correct |
29 ms |
22520 KB |
Output is correct |
20 |
Correct |
27 ms |
22656 KB |
Output is correct |
21 |
Correct |
26 ms |
22528 KB |
Output is correct |
22 |
Correct |
24 ms |
22528 KB |
Output is correct |
23 |
Correct |
27 ms |
22484 KB |
Output is correct |
24 |
Correct |
29 ms |
22620 KB |
Output is correct |
25 |
Correct |
28 ms |
22520 KB |
Output is correct |
26 |
Correct |
31 ms |
22528 KB |
Output is correct |
27 |
Correct |
26 ms |
22520 KB |
Output is correct |
28 |
Correct |
30 ms |
22528 KB |
Output is correct |
29 |
Correct |
29 ms |
22656 KB |
Output is correct |
30 |
Correct |
26 ms |
22528 KB |
Output is correct |
31 |
Correct |
29 ms |
22656 KB |
Output is correct |
32 |
Correct |
29 ms |
22528 KB |
Output is correct |
33 |
Correct |
34 ms |
23000 KB |
Output is correct |
34 |
Correct |
31 ms |
22900 KB |
Output is correct |
35 |
Correct |
47 ms |
23928 KB |
Output is correct |
36 |
Correct |
31 ms |
22776 KB |
Output is correct |
37 |
Correct |
41 ms |
23544 KB |
Output is correct |
38 |
Correct |
48 ms |
24240 KB |
Output is correct |
39 |
Correct |
35 ms |
24192 KB |
Output is correct |
40 |
Correct |
44 ms |
24184 KB |
Output is correct |
41 |
Correct |
42 ms |
24188 KB |
Output is correct |
42 |
Correct |
33 ms |
22648 KB |
Output is correct |
43 |
Correct |
31 ms |
22648 KB |
Output is correct |
44 |
Correct |
39 ms |
22784 KB |
Output is correct |
45 |
Correct |
56 ms |
24440 KB |
Output is correct |
46 |
Correct |
51 ms |
24440 KB |
Output is correct |
47 |
Correct |
45 ms |
24436 KB |
Output is correct |
48 |
Correct |
34 ms |
23680 KB |
Output is correct |
49 |
Correct |
40 ms |
23676 KB |
Output is correct |
50 |
Correct |
34 ms |
23288 KB |
Output is correct |
51 |
Correct |
55 ms |
25348 KB |
Output is correct |
52 |
Correct |
66 ms |
24948 KB |
Output is correct |
53 |
Correct |
50 ms |
24452 KB |
Output is correct |
54 |
Correct |
30 ms |
22648 KB |
Output is correct |
55 |
Correct |
27 ms |
22744 KB |
Output is correct |
56 |
Correct |
49 ms |
24352 KB |
Output is correct |
57 |
Correct |
32 ms |
23160 KB |
Output is correct |
58 |
Correct |
41 ms |
22904 KB |
Output is correct |
59 |
Correct |
46 ms |
22904 KB |
Output is correct |
60 |
Correct |
36 ms |
22904 KB |
Output is correct |
61 |
Correct |
40 ms |
23036 KB |
Output is correct |
62 |
Correct |
55 ms |
24184 KB |
Output is correct |
63 |
Correct |
92 ms |
26348 KB |
Output is correct |
64 |
Correct |
87 ms |
26348 KB |
Output is correct |
65 |
Correct |
139 ms |
26348 KB |
Output is correct |
66 |
Correct |
220 ms |
28308 KB |
Output is correct |
67 |
Correct |
164 ms |
28392 KB |
Output is correct |