This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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));
for (int i = 0; i < sz(x); ++i) {
seg[i].push_back(sz(p));
p.push_back({x[i], 0});
seg[i].push_back(sz(p));
p.push_back({x[i], h[i]});
}
for (int i = 0; i < sz(y); ++i) {
seg[l[i]].push_back(sz(p));
seg[sz(x) + i].push_back(sz(p));
p.push_back({x[l[i]], y[i]});
seg[r[i]].push_back(sz(p));
seg[sz(x) + i].push_back(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])) {
seg[j].push_back(sz(p));
seg[sz(x) + i].push_back(sz(p));
p.push_back({x[j], y[i]});
}
}
}
vector<vector<int>> adj(sz(p));
for (int i = 0; i < sz(seg); ++i) {
for (int j : seg[i]) {
for (int k : seg[i]) {
adj[j].push_back(k);
}
}
}
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];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |