#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
const int mod = (int) 1e9 + 7;
const ll inf = 1LL << 60;
const int maxn = (int) 3e4 + 5;
const ld eps = 1e-9;
int n, m, b[maxn], p[maxn], f[maxn];
map<int, int> d[maxn];
vector<int> a[maxn];
priority_queue<pair<int, ii>, vector<pair<int, ii> >, greater<pair<int, ii> > > pq;
int main() {
//freopen("test.txt", "r", stdin);
cin >> n >> m;
rep(i, 0, m) {
cin >> b[i] >> p[i];
a[b[i]].pb(p[i]);
d[b[i]][p[i]] = mod;
}
rep(i, 0, n) {
sort(all(a[i]));
a[i].resize(unique(all(a[i])) - a[i].begin());
}
d[b[0]][p[0]] = 0;
pq.push(make_pair(0, ii(b[0], p[0])));
while(!pq.empty()) {
int du = pq.top().fi;
int u = pq.top().se.fi;
int t = pq.top().se.se;
pq.pop();
if(du != d[u][t]) continue;
rep(i, -1, 2) if(i) {
int v = u + i * t;
if(v < 0 || v >= n) continue;
if(d[v].count(t) && d[v][t] <= du + 1) continue;
d[v][t] = du + 1;
pq.push(make_pair(du + 1, ii(v, t)));
}
for (auto s : a[u]) {
if(d[u][s] > du) {
d[u][s] = du;
pq.push(make_pair(du, ii(u, s)));
}
}
}
//cout << d[4][1] << endl;
if(d[b[1]][p[1]] != mod) cout << d[b[1]][p[1]] << '\n';
else cout << "-1\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4488 KB |
Output is correct |
2 |
Correct |
0 ms |
4488 KB |
Output is correct |
3 |
Correct |
0 ms |
4488 KB |
Output is correct |
4 |
Correct |
0 ms |
4488 KB |
Output is correct |
5 |
Correct |
0 ms |
4488 KB |
Output is correct |
6 |
Correct |
0 ms |
4488 KB |
Output is correct |
7 |
Correct |
0 ms |
4488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4488 KB |
Output is correct |
2 |
Correct |
0 ms |
4488 KB |
Output is correct |
3 |
Correct |
0 ms |
4488 KB |
Output is correct |
4 |
Correct |
0 ms |
4488 KB |
Output is correct |
5 |
Correct |
0 ms |
4488 KB |
Output is correct |
6 |
Correct |
0 ms |
4488 KB |
Output is correct |
7 |
Correct |
0 ms |
4488 KB |
Output is correct |
8 |
Correct |
0 ms |
4488 KB |
Output is correct |
9 |
Correct |
0 ms |
4488 KB |
Output is correct |
10 |
Correct |
0 ms |
4620 KB |
Output is correct |
11 |
Correct |
6 ms |
4752 KB |
Output is correct |
12 |
Correct |
0 ms |
4488 KB |
Output is correct |
13 |
Correct |
0 ms |
4488 KB |
Output is correct |
14 |
Correct |
6 ms |
4888 KB |
Output is correct |
15 |
Correct |
6 ms |
4888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4488 KB |
Output is correct |
2 |
Correct |
0 ms |
4488 KB |
Output is correct |
3 |
Correct |
0 ms |
4488 KB |
Output is correct |
4 |
Correct |
0 ms |
4488 KB |
Output is correct |
5 |
Correct |
0 ms |
4488 KB |
Output is correct |
6 |
Correct |
0 ms |
4488 KB |
Output is correct |
7 |
Correct |
0 ms |
4488 KB |
Output is correct |
8 |
Correct |
0 ms |
4488 KB |
Output is correct |
9 |
Correct |
0 ms |
4488 KB |
Output is correct |
10 |
Correct |
0 ms |
4620 KB |
Output is correct |
11 |
Correct |
6 ms |
4752 KB |
Output is correct |
12 |
Correct |
0 ms |
4488 KB |
Output is correct |
13 |
Correct |
3 ms |
4488 KB |
Output is correct |
14 |
Correct |
6 ms |
4888 KB |
Output is correct |
15 |
Correct |
6 ms |
4888 KB |
Output is correct |
16 |
Correct |
0 ms |
4488 KB |
Output is correct |
17 |
Correct |
9 ms |
5280 KB |
Output is correct |
18 |
Correct |
0 ms |
4620 KB |
Output is correct |
19 |
Correct |
0 ms |
4488 KB |
Output is correct |
20 |
Correct |
3 ms |
4620 KB |
Output is correct |
21 |
Correct |
3 ms |
4620 KB |
Output is correct |
22 |
Correct |
0 ms |
4620 KB |
Output is correct |
23 |
Correct |
0 ms |
4884 KB |
Output is correct |
24 |
Correct |
6 ms |
5148 KB |
Output is correct |
25 |
Correct |
3 ms |
4884 KB |
Output is correct |
26 |
Correct |
3 ms |
4884 KB |
Output is correct |
27 |
Correct |
3 ms |
4884 KB |
Output is correct |
28 |
Correct |
9 ms |
5676 KB |
Output is correct |
29 |
Correct |
29 ms |
7392 KB |
Output is correct |
30 |
Correct |
3 ms |
5280 KB |
Output is correct |
31 |
Correct |
16 ms |
6072 KB |
Output is correct |
32 |
Correct |
9 ms |
5676 KB |
Output is correct |
33 |
Correct |
63 ms |
10168 KB |
Output is correct |
34 |
Correct |
63 ms |
10168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4488 KB |
Output is correct |
2 |
Correct |
0 ms |
4488 KB |
Output is correct |
3 |
Correct |
0 ms |
4488 KB |
Output is correct |
4 |
Correct |
0 ms |
4488 KB |
Output is correct |
5 |
Correct |
0 ms |
4488 KB |
Output is correct |
6 |
Correct |
0 ms |
4488 KB |
Output is correct |
7 |
Correct |
0 ms |
4488 KB |
Output is correct |
8 |
Correct |
0 ms |
4488 KB |
Output is correct |
9 |
Correct |
0 ms |
4488 KB |
Output is correct |
10 |
Correct |
0 ms |
4620 KB |
Output is correct |
11 |
Correct |
6 ms |
4752 KB |
Output is correct |
12 |
Correct |
0 ms |
4488 KB |
Output is correct |
13 |
Correct |
3 ms |
4488 KB |
Output is correct |
14 |
Correct |
6 ms |
4888 KB |
Output is correct |
15 |
Correct |
6 ms |
4888 KB |
Output is correct |
16 |
Correct |
0 ms |
4488 KB |
Output is correct |
17 |
Correct |
13 ms |
5280 KB |
Output is correct |
18 |
Correct |
0 ms |
4620 KB |
Output is correct |
19 |
Correct |
0 ms |
4488 KB |
Output is correct |
20 |
Correct |
3 ms |
4620 KB |
Output is correct |
21 |
Correct |
3 ms |
4620 KB |
Output is correct |
22 |
Correct |
0 ms |
4620 KB |
Output is correct |
23 |
Correct |
3 ms |
4884 KB |
Output is correct |
24 |
Correct |
6 ms |
5148 KB |
Output is correct |
25 |
Correct |
3 ms |
4884 KB |
Output is correct |
26 |
Correct |
3 ms |
4884 KB |
Output is correct |
27 |
Correct |
6 ms |
4884 KB |
Output is correct |
28 |
Correct |
9 ms |
5676 KB |
Output is correct |
29 |
Correct |
26 ms |
7392 KB |
Output is correct |
30 |
Correct |
6 ms |
5280 KB |
Output is correct |
31 |
Correct |
9 ms |
6072 KB |
Output is correct |
32 |
Correct |
3 ms |
5676 KB |
Output is correct |
33 |
Correct |
63 ms |
10168 KB |
Output is correct |
34 |
Correct |
66 ms |
10168 KB |
Output is correct |
35 |
Correct |
186 ms |
9708 KB |
Output is correct |
36 |
Correct |
16 ms |
5412 KB |
Output is correct |
37 |
Correct |
303 ms |
13120 KB |
Output is correct |
38 |
Correct |
326 ms |
12664 KB |
Output is correct |
39 |
Correct |
299 ms |
12664 KB |
Output is correct |
40 |
Correct |
289 ms |
12664 KB |
Output is correct |
41 |
Correct |
299 ms |
12664 KB |
Output is correct |
42 |
Correct |
9 ms |
4968 KB |
Output is correct |
43 |
Correct |
16 ms |
4976 KB |
Output is correct |
44 |
Correct |
16 ms |
4768 KB |
Output is correct |
45 |
Correct |
659 ms |
27592 KB |
Output is correct |
46 |
Correct |
623 ms |
27592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4488 KB |
Output is correct |
2 |
Correct |
0 ms |
4488 KB |
Output is correct |
3 |
Correct |
0 ms |
4488 KB |
Output is correct |
4 |
Correct |
0 ms |
4488 KB |
Output is correct |
5 |
Correct |
0 ms |
4488 KB |
Output is correct |
6 |
Correct |
0 ms |
4488 KB |
Output is correct |
7 |
Correct |
0 ms |
4488 KB |
Output is correct |
8 |
Correct |
0 ms |
4488 KB |
Output is correct |
9 |
Correct |
0 ms |
4488 KB |
Output is correct |
10 |
Correct |
0 ms |
4620 KB |
Output is correct |
11 |
Correct |
6 ms |
4752 KB |
Output is correct |
12 |
Correct |
0 ms |
4488 KB |
Output is correct |
13 |
Correct |
0 ms |
4488 KB |
Output is correct |
14 |
Correct |
6 ms |
4888 KB |
Output is correct |
15 |
Correct |
6 ms |
4888 KB |
Output is correct |
16 |
Correct |
0 ms |
4488 KB |
Output is correct |
17 |
Correct |
9 ms |
5280 KB |
Output is correct |
18 |
Correct |
3 ms |
4620 KB |
Output is correct |
19 |
Correct |
0 ms |
4488 KB |
Output is correct |
20 |
Correct |
3 ms |
4620 KB |
Output is correct |
21 |
Correct |
3 ms |
4620 KB |
Output is correct |
22 |
Correct |
0 ms |
4620 KB |
Output is correct |
23 |
Correct |
3 ms |
4884 KB |
Output is correct |
24 |
Correct |
9 ms |
5148 KB |
Output is correct |
25 |
Correct |
0 ms |
4884 KB |
Output is correct |
26 |
Correct |
3 ms |
4884 KB |
Output is correct |
27 |
Correct |
3 ms |
4884 KB |
Output is correct |
28 |
Correct |
6 ms |
5676 KB |
Output is correct |
29 |
Correct |
29 ms |
7392 KB |
Output is correct |
30 |
Correct |
6 ms |
5280 KB |
Output is correct |
31 |
Correct |
13 ms |
6072 KB |
Output is correct |
32 |
Correct |
3 ms |
5676 KB |
Output is correct |
33 |
Correct |
66 ms |
10168 KB |
Output is correct |
34 |
Correct |
56 ms |
10168 KB |
Output is correct |
35 |
Correct |
173 ms |
9708 KB |
Output is correct |
36 |
Correct |
9 ms |
5412 KB |
Output is correct |
37 |
Correct |
309 ms |
13120 KB |
Output is correct |
38 |
Correct |
216 ms |
12664 KB |
Output is correct |
39 |
Correct |
333 ms |
12664 KB |
Output is correct |
40 |
Correct |
363 ms |
12664 KB |
Output is correct |
41 |
Correct |
256 ms |
12664 KB |
Output is correct |
42 |
Correct |
19 ms |
4968 KB |
Output is correct |
43 |
Correct |
23 ms |
4976 KB |
Output is correct |
44 |
Correct |
19 ms |
4768 KB |
Output is correct |
45 |
Correct |
716 ms |
27592 KB |
Output is correct |
46 |
Correct |
563 ms |
27592 KB |
Output is correct |
47 |
Execution timed out |
1000 ms |
40076 KB |
Execution timed out |
48 |
Halted |
0 ms |
0 KB |
- |