#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 3e4+4;
int n, m, power[N], position[N];
vi building[N];
void sol() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, p; cin >> x >> p; x++;
position[i] = x;
power[i] = p;
building[x].pb(i);
}
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<>> q;
q.push({0, 1, position[1]}); // curr dist, curr dogde, curr building
vii dis(m+1, vi(n+1, inf));
dis[1][position[1]] = 0;
while (!q.empty()) {
array<int, 3> f = q.top(); q.pop();
int currDoge = f[1], currBuild = f[2], dist = f[0];
if (dist != dis[currDoge][currBuild]) continue;
if (currDoge == 2) {
cout << dist << endl;
return;
}
// go to a differeent doge
for (auto chDoge : building[currBuild]) {
if (dis[chDoge][currBuild] > dis[currDoge][currBuild]) {
dis[chDoge][currBuild] = dis[currDoge][currBuild];
q.push({dis[chDoge][currBuild], chDoge, currBuild});
}
}
// go to next building
if (power[currDoge] + currBuild <= n) {
int chBuild = power[currDoge] + currBuild;
if (dis[currDoge][chBuild] > dis[currDoge][currBuild] + 1) {
dis[currDoge][chBuild] = dis[currDoge][currBuild] + 1;
q.push({dis[currDoge][chBuild], currDoge, chBuild});
}
}
// go to previous building
if (currBuild - power[currDoge] >= 1) {
int chBuild = currBuild - power[currDoge];
if (dis[currDoge][chBuild] > dis[currDoge][currBuild] + 1) {
dis[currDoge][chBuild] = dis[currDoge][currBuild] + 1;
q.push({dis[currDoge][chBuild], currDoge, chBuild});
}
}
}
cout << -1 << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1112 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1172 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1168 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1268 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1952 KB |
Output is correct |
12 |
Correct |
2 ms |
1884 KB |
Output is correct |
13 |
Correct |
8 ms |
2140 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1036 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1176 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
2136 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
9 ms |
2180 KB |
Output is correct |
14 |
Correct |
2 ms |
1880 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
3 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
8028 KB |
Output is correct |
19 |
Correct |
3 ms |
5468 KB |
Output is correct |
20 |
Correct |
367 ms |
17036 KB |
Output is correct |
21 |
Correct |
2 ms |
2904 KB |
Output is correct |
22 |
Correct |
2 ms |
6236 KB |
Output is correct |
23 |
Correct |
3 ms |
7260 KB |
Output is correct |
24 |
Correct |
7 ms |
15196 KB |
Output is correct |
25 |
Correct |
8 ms |
16988 KB |
Output is correct |
26 |
Correct |
7 ms |
16732 KB |
Output is correct |
27 |
Correct |
8 ms |
17240 KB |
Output is correct |
28 |
Correct |
7 ms |
16984 KB |
Output is correct |
29 |
Correct |
9 ms |
7004 KB |
Output is correct |
30 |
Correct |
3 ms |
2396 KB |
Output is correct |
31 |
Correct |
5 ms |
5724 KB |
Output is correct |
32 |
Correct |
4 ms |
4956 KB |
Output is correct |
33 |
Correct |
21 ms |
17020 KB |
Output is correct |
34 |
Correct |
20 ms |
16984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1952 KB |
Output is correct |
13 |
Correct |
8 ms |
2140 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
3 ms |
6492 KB |
Output is correct |
18 |
Correct |
4 ms |
8028 KB |
Output is correct |
19 |
Correct |
3 ms |
5372 KB |
Output is correct |
20 |
Correct |
370 ms |
17292 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
6236 KB |
Output is correct |
23 |
Correct |
3 ms |
7304 KB |
Output is correct |
24 |
Correct |
8 ms |
15260 KB |
Output is correct |
25 |
Correct |
7 ms |
16988 KB |
Output is correct |
26 |
Correct |
7 ms |
16732 KB |
Output is correct |
27 |
Correct |
7 ms |
16984 KB |
Output is correct |
28 |
Correct |
8 ms |
16984 KB |
Output is correct |
29 |
Correct |
9 ms |
7000 KB |
Output is correct |
30 |
Correct |
3 ms |
2392 KB |
Output is correct |
31 |
Correct |
6 ms |
5724 KB |
Output is correct |
32 |
Correct |
4 ms |
4952 KB |
Output is correct |
33 |
Correct |
21 ms |
16984 KB |
Output is correct |
34 |
Correct |
18 ms |
17016 KB |
Output is correct |
35 |
Correct |
71 ms |
147960 KB |
Output is correct |
36 |
Correct |
9 ms |
21596 KB |
Output is correct |
37 |
Correct |
64 ms |
150068 KB |
Output is correct |
38 |
Correct |
115 ms |
237972 KB |
Output is correct |
39 |
Correct |
86 ms |
237880 KB |
Output is correct |
40 |
Correct |
94 ms |
237724 KB |
Output is correct |
41 |
Correct |
105 ms |
238280 KB |
Output is correct |
42 |
Correct |
86 ms |
237548 KB |
Output is correct |
43 |
Correct |
91 ms |
238164 KB |
Output is correct |
44 |
Execution timed out |
1055 ms |
238720 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1180 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1176 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1176 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1952 KB |
Output is correct |
13 |
Correct |
7 ms |
2004 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
3 ms |
6332 KB |
Output is correct |
18 |
Correct |
4 ms |
8024 KB |
Output is correct |
19 |
Correct |
2 ms |
5468 KB |
Output is correct |
20 |
Correct |
402 ms |
17296 KB |
Output is correct |
21 |
Correct |
2 ms |
2904 KB |
Output is correct |
22 |
Correct |
3 ms |
6236 KB |
Output is correct |
23 |
Correct |
5 ms |
7072 KB |
Output is correct |
24 |
Correct |
8 ms |
15196 KB |
Output is correct |
25 |
Correct |
9 ms |
16896 KB |
Output is correct |
26 |
Correct |
7 ms |
16732 KB |
Output is correct |
27 |
Correct |
7 ms |
16980 KB |
Output is correct |
28 |
Correct |
8 ms |
16988 KB |
Output is correct |
29 |
Correct |
8 ms |
7000 KB |
Output is correct |
30 |
Correct |
3 ms |
2396 KB |
Output is correct |
31 |
Correct |
6 ms |
5724 KB |
Output is correct |
32 |
Correct |
4 ms |
4956 KB |
Output is correct |
33 |
Correct |
22 ms |
16988 KB |
Output is correct |
34 |
Correct |
15 ms |
16988 KB |
Output is correct |
35 |
Correct |
70 ms |
147904 KB |
Output is correct |
36 |
Correct |
9 ms |
21592 KB |
Output is correct |
37 |
Correct |
63 ms |
149944 KB |
Output is correct |
38 |
Correct |
93 ms |
237628 KB |
Output is correct |
39 |
Correct |
87 ms |
237788 KB |
Output is correct |
40 |
Correct |
93 ms |
237848 KB |
Output is correct |
41 |
Correct |
96 ms |
238264 KB |
Output is correct |
42 |
Correct |
91 ms |
237676 KB |
Output is correct |
43 |
Correct |
88 ms |
238152 KB |
Output is correct |
44 |
Execution timed out |
1067 ms |
238568 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |