#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][104];
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 = 100;
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) { // truong hop nhay den nha i lon hon can
for (auto x : vc[i])
if (p[x] <= sqr) { // buoc nhay con dog dang cho <= sqrt
if (d[i][p[x]] > dis) {
d[i][p[x]] = dis;
wyna.push({dis , i , p[x]});
}
} else { // con dog dang dung lai tai nha i
int pp = p[x];
int cnt = 0;
for (int x = i ; x < n ; x += pp) { // cho con cho den i + x * p va dung lai
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) { // cho con cho den i + x * p va dung lai
if (d[x][0] > dis + cnt) {
d[x][0] = dis + cnt;
wyna.push({dis + cnt , x , 0});
}
cnt++;
}
}
} else { // truong hop nhay den nha i <= sqrt va di tiep
if (i + pk < n && d[i + pk][pk] > dis + 1) { // di tiep sang ben trai
d[i + pk][pk] = dis + 1;
wyna.push({dis + 1 , i + pk , pk});
}
if (i - pk >= 0 && d[i - pk][pk] > dis + 1) { // di tiep sang ben phai
d[i - pk][pk] = dis + 1;
wyna.push({dis + 1 , i - pk , pk});
}
if (d[i][0] > dis) { // con dog hien tai dung lai
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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13228 KB |
Output is correct |
6 |
Correct |
3 ms |
13156 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13144 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
3 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13144 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13144 KB |
Output is correct |
11 |
Correct |
3 ms |
13404 KB |
Output is correct |
12 |
Correct |
2 ms |
13404 KB |
Output is correct |
13 |
Correct |
2 ms |
13224 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
3 ms |
13400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
3 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
3 ms |
13236 KB |
Output is correct |
12 |
Correct |
2 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13404 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
4 ms |
13404 KB |
Output is correct |
16 |
Correct |
3 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13404 KB |
Output is correct |
18 |
Correct |
2 ms |
13656 KB |
Output is correct |
19 |
Correct |
2 ms |
13148 KB |
Output is correct |
20 |
Correct |
2 ms |
13404 KB |
Output is correct |
21 |
Correct |
2 ms |
13400 KB |
Output is correct |
22 |
Correct |
2 ms |
13404 KB |
Output is correct |
23 |
Correct |
3 ms |
13188 KB |
Output is correct |
24 |
Correct |
3 ms |
13404 KB |
Output is correct |
25 |
Correct |
3 ms |
13404 KB |
Output is correct |
26 |
Correct |
3 ms |
13404 KB |
Output is correct |
27 |
Correct |
3 ms |
13404 KB |
Output is correct |
28 |
Correct |
3 ms |
13404 KB |
Output is correct |
29 |
Correct |
5 ms |
13404 KB |
Output is correct |
30 |
Correct |
3 ms |
13148 KB |
Output is correct |
31 |
Correct |
4 ms |
13400 KB |
Output is correct |
32 |
Correct |
3 ms |
13404 KB |
Output is correct |
33 |
Correct |
10 ms |
13460 KB |
Output is correct |
34 |
Correct |
8 ms |
13404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13144 KB |
Output is correct |
11 |
Correct |
3 ms |
13404 KB |
Output is correct |
12 |
Correct |
3 ms |
13404 KB |
Output is correct |
13 |
Correct |
2 ms |
13400 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
2 ms |
13404 KB |
Output is correct |
16 |
Correct |
2 ms |
13404 KB |
Output is correct |
17 |
Correct |
3 ms |
13404 KB |
Output is correct |
18 |
Correct |
2 ms |
13404 KB |
Output is correct |
19 |
Correct |
2 ms |
13404 KB |
Output is correct |
20 |
Correct |
3 ms |
13404 KB |
Output is correct |
21 |
Correct |
3 ms |
13400 KB |
Output is correct |
22 |
Correct |
2 ms |
13404 KB |
Output is correct |
23 |
Correct |
2 ms |
13404 KB |
Output is correct |
24 |
Correct |
3 ms |
13400 KB |
Output is correct |
25 |
Correct |
3 ms |
13656 KB |
Output is correct |
26 |
Correct |
3 ms |
13404 KB |
Output is correct |
27 |
Correct |
2 ms |
13404 KB |
Output is correct |
28 |
Correct |
3 ms |
13404 KB |
Output is correct |
29 |
Correct |
5 ms |
13404 KB |
Output is correct |
30 |
Correct |
3 ms |
13144 KB |
Output is correct |
31 |
Correct |
4 ms |
13404 KB |
Output is correct |
32 |
Correct |
4 ms |
13404 KB |
Output is correct |
33 |
Correct |
9 ms |
13400 KB |
Output is correct |
34 |
Correct |
9 ms |
13404 KB |
Output is correct |
35 |
Correct |
9 ms |
13916 KB |
Output is correct |
36 |
Correct |
4 ms |
13404 KB |
Output is correct |
37 |
Correct |
11 ms |
14044 KB |
Output is correct |
38 |
Correct |
12 ms |
14196 KB |
Output is correct |
39 |
Correct |
12 ms |
14172 KB |
Output is correct |
40 |
Correct |
12 ms |
14256 KB |
Output is correct |
41 |
Correct |
12 ms |
14168 KB |
Output is correct |
42 |
Correct |
5 ms |
13912 KB |
Output is correct |
43 |
Correct |
5 ms |
13912 KB |
Output is correct |
44 |
Correct |
5 ms |
13864 KB |
Output is correct |
45 |
Correct |
18 ms |
14272 KB |
Output is correct |
46 |
Correct |
18 ms |
14168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
3 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13144 KB |
Output is correct |
4 |
Correct |
2 ms |
13144 KB |
Output is correct |
5 |
Correct |
3 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13144 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13392 KB |
Output is correct |
11 |
Correct |
3 ms |
13404 KB |
Output is correct |
12 |
Correct |
2 ms |
13404 KB |
Output is correct |
13 |
Correct |
3 ms |
13404 KB |
Output is correct |
14 |
Correct |
3 ms |
13404 KB |
Output is correct |
15 |
Correct |
3 ms |
13404 KB |
Output is correct |
16 |
Correct |
3 ms |
13148 KB |
Output is correct |
17 |
Correct |
3 ms |
13404 KB |
Output is correct |
18 |
Correct |
2 ms |
13404 KB |
Output is correct |
19 |
Correct |
3 ms |
13404 KB |
Output is correct |
20 |
Correct |
2 ms |
13404 KB |
Output is correct |
21 |
Correct |
2 ms |
13400 KB |
Output is correct |
22 |
Correct |
2 ms |
13404 KB |
Output is correct |
23 |
Correct |
2 ms |
13404 KB |
Output is correct |
24 |
Correct |
3 ms |
13404 KB |
Output is correct |
25 |
Correct |
2 ms |
13404 KB |
Output is correct |
26 |
Correct |
3 ms |
13404 KB |
Output is correct |
27 |
Correct |
3 ms |
13404 KB |
Output is correct |
28 |
Correct |
3 ms |
13404 KB |
Output is correct |
29 |
Correct |
5 ms |
13424 KB |
Output is correct |
30 |
Correct |
3 ms |
13148 KB |
Output is correct |
31 |
Correct |
4 ms |
13400 KB |
Output is correct |
32 |
Correct |
3 ms |
13404 KB |
Output is correct |
33 |
Correct |
8 ms |
13404 KB |
Output is correct |
34 |
Correct |
9 ms |
13404 KB |
Output is correct |
35 |
Correct |
9 ms |
13916 KB |
Output is correct |
36 |
Correct |
3 ms |
13404 KB |
Output is correct |
37 |
Correct |
11 ms |
13972 KB |
Output is correct |
38 |
Correct |
11 ms |
14172 KB |
Output is correct |
39 |
Correct |
12 ms |
14220 KB |
Output is correct |
40 |
Correct |
12 ms |
14172 KB |
Output is correct |
41 |
Correct |
11 ms |
14172 KB |
Output is correct |
42 |
Correct |
8 ms |
13916 KB |
Output is correct |
43 |
Correct |
5 ms |
13916 KB |
Output is correct |
44 |
Correct |
6 ms |
14212 KB |
Output is correct |
45 |
Correct |
18 ms |
14172 KB |
Output is correct |
46 |
Correct |
18 ms |
14172 KB |
Output is correct |
47 |
Correct |
33 ms |
14620 KB |
Output is correct |
48 |
Correct |
6 ms |
14172 KB |
Output is correct |
49 |
Correct |
8 ms |
14172 KB |
Output is correct |
50 |
Correct |
5 ms |
13916 KB |
Output is correct |
51 |
Correct |
28 ms |
14936 KB |
Output is correct |
52 |
Correct |
26 ms |
14936 KB |
Output is correct |
53 |
Correct |
10 ms |
14428 KB |
Output is correct |
54 |
Correct |
3 ms |
13500 KB |
Output is correct |
55 |
Correct |
3 ms |
13148 KB |
Output is correct |
56 |
Correct |
9 ms |
14780 KB |
Output is correct |
57 |
Correct |
33 ms |
13404 KB |
Output is correct |
58 |
Correct |
6 ms |
13880 KB |
Output is correct |
59 |
Correct |
8 ms |
13916 KB |
Output is correct |
60 |
Correct |
8 ms |
13916 KB |
Output is correct |
61 |
Correct |
8 ms |
14104 KB |
Output is correct |
62 |
Correct |
15 ms |
14680 KB |
Output is correct |
63 |
Correct |
19 ms |
15576 KB |
Output is correct |
64 |
Correct |
23 ms |
15576 KB |
Output is correct |
65 |
Correct |
29 ms |
15576 KB |
Output is correct |
66 |
Correct |
226 ms |
15572 KB |
Output is correct |
67 |
Correct |
228 ms |
15576 KB |
Output is correct |