#include "walk.h"
#include <bits/stdc++.h>
using namespace std;
#define ar array<int, 2>
#define sz(a) ((int)a.size())
int dist (ar &a, ar &b) {
return abs(a[0] - b[0]) + abs(a[1] - b[1]);
}
bool inter (ar &a, ar &b, ar &c, ar &d) {
return c[0] <= a[0] && a[0] <= d[0] && a[1] <= c[1] && c[1] <= b[1];
}
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 src, int snk) {
vector<ar> p;
vector<vector<int>> seg(sz(x) + sz(y));
vector<vector<int>> adj((int)1e6);
auto add = [&] (int i, int j) {
for (int k : seg[i]) {
adj[k].push_back(j);
adj[j].push_back(k);
}
seg[i].push_back(j);
};
for (int i = 0; i < sz(x); ++i) {
add(i, sz(p));
p.push_back({x[i], 0});
add(i, sz(p));
p.push_back({x[i], h[i]});
}
for (int i = 0; i < sz(y); ++i) {
add(l[i], sz(p));
add(sz(x) + i, sz(p));
p.push_back({x[l[i]], y[i]});
add(r[i], sz(p));
add(sz(x) + i, sz(p));
p.push_back({x[r[i]], y[i]});
int T = p.size();
for (int j = 0; j < sz(x); ++j) {
if (inter(p[2 * j], p[2 * j + 1], p[T - 2], p[T - 1])) {
add(j, sz(p));
add(sz(x) + i, sz(p));
p.push_back({x[j], y[i]});
}
}
}
priority_queue<pair<long long, int>> pq;
pq.push({0, 2 * src});
vector<long long> dis(adj.size(), 1e18);
dis[2 * src] = 0;
while (pq.size()) {
long long d = -pq.top().first;
int u = pq.top().second;
pq.pop();
if (d != dis[u]) {
continue;
}
for (int v : adj[u]) {
long long dd = dist(p[u], p[v]);
if (d + dd < dis[v]) {
dis[v] = d + dd;
pq.push({-dis[v], v});
}
}
}
return dis[2 * snk] == 1e18 ? -1 : dis[2 * snk];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
31564 KB |
Output is correct |
2 |
Correct |
20 ms |
31524 KB |
Output is correct |
3 |
Correct |
20 ms |
31620 KB |
Output is correct |
4 |
Correct |
20 ms |
31596 KB |
Output is correct |
5 |
Correct |
21 ms |
31780 KB |
Output is correct |
6 |
Correct |
20 ms |
31748 KB |
Output is correct |
7 |
Correct |
20 ms |
31804 KB |
Output is correct |
8 |
Correct |
21 ms |
31692 KB |
Output is correct |
9 |
Correct |
20 ms |
31548 KB |
Output is correct |
10 |
Correct |
21 ms |
31932 KB |
Output is correct |
11 |
Correct |
20 ms |
31588 KB |
Output is correct |
12 |
Correct |
20 ms |
31612 KB |
Output is correct |
13 |
Correct |
22 ms |
31528 KB |
Output is correct |
14 |
Correct |
20 ms |
31564 KB |
Output is correct |
15 |
Correct |
22 ms |
31584 KB |
Output is correct |
16 |
Correct |
20 ms |
31632 KB |
Output is correct |
17 |
Correct |
21 ms |
31812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
31528 KB |
Output is correct |
2 |
Correct |
20 ms |
31564 KB |
Output is correct |
3 |
Execution timed out |
4080 ms |
425744 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1235 ms |
72816 KB |
Output is correct |
2 |
Execution timed out |
4078 ms |
729116 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1235 ms |
72816 KB |
Output is correct |
2 |
Execution timed out |
4078 ms |
729116 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
31564 KB |
Output is correct |
2 |
Correct |
20 ms |
31524 KB |
Output is correct |
3 |
Correct |
20 ms |
31620 KB |
Output is correct |
4 |
Correct |
20 ms |
31596 KB |
Output is correct |
5 |
Correct |
21 ms |
31780 KB |
Output is correct |
6 |
Correct |
20 ms |
31748 KB |
Output is correct |
7 |
Correct |
20 ms |
31804 KB |
Output is correct |
8 |
Correct |
21 ms |
31692 KB |
Output is correct |
9 |
Correct |
20 ms |
31548 KB |
Output is correct |
10 |
Correct |
21 ms |
31932 KB |
Output is correct |
11 |
Correct |
20 ms |
31588 KB |
Output is correct |
12 |
Correct |
20 ms |
31612 KB |
Output is correct |
13 |
Correct |
22 ms |
31528 KB |
Output is correct |
14 |
Correct |
20 ms |
31564 KB |
Output is correct |
15 |
Correct |
22 ms |
31584 KB |
Output is correct |
16 |
Correct |
20 ms |
31632 KB |
Output is correct |
17 |
Correct |
21 ms |
31812 KB |
Output is correct |
18 |
Correct |
20 ms |
31528 KB |
Output is correct |
19 |
Correct |
20 ms |
31564 KB |
Output is correct |
20 |
Execution timed out |
4080 ms |
425744 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |