#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 5e5 + 5;
const int root = 200;
ll dp[30005], vis[30005], u[30005], v[30005];
bool ok[30005][205];
vector<ll> E[30005];
struct viet{
ll u, v, c;
bool operator <(const viet& o) const{
return c > o.c;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("VietCT.INP", "r")){
freopen("VietCT.INP", "r", stdin);
freopen("VietCT.OUT", "w", stdout);
}
ll n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++){
cin >> u[i] >> v[i];
u[i]++;
E[u[i]].push_back(i);
}
priority_queue<viet> q;
for (int i = 1; i <= n; i++){
dp[i] = 1e9;
}
for (auto j : E[u[1]]){
q.push({u[j], v[j], 0});
dp[u[j]] = 0;
ok[u[j]][v[j]] = 1;
}
vis[1] = 1;
while (q.size()){
viet x = q.top();
q.pop();
if (x.v < root){
if ((x.u - x.v) > 0){
if (vis[x.u - x.v] == 0){
vis[x.u - x.v] = 1;
dp[x.u - x.v] = min(dp[x.u - x.v], x.c + 1);
for (auto j : E[x.u - x.v]){
if (v[j] < root) ok[x.u - x.v][v[j]] = 1;
q.push({x.u - x.v, v[j], x.c + 1});
}
q.push({x.u - x.v, x.v, x.c + 1});
} else if (ok[x.u - x.v][x.v] == 0){
ok[x.u - x.v][x.v] = 1;
dp[x.u - x.v] = min(dp[x.u - x.v], x.c + 1);
q.push({x.u - x.v, x.v, x.c + 1});
}
}
if ((x.u + x.v) <= n){
if (vis[x.u + x.v] == 0){
vis[x.u + x.v] = 1;
dp[x.u + x.v] = min(dp[x.u + x.v], x.c + 1);
for (auto j : E[x.u + x.v]){
if (v[j] < root) ok[x.u + x.v][v[j]] = 1;
q.push({x.u + x.v, v[j], x.c + 1});
}
q.push({x.u + x.v, x.v, x.c + 1});
} else if (ok[x.u + x.v][x.v] == 0){
ok[x.u + x.v][x.v] = 1;
dp[x.u + x.v] = min(dp[x.u + x.v], x.c + 1);
q.push({x.u + x.v, x.v, x.c + 1});
}
}
} else {
for (int i = 1; i <= root; i++){
if ((x.u + x.v * i) > n) break;
if (vis[x.u + x.v * i] == 0){
vis[x.u + x.v * i] = 1;
dp[x.u + x.v * i] = min(dp[x.u + x.v * i], x.c + i);
for (auto j : E[x.u + x.v * i]){
if (v[j] < root) ok[x.u + x.v * i][v[j]] = 1;
q.push({x.u + x.v * i, v[j], x.c + i});
}
} else if (dp[x.u + x.v * i] > (x.c + i)){
dp[x.u + x.v * i] = min(dp[x.u + x.v * i], x.c + i);
for (auto j : E[x.u + x.v * i]){
if (v[j] < root) ok[x.u + x.v * i][v[j]] = 1;
q.push({x.u + x.v * i, v[j], x.c + i});
}
}
}
for (int i = 1; i <= root; i++){
if ((x.u - x.v * i) <= 0) break;
if (vis[x.u - x.v * i] == 0){
vis[x.u - x.v * i] = 1;
dp[x.u - x.v * i] = min(dp[x.u - x.v * i], x.c + i);
for (auto j : E[x.u - x.v * i]){
if (v[j] < root) ok[x.u - x.v * i][v[j]] = 1;
q.push({x.u - x.v * i, v[j], x.c + i});
}
} else if (dp[x.u - x.v * i] > (x.c + i)){
dp[x.u - x.v * i] = min(dp[x.u - x.v * i], x.c + i);
for (auto j : E[x.u - x.v * i]){
if (v[j] < root) ok[x.u - x.v * i][v[j]] = 1;
q.push({x.u - x.v * i, v[j], x.c + i});
}
}
}
}
}
ll ans = dp[u[2]];
if (ans == 1e9) cout << -1;
else cout << ans;
}
/*
*/
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:22:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
22 | freopen("VietCT.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:23:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
23 | freopen("VietCT.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1152 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
2 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1076 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
2 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1152 KB |
Output is correct |
17 |
Correct |
3 ms |
1408 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1152 KB |
Output is correct |
20 |
Correct |
2 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1152 KB |
Output is correct |
23 |
Correct |
3 ms |
1408 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
2 ms |
1536 KB |
Output is correct |
26 |
Correct |
2 ms |
1664 KB |
Output is correct |
27 |
Correct |
2 ms |
1664 KB |
Output is correct |
28 |
Correct |
2 ms |
1664 KB |
Output is correct |
29 |
Correct |
7 ms |
1536 KB |
Output is correct |
30 |
Correct |
3 ms |
1536 KB |
Output is correct |
31 |
Correct |
4 ms |
1536 KB |
Output is correct |
32 |
Correct |
3 ms |
1536 KB |
Output is correct |
33 |
Correct |
13 ms |
1664 KB |
Output is correct |
34 |
Correct |
12 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
2 ms |
1152 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
2 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1152 KB |
Output is correct |
17 |
Correct |
3 ms |
1408 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1152 KB |
Output is correct |
20 |
Correct |
2 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1152 KB |
Output is correct |
23 |
Correct |
2 ms |
1536 KB |
Output is correct |
24 |
Correct |
3 ms |
1536 KB |
Output is correct |
25 |
Correct |
2 ms |
1536 KB |
Output is correct |
26 |
Correct |
2 ms |
1664 KB |
Output is correct |
27 |
Correct |
2 ms |
1664 KB |
Output is correct |
28 |
Correct |
3 ms |
1664 KB |
Output is correct |
29 |
Correct |
7 ms |
1536 KB |
Output is correct |
30 |
Correct |
3 ms |
1536 KB |
Output is correct |
31 |
Correct |
5 ms |
1536 KB |
Output is correct |
32 |
Correct |
3 ms |
1536 KB |
Output is correct |
33 |
Correct |
13 ms |
1664 KB |
Output is correct |
34 |
Correct |
13 ms |
1664 KB |
Output is correct |
35 |
Correct |
18 ms |
3324 KB |
Output is correct |
36 |
Correct |
4 ms |
1664 KB |
Output is correct |
37 |
Correct |
22 ms |
3196 KB |
Output is correct |
38 |
Correct |
26 ms |
4472 KB |
Output is correct |
39 |
Correct |
26 ms |
4344 KB |
Output is correct |
40 |
Correct |
26 ms |
4344 KB |
Output is correct |
41 |
Correct |
27 ms |
4344 KB |
Output is correct |
42 |
Correct |
13 ms |
3324 KB |
Output is correct |
43 |
Correct |
12 ms |
3324 KB |
Output is correct |
44 |
Correct |
10 ms |
3360 KB |
Output is correct |
45 |
Correct |
52 ms |
3324 KB |
Output is correct |
46 |
Correct |
51 ms |
3324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1152 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
2 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1152 KB |
Output is correct |
17 |
Correct |
3 ms |
1408 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1152 KB |
Output is correct |
20 |
Correct |
2 ms |
1664 KB |
Output is correct |
21 |
Correct |
2 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1152 KB |
Output is correct |
23 |
Correct |
2 ms |
1536 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
3 ms |
1536 KB |
Output is correct |
26 |
Correct |
2 ms |
1664 KB |
Output is correct |
27 |
Correct |
2 ms |
1664 KB |
Output is correct |
28 |
Correct |
3 ms |
1664 KB |
Output is correct |
29 |
Correct |
7 ms |
1664 KB |
Output is correct |
30 |
Correct |
3 ms |
1536 KB |
Output is correct |
31 |
Correct |
4 ms |
1536 KB |
Output is correct |
32 |
Correct |
4 ms |
1536 KB |
Output is correct |
33 |
Correct |
15 ms |
1664 KB |
Output is correct |
34 |
Correct |
15 ms |
1664 KB |
Output is correct |
35 |
Correct |
18 ms |
3324 KB |
Output is correct |
36 |
Correct |
4 ms |
1664 KB |
Output is correct |
37 |
Correct |
23 ms |
3196 KB |
Output is correct |
38 |
Correct |
26 ms |
4344 KB |
Output is correct |
39 |
Correct |
27 ms |
4344 KB |
Output is correct |
40 |
Correct |
28 ms |
4344 KB |
Output is correct |
41 |
Correct |
25 ms |
4344 KB |
Output is correct |
42 |
Correct |
13 ms |
3324 KB |
Output is correct |
43 |
Correct |
11 ms |
3324 KB |
Output is correct |
44 |
Correct |
15 ms |
3360 KB |
Output is correct |
45 |
Correct |
55 ms |
3324 KB |
Output is correct |
46 |
Correct |
75 ms |
3388 KB |
Output is correct |
47 |
Correct |
90 ms |
6392 KB |
Output is correct |
48 |
Correct |
9 ms |
2304 KB |
Output is correct |
49 |
Correct |
8 ms |
2304 KB |
Output is correct |
50 |
Correct |
7 ms |
2048 KB |
Output is correct |
51 |
Correct |
49 ms |
9584 KB |
Output is correct |
52 |
Correct |
67 ms |
9496 KB |
Output is correct |
53 |
Correct |
27 ms |
8960 KB |
Output is correct |
54 |
Correct |
8 ms |
7552 KB |
Output is correct |
55 |
Correct |
8 ms |
7552 KB |
Output is correct |
56 |
Correct |
17 ms |
9212 KB |
Output is correct |
57 |
Correct |
58 ms |
7672 KB |
Output is correct |
58 |
Correct |
20 ms |
9212 KB |
Output is correct |
59 |
Correct |
25 ms |
9340 KB |
Output is correct |
60 |
Correct |
26 ms |
9340 KB |
Output is correct |
61 |
Correct |
26 ms |
9340 KB |
Output is correct |
62 |
Correct |
41 ms |
9268 KB |
Output is correct |
63 |
Correct |
22 ms |
8888 KB |
Output is correct |
64 |
Correct |
23 ms |
8888 KB |
Output is correct |
65 |
Correct |
238 ms |
8952 KB |
Output is correct |
66 |
Correct |
869 ms |
9392 KB |
Output is correct |
67 |
Correct |
848 ms |
9436 KB |
Output is correct |