#include <bits/stdc++.h>
#define ll long long
#define ar array
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#include "walk.h"
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const ll INF = 1e18;
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
int n = sz(x), m = sz(l);
vector<int> idx(n);
iota(all(idx), 0);
sort(all(idx), [&](int x, int y) { return h[x] > h[y]; });
vector<int> idx2(m);
iota(all(idx2), 0);
sort(all(idx2), [&](int X, int Y) { return y[X] > y[Y]; });
set<int> who;
vector<ar<int, 3>> edges;
for (int i = 0, on = 0; i < m; i++) {
int L = l[idx2[i]], R = r[idx2[i]], H = y[idx2[i]];
while (on < n && h[idx[on]] >= H) {
who.insert(idx[on++]);
}
int lst = -1;
auto it1 = who.lower_bound(L);
if (it1 == who.end() || *it1 > R) continue;
lst = *it1;
while (1) {
auto it = who.upper_bound(lst);
if (it == who.end() || *it > R) break;
edges.push_back({lst, *it, H});
lst = *it;
}
}
vector<vector<int>> nodes(n);
vector<map<int, vector<int>>> adj(n);
for (auto [l, r, H] : edges) {
nodes[l].emplace_back(H);
nodes[r].emplace_back(H);
adj[l][H].push_back(r);
adj[r][H].push_back(l);
}
nodes[s].emplace_back(0);
nodes[g].emplace_back(0);
for (int i = 0; i < n; i++) {
sort(all(nodes[i]));
nodes[i].erase(unique(all(nodes[i])), nodes[i].end());
}
vector<map<int, ll>> dist(n);
dist[s][0] = 0;
priority_queue<ar<ll, 3>, vector<ar<ll, 3>>, greater<ar<ll, 3>>> q;
q.push({0, s, 0});
auto rlx = [&](int v1, int x1, int v2, int x2) {
ll new_dist = dist[v1][x1] + abs(x[v1] - x[v2]) + abs(x1 - x2);
if (!dist[v2].count(x2) || dist[v2][x2] > new_dist) {
dist[v2][x2] = new_dist;
q.push({dist[v2][x2], v2, x2});
}
};
while (q.size()) {
auto [cost, v, i] = q.top(); q.pop();
if (cost != dist[v][i]) continue;
auto it = upper_bound(all(nodes[v]), i);
if (it != nodes[v].end() && *it <= h[v]) {
rlx(v, i, v, *it);
}
assert(it != nodes[v].begin());
it--;
if (it != nodes[v].begin() && i <= h[v]) {
rlx(v, i, v, *prev(it));
}
for (int x : adj[v][i]) {
rlx(v, i, x, i);
}
}
return dist[g].count(0) ? dist[g][0] : -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1425 ms |
141516 KB |
Output is correct |
4 |
Correct |
901 ms |
158956 KB |
Output is correct |
5 |
Correct |
436 ms |
99844 KB |
Output is correct |
6 |
Correct |
312 ms |
89624 KB |
Output is correct |
7 |
Correct |
415 ms |
99832 KB |
Output is correct |
8 |
Correct |
1957 ms |
179544 KB |
Output is correct |
9 |
Correct |
574 ms |
102908 KB |
Output is correct |
10 |
Correct |
1255 ms |
216564 KB |
Output is correct |
11 |
Correct |
433 ms |
77004 KB |
Output is correct |
12 |
Correct |
265 ms |
60160 KB |
Output is correct |
13 |
Correct |
997 ms |
192588 KB |
Output is correct |
14 |
Correct |
152 ms |
46236 KB |
Output is correct |
15 |
Correct |
201 ms |
60184 KB |
Output is correct |
16 |
Correct |
197 ms |
60364 KB |
Output is correct |
17 |
Correct |
250 ms |
58612 KB |
Output is correct |
18 |
Correct |
357 ms |
55424 KB |
Output is correct |
19 |
Correct |
6 ms |
3164 KB |
Output is correct |
20 |
Correct |
67 ms |
28948 KB |
Output is correct |
21 |
Correct |
174 ms |
54804 KB |
Output is correct |
22 |
Correct |
213 ms |
60136 KB |
Output is correct |
23 |
Correct |
274 ms |
70660 KB |
Output is correct |
24 |
Correct |
218 ms |
60108 KB |
Output is correct |
25 |
Correct |
200 ms |
57584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
15560 KB |
Output is correct |
2 |
Execution timed out |
4067 ms |
685404 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
15560 KB |
Output is correct |
2 |
Execution timed out |
4067 ms |
685404 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1425 ms |
141516 KB |
Output is correct |
21 |
Correct |
901 ms |
158956 KB |
Output is correct |
22 |
Correct |
436 ms |
99844 KB |
Output is correct |
23 |
Correct |
312 ms |
89624 KB |
Output is correct |
24 |
Correct |
415 ms |
99832 KB |
Output is correct |
25 |
Correct |
1957 ms |
179544 KB |
Output is correct |
26 |
Correct |
574 ms |
102908 KB |
Output is correct |
27 |
Correct |
1255 ms |
216564 KB |
Output is correct |
28 |
Correct |
433 ms |
77004 KB |
Output is correct |
29 |
Correct |
265 ms |
60160 KB |
Output is correct |
30 |
Correct |
997 ms |
192588 KB |
Output is correct |
31 |
Correct |
152 ms |
46236 KB |
Output is correct |
32 |
Correct |
201 ms |
60184 KB |
Output is correct |
33 |
Correct |
197 ms |
60364 KB |
Output is correct |
34 |
Correct |
250 ms |
58612 KB |
Output is correct |
35 |
Correct |
357 ms |
55424 KB |
Output is correct |
36 |
Correct |
6 ms |
3164 KB |
Output is correct |
37 |
Correct |
67 ms |
28948 KB |
Output is correct |
38 |
Correct |
174 ms |
54804 KB |
Output is correct |
39 |
Correct |
213 ms |
60136 KB |
Output is correct |
40 |
Correct |
274 ms |
70660 KB |
Output is correct |
41 |
Correct |
218 ms |
60108 KB |
Output is correct |
42 |
Correct |
200 ms |
57584 KB |
Output is correct |
43 |
Correct |
53 ms |
15560 KB |
Output is correct |
44 |
Execution timed out |
4067 ms |
685404 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |