| #include <algorithm> |
| #include <cassert> |
| #include <cmath> |
| #include <functional> |
| #include <iostream> |
| #include <queue> |
| #include <utility> |
| #include <vector> |
| using namespace std; |
|
|
| const int LIM = 6000006; |
| const long double EPS = 1e-9; |
|
|
| using LD = long double; |
| using pii = pair<int, int>; |
|
|
| |
| struct point { |
| LD x, y; |
|
|
| point(): x(0), y(0) {} |
| point(LD _x, LD _y): x(_x), y(_y) {} |
|
|
| LD operator/(point p) const { return x * p.y - y * p.x; } |
| point operator+(point p) const { return point(x + p.x, y + p.y); } |
| point operator-(point p) const { return point(x - p.x, y - p.y); } |
| friend point operator*(LD b, point a) { return point(b * a.x, b * a.y); } |
|
|
| LD size() const { return hypot(x, y); } |
| LD sz2() const { return x * x + y * y; } |
| point r90() const { return point(-y, x); } |
| LD dot(point p) { return x * p.x + y * p.y; } |
| }; |
|
|
| int dcmp(LD x) { |
| return x < -EPS ? -1 : x > EPS ? 1 : 0; |
| } |
|
|
| point line_intersect(point a, point b, point u, point v) { |
| return u + (((a - u) / b) / (v / b)) * v; |
| } |
|
|
| point get_circumcenter(point p0, point p1, point p2) { |
| return line_intersect( |
| 0.5 * (p0 + p1), (p0 - p1).r90(), 0.5 * (p1 + p2), (p1 - p2).r90() |
| ); |
| } |
|
|
| point project_point_segment(point a, point b, point c) { |
| LD r = (b - a).dot(b - a); |
| if (fabs(r) < EPS) { |
| return a; |
| } |
| r = (c - a).dot(b - a) / r; |
| if (r < 0) { |
| return a; |
| } |
| if (r > 1) { |
| return b; |
| } |
| return a + r * (b - a); |
| } |
|
|
| bool point_in_polygon_incl(const vector<point> &p, point q) { |
| bool c = 0; |
| for (int i = 0; i < p.size(); i++) { |
| int j = (i + 1) % p.size(); |
| if ((project_point_segment(p[i], p[j], q) - q).sz2() < EPS) { |
| return true; |
| } |
| if ( |
| (p[i].y <= q.y && q.y < p[j].y || p[j].y <= q.y && q.y < p[i].y) && |
| q.x < p[i].x + (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) |
| ) { |
| c = !c; |
| } |
| } |
| return c; |
| } |
|
|
| |
| LD parabola_intersect(point left, point right, LD sweepline) { |
| auto f2 = [](point left, point right, LD sweepline) { |
| int sign = left.x < right.x ? 1 : -1; |
| point m = 0.5 * (left + right); |
| point v = line_intersect( |
| m, (right - left).r90(), point(0, sweepline), point(1, 0) |
| ); |
| point w = line_intersect(m, (left - v).r90(), v, left - v); |
| LD l1 = (v - w).size(); |
| LD l2 = sqrt(pow(sweepline - m.y, 2) - (m - w).sz2()); |
| LD l3 = (left - v).size(); |
| return v.x + (m.x - v.x) * l3 / (l1 + sign * l2); |
| }; |
| if (fabs(left.y - right.y) < fabs(left.x - right.x) * EPS) { |
| return f2(left, right, sweepline); |
| } |
| int sign = left.y < right.y ? -1 : 1; |
| point v = line_intersect( |
| left, right - left, point(0, sweepline), point(1, 0) |
| ); |
| LD d1 = (0.5 * (left + right) - v).sz2(), d2 = (0.5 * (left - right)).sz2(); |
| return v.x + sign * sqrt(max(0.0L, d1 - d2)); |
| } |
|
|
| struct Beachline { |
| struct node { |
| point pt; |
| int idx; |
| int end; |
| node *link[2], *par, *prv, *nxt; |
|
|
| node() {} |
| node(point _pt, int idx): |
| pt(_pt), idx(idx), end(0), link{0, 0}, par(0), prv(0), nxt(0) {} |
| } *root; |
|
|
| LD sweepline; |
|
|
| Beachline() : sweepline(-1e20), root(NULL) { } |
|
|
| inline int dir(node* x) { |
| return x->par->link[0] != x; |
| } |
|
|
| |
| |
| |
| |
| |
| void rotate(node* n) { |
| node* p = n->par; |
| int d = dir(n); |
| p->link[d] = n->link[!d]; |
| if (n->link[!d]) { |
| n->link[!d]->par = p; |
| } |
| n->par = p->par; |
| if (p->par) { |
| p->par->link[dir(p)] = n; |
| } |
| n->link[!d] = p; |
| p->par = n; |
| } |
|
|
| void splay(node* x, node* f = NULL) { |
| while (x->par != f) { |
| if (x->par->par == f) { |
| |
| } else if (dir(x) == dir(x->par)) { |
| rotate(x->par); |
| } else { |
| rotate(x); |
| } |
| rotate(x); |
| } |
| if (f == NULL) { |
| root = x; |
| } |
| } |
|
|
| void insert(node* n, node* p, int d) { |
| splay(p); |
| node *c = p->link[d]; |
| n->link[d] = c; |
| if (c) { |
| c->par = n; |
| } |
| p->link[d] = n; |
| n->par = p; |
| node *prv = !d ? p->prv : p, *nxt = !d ? p : p->nxt; |
| n->prv = prv; |
| if (prv) { |
| prv->nxt = n; |
| } |
| n->nxt = nxt; |
| if (nxt) { |
| nxt->prv = n; |
| } |
| } |
|
|
| void erase(node* n) { |
| node *prv = n->prv, *nxt = n->nxt; |
| if (!prv && !nxt) { |
| if (n == root) { |
| root = NULL; |
| } |
| return; |
| } |
| n->prv = NULL; |
| if (prv) { |
| prv->nxt = nxt; |
| } |
| n->nxt = NULL; |
| if (nxt) { |
| nxt->prv = prv; |
| } |
| splay(n); |
| if (!nxt) { |
| root->par = NULL; |
| n->link[0] = NULL; |
| root = prv; |
| } else { |
| splay(nxt, n); |
| node* c = n->link[0]; |
| nxt->link[0] = c; |
| c->par = nxt; |
| n->link[0] = NULL; |
| n->link[1] = NULL; |
| nxt->par = NULL; |
| root = nxt; |
| } |
| } |
|
|
| bool get_event(node *cur, LD &next_sweep) { |
| if (!cur->prv || !cur->nxt) { |
| return false; |
| } |
| point u = (cur->pt - cur->prv->pt).r90(); |
| point v = (cur->nxt->pt - cur->pt).r90(); |
| if (dcmp(u / v) != 1) { |
| return false; |
| } |
| point p = get_circumcenter(cur->pt, cur->prv->pt, cur->nxt->pt); |
| next_sweep = p.y + (p - cur->pt).size(); |
| return true; |
| } |
|
|
| node* find_beachline(LD x) { |
| node* cur = root; |
| while (cur) { |
| LD left = cur->prv |
| ? parabola_intersect(cur->prv->pt, cur->pt, sweepline) |
| : -1e30; |
| LD right = cur->nxt |
| ? parabola_intersect(cur->pt, cur->nxt->pt, sweepline) |
| : 1e30; |
| if (left <= x && x <= right) { |
| splay(cur); |
| return cur; |
| } |
| cur = cur->link[x > right]; |
| } |
| return NULL; |
| } |
| }; |
|
|
| using BeachNode = Beachline::node; |
|
|
| static BeachNode arr[LIM]; |
| static int sz; |
| static BeachNode* new_node(point point, int idx) { |
| arr[sz] = BeachNode(point, idx); |
| return arr + (sz++); |
| } |
|
|
| struct event { |
| int type, idx, prv, nxt; |
| BeachNode *cur; |
| LD sweep; |
|
|
| event(LD sweep, int idx) : type(0), sweep(sweep), idx(idx) {} |
| event(LD sweep, BeachNode* cur) : |
| type(1), sweep(sweep), prv(cur->prv->idx), cur(cur), nxt(cur->nxt->idx) {} |
|
|
| bool operator>(const event& l) const { return sweep > l.sweep; } |
| }; |
|
|
| void voronoi_diagram( |
| vector<point> &input, |
| vector<point> &vertex, |
| vector<pii> &edge, |
| vector<pii> &area |
| ) { |
| Beachline beachline = Beachline(); |
| priority_queue<event, vector<event>, greater<event>> events; |
|
|
| auto add_edge = [&](int u, int v, int a, int b, BeachNode* c1, BeachNode* c2) { |
| if (c1) { |
| c1->end = edge.size() * 2; |
| } |
| if (c2) { |
| c2->end = edge.size() * 2 + 1; |
| } |
| edge.emplace_back(u, v); |
| area.emplace_back(a, b); |
| }; |
| auto write_edge = [&](int idx, int v) { |
| if (idx % 2 == 0) { |
| edge[idx / 2].first = v; |
| } else { |
| edge[idx / 2].second = v; |
| } |
| }; |
| auto add_event = [&](BeachNode* cur) { |
| LD nxt; |
| if (beachline.get_event(cur, nxt)) { |
| events.emplace(nxt, cur); |
| } |
| }; |
|
|
| int n = input.size(), cnt = 0; |
| sz = 0; |
| sort(input.begin(), input.end(), [](const point &l, const point &r) { |
| return l.y != r.y ? l.y < r.y : l.x < r.x; |
| }); |
|
|
| BeachNode *tmp = beachline.root = new_node(input[0], 0), *t2; |
| for (int i = 1; i < n; i++) { |
| if (dcmp(input[i].y - input[0].y) == 0) { |
| add_edge(-1, -1, i - 1, i, 0, tmp); |
| beachline.insert(t2 = new_node(input[i], i), tmp, 1); |
| tmp = t2; |
| } else { |
| events.emplace(input[i].y, i); |
| } |
| } |
|
|
| while (events.size()) { |
| event q = events.top(); |
| events.pop(); |
| BeachNode *prv, *cur, *nxt, *site; |
| int v = vertex.size(), idx = q.idx; |
| beachline.sweepline = q.sweep; |
| if (q.type == 0) { |
| point pt = input[idx]; |
| cur = beachline.find_beachline(pt.x); |
| beachline.insert(site = new_node(pt, idx), cur, 0); |
| beachline.insert(prv = new_node(cur->pt, cur->idx), site, 0); |
| add_edge(-1, -1, cur->idx, idx, site, prv); |
| add_event(prv); |
| add_event(cur); |
| } else { |
| cur = q.cur; |
| prv = cur->prv; |
| nxt = cur->nxt; |
| if (!prv || !nxt || prv->idx != q.prv || nxt->idx != q.nxt) { |
| continue; |
| } |
| vertex.push_back(get_circumcenter(prv->pt, nxt->pt, cur->pt)); |
| write_edge(prv->end, v); |
| write_edge(cur->end, v); |
| add_edge(v, -1, prv->idx, nxt->idx, 0, prv); |
| beachline.erase(cur); |
| add_event(prv); |
| add_event(nxt); |
| } |
| } |
| |
| } |
|
|
| int XR, YR; |
| int N, M; |
| vector<pair<int, LD>> adj[LIM]; |
| point KP[2]; |
| int KN[2]; |
| LD ans; |
|
|
| void process_voronoi_diagrams(vector<point> P) { |
| int i, j, k; |
| vector<point> vertex; |
| vector<pii> edge, area; |
| voronoi_diagram(P, vertex, edge, area); |
| M = vertex.size(); |
| for (int i = 0; i < area.size(); i++) { |
| int e1 = edge[i].first, e2 = edge[i].second; |
| if (e1 < 0 || e2 < 0) { |
| continue; |
| } |
| point v1 = vertex[e1], v2 = vertex[e2]; |
| if ( |
| min(v1.x, v2.x) < -EPS || max(v1.x, v2.x) > XR + EPS || |
| min(v1.y, v2.y) < -EPS || max(v1.y, v2.y) > YR + EPS |
| ) { |
| continue; |
| } |
| |
| int pi1 = area[i].first, pi2 = area[i].second; |
| point p1 = P[pi1], p2 = P[pi2]; |
| LD d = (p1 - project_point_segment(v1, v2, p1)).size(); |
| adj[e1].emplace_back(e2, d); |
| adj[e2].emplace_back(e1, d); |
| |
| point mid = 0.5 * (p1 + p2); |
| for (int j : {0, 1}) { |
| for (int k : {0, 1}) { |
| point kp = KP[j], p = k ? p2 : p1; |
| ans = min(ans, (kp - p).size()); |
| if (point_in_polygon_incl({p, v1, mid}, kp)) { |
| KN[j] = e1; |
| } |
| if (point_in_polygon_incl({p, mid, v2}, kp)) { |
| KN[j] = e2; |
| } |
| } |
| } |
| } |
| } |
|
|
| LD solve() { |
| vector<point> P; |
| for (int i = 0; i < LIM; i++) { |
| adj[i].clear(); |
| } |
| |
| cin >> XR >> YR; |
| cin >> KP[0].x >> KP[0].y >> KP[1].x >> KP[1].y; |
| cin >> N; |
| for (int i = 0; i < N; i++) { |
| int a, b; |
| cin >> a >> b; |
| P.emplace_back(a, b); |
| |
| P.emplace_back(-a, b); |
| P.emplace_back(a, -b); |
| P.emplace_back(2 * XR - a, b); |
| P.emplace_back(a, 2 * YR - b); |
| } |
| |
| ans = 1e18; |
| KN[0] = KN[1] = -1; |
| process_voronoi_diagrams(P); |
| assert(KN[0] >= 0 && KN[1] >= 0); |
| |
| vector<bool> visit(M); |
| priority_queue<pair<LD, int>> Q; |
| Q.emplace(ans, KN[0]); |
| while (!Q.empty()) { |
| ans = min(ans, Q.top().first); |
| int i = Q.top().second; |
| Q.pop(); |
| if (i == KN[1]) { |
| break; |
| } |
| if (visit[i]) { |
| continue; |
| } |
| visit[i] = true; |
| for (auto c : adj[i]) { |
| if (!visit[c.first]) { |
| Q.emplace(c.second, c.first); |
| } |
| } |
| } |
| return ans; |
| } |
|
|
| int main() { |
| cout.precision(10); |
| int T; |
| cin >> T; |
| for (int t = 1; t <= T; t++) { |
| cout << "Case #" << t << ": " << fixed << solve() << endl; |
| } |
| return 0; |
| } |
|
|