#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=(a);i<=(b);++i)
#define fd(i,a,b) for(int i=(a);i>=(b);--i)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second
#define LL unsigned long long
#define uint unsigned int
#define pb push_back
#define eb emplace_back
#define bit(s,i) ((s >> i) & 1)
#define off(s,i) (s & (~ (1 << i)))
#define ii pair <int , int>
#define iii1 pair <ii , int>
#define iii2 pair <int , ii>
#define TASK "SKY"
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const int oo = 0x3f;
int n , m , p[30004] , b[30004];
vector < int > vc[30004];
int d[30004][210];
struct node {
int dis , i , p;
};
bool operator > (node a , node b) {
return a.dis > b.dis;
}
///--------------------------
void readf() {
cin >> n >> m;
}
///--------------------------
void solve() {
int sqr = 200;
for (int i = 0 ; i < m ; ++i) {
cin >> b[i] >> p[i];
vc[b[i]].pb(i);
}
int ans = 1e9;
memset(d,0x3f,sizeof(d));
priority_queue < node , vector < node > , greater < node > > wyna;
wyna.push({0 , b[0] , 0});
d[b[0]][0] = 0;
while (!wyna.empty()) {
node x = wyna.top();
int dis = x.dis , pk = x.p , i = x.i;
if (i == b[1]) ans = min(ans , dis);
wyna.pop();
if (dis != d[i][pk]) continue;
if (pk == 0) {
for (auto x : vc[i])
if (p[x] <= sqr) {
if (d[i][p[x]] > dis) {
d[i][p[x]] = dis;
wyna.push({dis , i , p[x]});
}
} else {
int pp = p[x];
int cnt = 0;
for (int x = i ; x < n ; x += pp) {
if (d[x][0] > dis + cnt) {
d[x][0] = dis + cnt;
wyna.push({dis + cnt , x , 0});
}
cnt++;
}
cnt = 0;
for (int x = i ; x >= 0 ; x -= pp) {
if (d[x][0] > dis + cnt) {
d[x][0] = dis + cnt;
wyna.push({dis + cnt , x , 0});
}
cnt++;
}
}
} else {
if (i + pk < n && d[i + pk][pk] > dis + 1) {
d[i + pk][pk] = dis + 1;
wyna.push({dis + 1 , i + pk , pk});
}
if (i - pk >= 0 && d[i - pk][pk] > dis + 1) {
d[i - pk][pk] = dis + 1;
wyna.push({dis + 1 , i - pk , pk});
}
if (d[i][0] > dis) {
d[i][0] = dis;
wyna.push({dis , i , 0});
}
}
}
if (ans == 1e9) cout << -1;
else cout << ans;
}
///--------------------------
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
readf();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
25728 KB |
Output is correct |
2 |
Correct |
14 ms |
25728 KB |
Output is correct |
3 |
Correct |
15 ms |
25728 KB |
Output is correct |
4 |
Correct |
14 ms |
25728 KB |
Output is correct |
5 |
Correct |
15 ms |
25728 KB |
Output is correct |
6 |
Correct |
14 ms |
25728 KB |
Output is correct |
7 |
Correct |
14 ms |
25728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
25728 KB |
Output is correct |
2 |
Correct |
20 ms |
25728 KB |
Output is correct |
3 |
Correct |
14 ms |
25728 KB |
Output is correct |
4 |
Correct |
15 ms |
25728 KB |
Output is correct |
5 |
Correct |
20 ms |
25728 KB |
Output is correct |
6 |
Correct |
14 ms |
25728 KB |
Output is correct |
7 |
Correct |
14 ms |
25708 KB |
Output is correct |
8 |
Correct |
14 ms |
25728 KB |
Output is correct |
9 |
Correct |
16 ms |
25728 KB |
Output is correct |
10 |
Correct |
14 ms |
25728 KB |
Output is correct |
11 |
Correct |
15 ms |
25728 KB |
Output is correct |
12 |
Correct |
15 ms |
25728 KB |
Output is correct |
13 |
Correct |
14 ms |
25728 KB |
Output is correct |
14 |
Correct |
15 ms |
25728 KB |
Output is correct |
15 |
Correct |
15 ms |
25728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
25728 KB |
Output is correct |
2 |
Correct |
16 ms |
25728 KB |
Output is correct |
3 |
Correct |
15 ms |
25728 KB |
Output is correct |
4 |
Correct |
16 ms |
25728 KB |
Output is correct |
5 |
Correct |
16 ms |
25728 KB |
Output is correct |
6 |
Correct |
15 ms |
25728 KB |
Output is correct |
7 |
Correct |
17 ms |
25684 KB |
Output is correct |
8 |
Correct |
18 ms |
25728 KB |
Output is correct |
9 |
Correct |
13 ms |
25728 KB |
Output is correct |
10 |
Correct |
14 ms |
25728 KB |
Output is correct |
11 |
Correct |
15 ms |
25728 KB |
Output is correct |
12 |
Correct |
16 ms |
25728 KB |
Output is correct |
13 |
Correct |
14 ms |
25728 KB |
Output is correct |
14 |
Correct |
14 ms |
25728 KB |
Output is correct |
15 |
Correct |
15 ms |
25728 KB |
Output is correct |
16 |
Correct |
14 ms |
25728 KB |
Output is correct |
17 |
Correct |
16 ms |
25728 KB |
Output is correct |
18 |
Correct |
14 ms |
25728 KB |
Output is correct |
19 |
Correct |
14 ms |
25728 KB |
Output is correct |
20 |
Correct |
15 ms |
25728 KB |
Output is correct |
21 |
Correct |
14 ms |
25728 KB |
Output is correct |
22 |
Correct |
14 ms |
25728 KB |
Output is correct |
23 |
Correct |
15 ms |
25728 KB |
Output is correct |
24 |
Correct |
16 ms |
25728 KB |
Output is correct |
25 |
Correct |
15 ms |
25728 KB |
Output is correct |
26 |
Correct |
20 ms |
25728 KB |
Output is correct |
27 |
Correct |
14 ms |
25728 KB |
Output is correct |
28 |
Correct |
16 ms |
25728 KB |
Output is correct |
29 |
Correct |
25 ms |
25728 KB |
Output is correct |
30 |
Correct |
15 ms |
25728 KB |
Output is correct |
31 |
Correct |
18 ms |
25800 KB |
Output is correct |
32 |
Correct |
16 ms |
25728 KB |
Output is correct |
33 |
Correct |
26 ms |
25728 KB |
Output is correct |
34 |
Correct |
22 ms |
25728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
25728 KB |
Output is correct |
2 |
Correct |
19 ms |
25728 KB |
Output is correct |
3 |
Correct |
15 ms |
25728 KB |
Output is correct |
4 |
Correct |
14 ms |
25728 KB |
Output is correct |
5 |
Correct |
14 ms |
25728 KB |
Output is correct |
6 |
Correct |
14 ms |
25728 KB |
Output is correct |
7 |
Correct |
14 ms |
25728 KB |
Output is correct |
8 |
Correct |
13 ms |
25728 KB |
Output is correct |
9 |
Correct |
13 ms |
25728 KB |
Output is correct |
10 |
Correct |
18 ms |
25720 KB |
Output is correct |
11 |
Correct |
16 ms |
25728 KB |
Output is correct |
12 |
Correct |
15 ms |
25728 KB |
Output is correct |
13 |
Correct |
15 ms |
25728 KB |
Output is correct |
14 |
Correct |
17 ms |
25728 KB |
Output is correct |
15 |
Correct |
20 ms |
25728 KB |
Output is correct |
16 |
Correct |
15 ms |
25720 KB |
Output is correct |
17 |
Correct |
20 ms |
25728 KB |
Output is correct |
18 |
Correct |
14 ms |
25728 KB |
Output is correct |
19 |
Correct |
14 ms |
25728 KB |
Output is correct |
20 |
Correct |
15 ms |
25728 KB |
Output is correct |
21 |
Correct |
14 ms |
25728 KB |
Output is correct |
22 |
Correct |
15 ms |
25728 KB |
Output is correct |
23 |
Correct |
17 ms |
25728 KB |
Output is correct |
24 |
Correct |
21 ms |
25728 KB |
Output is correct |
25 |
Correct |
15 ms |
25728 KB |
Output is correct |
26 |
Correct |
14 ms |
25728 KB |
Output is correct |
27 |
Correct |
14 ms |
25728 KB |
Output is correct |
28 |
Correct |
15 ms |
25728 KB |
Output is correct |
29 |
Correct |
18 ms |
25728 KB |
Output is correct |
30 |
Correct |
16 ms |
25728 KB |
Output is correct |
31 |
Correct |
18 ms |
25728 KB |
Output is correct |
32 |
Correct |
18 ms |
25728 KB |
Output is correct |
33 |
Correct |
24 ms |
25728 KB |
Output is correct |
34 |
Correct |
24 ms |
25728 KB |
Output is correct |
35 |
Correct |
27 ms |
26240 KB |
Output is correct |
36 |
Correct |
18 ms |
25856 KB |
Output is correct |
37 |
Correct |
31 ms |
26240 KB |
Output is correct |
38 |
Correct |
32 ms |
26360 KB |
Output is correct |
39 |
Correct |
31 ms |
26368 KB |
Output is correct |
40 |
Correct |
34 ms |
26360 KB |
Output is correct |
41 |
Correct |
42 ms |
26360 KB |
Output is correct |
42 |
Correct |
21 ms |
26240 KB |
Output is correct |
43 |
Correct |
21 ms |
26116 KB |
Output is correct |
44 |
Correct |
19 ms |
26112 KB |
Output is correct |
45 |
Correct |
53 ms |
26692 KB |
Output is correct |
46 |
Correct |
55 ms |
26680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
25728 KB |
Output is correct |
2 |
Correct |
16 ms |
25728 KB |
Output is correct |
3 |
Correct |
15 ms |
25728 KB |
Output is correct |
4 |
Correct |
15 ms |
25728 KB |
Output is correct |
5 |
Correct |
15 ms |
25728 KB |
Output is correct |
6 |
Correct |
14 ms |
25728 KB |
Output is correct |
7 |
Correct |
15 ms |
25728 KB |
Output is correct |
8 |
Correct |
16 ms |
25728 KB |
Output is correct |
9 |
Correct |
15 ms |
25728 KB |
Output is correct |
10 |
Correct |
15 ms |
25728 KB |
Output is correct |
11 |
Correct |
18 ms |
25720 KB |
Output is correct |
12 |
Correct |
15 ms |
25728 KB |
Output is correct |
13 |
Correct |
14 ms |
25728 KB |
Output is correct |
14 |
Correct |
15 ms |
25728 KB |
Output is correct |
15 |
Correct |
14 ms |
25728 KB |
Output is correct |
16 |
Correct |
14 ms |
25728 KB |
Output is correct |
17 |
Correct |
16 ms |
25728 KB |
Output is correct |
18 |
Correct |
14 ms |
25728 KB |
Output is correct |
19 |
Correct |
14 ms |
25728 KB |
Output is correct |
20 |
Correct |
14 ms |
25772 KB |
Output is correct |
21 |
Correct |
16 ms |
25784 KB |
Output is correct |
22 |
Correct |
14 ms |
25728 KB |
Output is correct |
23 |
Correct |
15 ms |
25728 KB |
Output is correct |
24 |
Correct |
15 ms |
25728 KB |
Output is correct |
25 |
Correct |
15 ms |
25728 KB |
Output is correct |
26 |
Correct |
15 ms |
25728 KB |
Output is correct |
27 |
Correct |
15 ms |
25728 KB |
Output is correct |
28 |
Correct |
15 ms |
25728 KB |
Output is correct |
29 |
Correct |
17 ms |
25728 KB |
Output is correct |
30 |
Correct |
15 ms |
25728 KB |
Output is correct |
31 |
Correct |
17 ms |
25728 KB |
Output is correct |
32 |
Correct |
15 ms |
25728 KB |
Output is correct |
33 |
Correct |
22 ms |
25728 KB |
Output is correct |
34 |
Correct |
21 ms |
25728 KB |
Output is correct |
35 |
Correct |
25 ms |
26240 KB |
Output is correct |
36 |
Correct |
17 ms |
25856 KB |
Output is correct |
37 |
Correct |
38 ms |
26240 KB |
Output is correct |
38 |
Correct |
38 ms |
26360 KB |
Output is correct |
39 |
Correct |
31 ms |
26360 KB |
Output is correct |
40 |
Correct |
30 ms |
26368 KB |
Output is correct |
41 |
Correct |
29 ms |
26368 KB |
Output is correct |
42 |
Correct |
19 ms |
26240 KB |
Output is correct |
43 |
Correct |
18 ms |
26116 KB |
Output is correct |
44 |
Correct |
19 ms |
26204 KB |
Output is correct |
45 |
Correct |
52 ms |
26692 KB |
Output is correct |
46 |
Correct |
51 ms |
26688 KB |
Output is correct |
47 |
Correct |
82 ms |
26816 KB |
Output is correct |
48 |
Correct |
20 ms |
26368 KB |
Output is correct |
49 |
Correct |
20 ms |
26368 KB |
Output is correct |
50 |
Correct |
21 ms |
26232 KB |
Output is correct |
51 |
Correct |
54 ms |
27072 KB |
Output is correct |
52 |
Correct |
57 ms |
27072 KB |
Output is correct |
53 |
Correct |
33 ms |
26496 KB |
Output is correct |
54 |
Correct |
15 ms |
25728 KB |
Output is correct |
55 |
Correct |
15 ms |
25728 KB |
Output is correct |
56 |
Correct |
23 ms |
26880 KB |
Output is correct |
57 |
Correct |
66 ms |
25856 KB |
Output is correct |
58 |
Correct |
20 ms |
26116 KB |
Output is correct |
59 |
Correct |
26 ms |
26240 KB |
Output is correct |
60 |
Correct |
28 ms |
26240 KB |
Output is correct |
61 |
Correct |
24 ms |
26240 KB |
Output is correct |
62 |
Correct |
49 ms |
26872 KB |
Output is correct |
63 |
Correct |
60 ms |
27896 KB |
Output is correct |
64 |
Correct |
65 ms |
27768 KB |
Output is correct |
65 |
Correct |
309 ms |
27892 KB |
Output is correct |
66 |
Correct |
791 ms |
27768 KB |
Output is correct |
67 |
Correct |
775 ms |
27760 KB |
Output is correct |