#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
const int maxn = 30010;
const int INF = INT_MAX/2;
typedef pair<int, pi> pp;
int n,doges;
vector <int> bufflist[maxn];
vector <int> smalldogs[maxn];
const int len = 174;
int dist[maxn][len+10];
int stp,endp;
int32_t main() {
FAST
cin >> n >> doges;
for (int i =0;i<doges;i++) {
int pos, jump; cin >> pos >> jump;
if (i == 0) stp = pos;
if (i == 1) endp = pos;
if (jump < len) {
smalldogs[pos].push_back(jump);
} else {
bufflist[pos].push_back(jump);
}
}
memset(dist, -1, sizeof dist);
deque <pp> pq;
pq.push_front(pp(0,pi(stp,0)));
dist[stp][0] = 0;
while (!pq.empty()) {
pp cur = pq.front(); pq.pop_front();
int d = cur.f, pos = cur.s.f, jump = cur.s.s;
//cout << pos << " " << jump << " " << d << "\n";
if (jump == 0) {
if (d != dist[pos][0]) continue;
for (auto i: bufflist[pos]) {
if (pos + i < n) pq.push_back(pp(d+1,pi(pos + i,i)));
if (pos - i >= 0) pq.push_back(pp(d+1,pi(pos - i,-i)));
}
for (auto i: smalldogs[pos]) {
if (dist[pos][i] == -1 or dist[pos][i] > d) {
dist[pos][i] = d;
pq.push_front(pp(d, pi(pos,i)));
}
}
} else if (jump > 0 and jump < len) {
if (d != dist[pos][jump]) continue;
if (dist[pos][0] == -1 or dist[pos][0] > d) {
dist[pos][0] = d;
pq.push_front(pp(d,pi(pos,0)));
}
int newp = pos + jump;
if (newp < n) {
if (dist[newp][jump] == -1 or dist[newp][jump] > d + 1) {
dist[newp][jump] = d + 1;
pq.push_back(pp(d+1, pi(newp,jump)));
}
}
newp = pos - jump;
if (newp >= 0) {
if (dist[newp][jump] == -1 or dist[newp][jump] > d + 1) {
dist[newp][jump] = d + 1;
pq.push_back(pp(d + 1, pi(newp,jump)));
}
}
} else { //On a buff dog train
//Get off
if (dist[pos][0] == -1 or dist[pos][0] > d) {
dist[pos][0] = d;
pq.push_front(pp(d,pi(pos,0)));
}
//Continue train
if (jump > 0 and pos + jump < n) pq.push_back(pp(d+1,pi(pos + jump,jump)));
else if (jump < 0 and pos + jump >= 0) pq.push_back(pp(d+1,pi(pos + jump,jump)));
}
}
cout << dist[endp][0];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
44908 KB |
Output is correct |
2 |
Correct |
25 ms |
44908 KB |
Output is correct |
3 |
Correct |
25 ms |
44908 KB |
Output is correct |
4 |
Correct |
25 ms |
44908 KB |
Output is correct |
5 |
Correct |
25 ms |
44908 KB |
Output is correct |
6 |
Correct |
26 ms |
44908 KB |
Output is correct |
7 |
Correct |
26 ms |
44908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
44908 KB |
Output is correct |
2 |
Correct |
25 ms |
44928 KB |
Output is correct |
3 |
Correct |
26 ms |
45036 KB |
Output is correct |
4 |
Correct |
25 ms |
44908 KB |
Output is correct |
5 |
Correct |
25 ms |
45036 KB |
Output is correct |
6 |
Correct |
25 ms |
44908 KB |
Output is correct |
7 |
Correct |
26 ms |
45036 KB |
Output is correct |
8 |
Correct |
25 ms |
44908 KB |
Output is correct |
9 |
Correct |
25 ms |
44908 KB |
Output is correct |
10 |
Correct |
25 ms |
45036 KB |
Output is correct |
11 |
Correct |
26 ms |
45036 KB |
Output is correct |
12 |
Correct |
27 ms |
45036 KB |
Output is correct |
13 |
Correct |
26 ms |
45036 KB |
Output is correct |
14 |
Correct |
26 ms |
45036 KB |
Output is correct |
15 |
Correct |
26 ms |
45036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
44908 KB |
Output is correct |
2 |
Correct |
26 ms |
44908 KB |
Output is correct |
3 |
Correct |
26 ms |
44908 KB |
Output is correct |
4 |
Correct |
25 ms |
44908 KB |
Output is correct |
5 |
Correct |
25 ms |
44908 KB |
Output is correct |
6 |
Correct |
26 ms |
44908 KB |
Output is correct |
7 |
Correct |
26 ms |
44908 KB |
Output is correct |
8 |
Correct |
26 ms |
44908 KB |
Output is correct |
9 |
Correct |
26 ms |
44908 KB |
Output is correct |
10 |
Correct |
25 ms |
45036 KB |
Output is correct |
11 |
Correct |
27 ms |
45036 KB |
Output is correct |
12 |
Correct |
26 ms |
45036 KB |
Output is correct |
13 |
Correct |
26 ms |
45036 KB |
Output is correct |
14 |
Correct |
26 ms |
45164 KB |
Output is correct |
15 |
Correct |
26 ms |
45036 KB |
Output is correct |
16 |
Correct |
26 ms |
45036 KB |
Output is correct |
17 |
Correct |
28 ms |
45036 KB |
Output is correct |
18 |
Correct |
26 ms |
45036 KB |
Output is correct |
19 |
Correct |
25 ms |
45036 KB |
Output is correct |
20 |
Correct |
27 ms |
45036 KB |
Output is correct |
21 |
Correct |
25 ms |
45036 KB |
Output is correct |
22 |
Correct |
26 ms |
45164 KB |
Output is correct |
23 |
Correct |
26 ms |
45036 KB |
Output is correct |
24 |
Correct |
26 ms |
45036 KB |
Output is correct |
25 |
Correct |
26 ms |
45036 KB |
Output is correct |
26 |
Correct |
26 ms |
45036 KB |
Output is correct |
27 |
Correct |
25 ms |
45036 KB |
Output is correct |
28 |
Correct |
28 ms |
45036 KB |
Output is correct |
29 |
Correct |
28 ms |
45036 KB |
Output is correct |
30 |
Correct |
26 ms |
45036 KB |
Output is correct |
31 |
Correct |
26 ms |
45164 KB |
Output is correct |
32 |
Correct |
27 ms |
45036 KB |
Output is correct |
33 |
Correct |
30 ms |
45036 KB |
Output is correct |
34 |
Correct |
30 ms |
45036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
44908 KB |
Output is correct |
2 |
Correct |
25 ms |
44908 KB |
Output is correct |
3 |
Correct |
25 ms |
44908 KB |
Output is correct |
4 |
Correct |
25 ms |
44908 KB |
Output is correct |
5 |
Correct |
25 ms |
44908 KB |
Output is correct |
6 |
Correct |
26 ms |
44908 KB |
Output is correct |
7 |
Correct |
25 ms |
44908 KB |
Output is correct |
8 |
Correct |
26 ms |
44908 KB |
Output is correct |
9 |
Correct |
25 ms |
44908 KB |
Output is correct |
10 |
Correct |
25 ms |
45036 KB |
Output is correct |
11 |
Correct |
26 ms |
45036 KB |
Output is correct |
12 |
Correct |
26 ms |
45036 KB |
Output is correct |
13 |
Correct |
29 ms |
45036 KB |
Output is correct |
14 |
Correct |
25 ms |
45036 KB |
Output is correct |
15 |
Correct |
26 ms |
45036 KB |
Output is correct |
16 |
Correct |
26 ms |
45036 KB |
Output is correct |
17 |
Correct |
26 ms |
45036 KB |
Output is correct |
18 |
Correct |
26 ms |
45036 KB |
Output is correct |
19 |
Correct |
25 ms |
45036 KB |
Output is correct |
20 |
Correct |
26 ms |
45036 KB |
Output is correct |
21 |
Correct |
25 ms |
45036 KB |
Output is correct |
22 |
Correct |
25 ms |
45036 KB |
Output is correct |
23 |
Correct |
26 ms |
45036 KB |
Output is correct |
24 |
Correct |
27 ms |
45036 KB |
Output is correct |
25 |
Correct |
26 ms |
45036 KB |
Output is correct |
26 |
Correct |
26 ms |
45036 KB |
Output is correct |
27 |
Correct |
26 ms |
45036 KB |
Output is correct |
28 |
Correct |
27 ms |
45164 KB |
Output is correct |
29 |
Correct |
28 ms |
45036 KB |
Output is correct |
30 |
Correct |
26 ms |
45036 KB |
Output is correct |
31 |
Correct |
26 ms |
45036 KB |
Output is correct |
32 |
Correct |
28 ms |
45036 KB |
Output is correct |
33 |
Correct |
29 ms |
45036 KB |
Output is correct |
34 |
Correct |
29 ms |
45036 KB |
Output is correct |
35 |
Correct |
39 ms |
45932 KB |
Output is correct |
36 |
Correct |
27 ms |
45164 KB |
Output is correct |
37 |
Correct |
37 ms |
46060 KB |
Output is correct |
38 |
Correct |
38 ms |
46316 KB |
Output is correct |
39 |
Correct |
38 ms |
46344 KB |
Output is correct |
40 |
Correct |
38 ms |
46316 KB |
Output is correct |
41 |
Correct |
37 ms |
46316 KB |
Output is correct |
42 |
Correct |
35 ms |
45548 KB |
Output is correct |
43 |
Correct |
31 ms |
45552 KB |
Output is correct |
44 |
Correct |
30 ms |
45588 KB |
Output is correct |
45 |
Correct |
51 ms |
46316 KB |
Output is correct |
46 |
Correct |
42 ms |
46316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
44908 KB |
Output is correct |
2 |
Correct |
26 ms |
44908 KB |
Output is correct |
3 |
Correct |
26 ms |
44908 KB |
Output is correct |
4 |
Correct |
25 ms |
44908 KB |
Output is correct |
5 |
Correct |
26 ms |
44908 KB |
Output is correct |
6 |
Correct |
26 ms |
44908 KB |
Output is correct |
7 |
Correct |
26 ms |
44928 KB |
Output is correct |
8 |
Correct |
25 ms |
44908 KB |
Output is correct |
9 |
Correct |
25 ms |
44908 KB |
Output is correct |
10 |
Correct |
25 ms |
45036 KB |
Output is correct |
11 |
Correct |
26 ms |
45036 KB |
Output is correct |
12 |
Correct |
26 ms |
45036 KB |
Output is correct |
13 |
Correct |
26 ms |
45036 KB |
Output is correct |
14 |
Correct |
26 ms |
45036 KB |
Output is correct |
15 |
Correct |
26 ms |
45036 KB |
Output is correct |
16 |
Correct |
26 ms |
45036 KB |
Output is correct |
17 |
Correct |
27 ms |
45036 KB |
Output is correct |
18 |
Correct |
26 ms |
45036 KB |
Output is correct |
19 |
Correct |
26 ms |
45036 KB |
Output is correct |
20 |
Correct |
26 ms |
45036 KB |
Output is correct |
21 |
Correct |
26 ms |
45036 KB |
Output is correct |
22 |
Correct |
26 ms |
45036 KB |
Output is correct |
23 |
Correct |
26 ms |
45036 KB |
Output is correct |
24 |
Correct |
28 ms |
45164 KB |
Output is correct |
25 |
Correct |
26 ms |
45164 KB |
Output is correct |
26 |
Correct |
27 ms |
45036 KB |
Output is correct |
27 |
Correct |
27 ms |
45036 KB |
Output is correct |
28 |
Correct |
33 ms |
45036 KB |
Output is correct |
29 |
Correct |
27 ms |
45036 KB |
Output is correct |
30 |
Correct |
26 ms |
45036 KB |
Output is correct |
31 |
Correct |
27 ms |
45036 KB |
Output is correct |
32 |
Correct |
26 ms |
45036 KB |
Output is correct |
33 |
Correct |
29 ms |
45036 KB |
Output is correct |
34 |
Correct |
29 ms |
45036 KB |
Output is correct |
35 |
Correct |
34 ms |
45932 KB |
Output is correct |
36 |
Correct |
27 ms |
45164 KB |
Output is correct |
37 |
Correct |
36 ms |
46060 KB |
Output is correct |
38 |
Correct |
38 ms |
46316 KB |
Output is correct |
39 |
Correct |
39 ms |
46316 KB |
Output is correct |
40 |
Correct |
38 ms |
46316 KB |
Output is correct |
41 |
Correct |
37 ms |
46316 KB |
Output is correct |
42 |
Correct |
30 ms |
45548 KB |
Output is correct |
43 |
Correct |
31 ms |
45552 KB |
Output is correct |
44 |
Correct |
33 ms |
45588 KB |
Output is correct |
45 |
Correct |
42 ms |
46316 KB |
Output is correct |
46 |
Correct |
42 ms |
46316 KB |
Output is correct |
47 |
Correct |
74 ms |
46572 KB |
Output is correct |
48 |
Correct |
32 ms |
45804 KB |
Output is correct |
49 |
Correct |
32 ms |
45676 KB |
Output is correct |
50 |
Correct |
30 ms |
45548 KB |
Output is correct |
51 |
Correct |
55 ms |
46060 KB |
Output is correct |
52 |
Correct |
58 ms |
46188 KB |
Output is correct |
53 |
Correct |
39 ms |
45932 KB |
Output is correct |
54 |
Correct |
26 ms |
44908 KB |
Output is correct |
55 |
Correct |
26 ms |
44908 KB |
Output is correct |
56 |
Correct |
35 ms |
46188 KB |
Output is correct |
57 |
Correct |
57 ms |
45036 KB |
Output is correct |
58 |
Correct |
31 ms |
45424 KB |
Output is correct |
59 |
Correct |
37 ms |
45588 KB |
Output is correct |
60 |
Correct |
39 ms |
45552 KB |
Output is correct |
61 |
Correct |
36 ms |
45544 KB |
Output is correct |
62 |
Correct |
77 ms |
46316 KB |
Output is correct |
63 |
Correct |
110 ms |
46060 KB |
Output is correct |
64 |
Correct |
123 ms |
46060 KB |
Output is correct |
65 |
Correct |
226 ms |
46316 KB |
Output is correct |
66 |
Correct |
443 ms |
46444 KB |
Output is correct |
67 |
Correct |
490 ms |
46444 KB |
Output is correct |