Closest point of poj 3714 Deformation

Source: Internet
Author: User
Raid
Time Limit:5000 MS   Memory Limit:65536 K
Total Submissions:7075   Accepted:2098

Description

After successive failures in the battles against the Union, the Empire retreated to its last stronghold. depending on its powerful defense system, the Empire repelled the six waves of Union's attack. after several sleepless nights of thinking, Arthur, General
Of the Union, noticed that the only weakness of the defense system was its energy supply. The system was chargedNNuclear power stations and breaking down any of them wocould disable the system.

The general soon started a raid to the stationsNSpecial agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur
Soon realized that he needed to rearrange the plan. the first thing he wants to know now is that which agent is the nearest to any power station. cocould you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

Input

The first line is a integerTRepresenting the number of test cases.
Each test case begins with an integerN(1 ≤N≤ 100000 ).
The nextNLines describe the positions of the stations. Each line consists of two integersX(0 ≤X≤ 1000000000) andY(0 ≤Y≤ 1000000000) indicating the positions of the station.
The next followingNLines describe the positions of the agents. Each line consists of two integersX(0 ≤X≤ 1000000000) andY(0 ≤YLess than 1000000000) indicating the positions of the agent.

Output

For each test case output the minimum distance with precision of three decimal placed in a separate line.

Sample Input

240 00 11 01 12 22 33 23 340 00 00 00 00 00 00 00 0

Sample Output

1.4140.000
This is a very typical closest point, that is, to find the closest distance of the above series of points. It has a special requirement that the two points of this distance must be in different sets. In this case, you only need to set the distance between the two vertices in the same set in the closest vertex to infinity.
The following code is used:
# Include <stdlib. h> # include <stdio. h> # include <string. h> # include <math. h> const int N = 200002; const double MAX = 10e100, eps = 0.00001; // mark! Struct Point {double x, y; int index; short set; // The core must ensure that the two points are in different vertex sets} a [N], B [N], c [N]; inline double min (double p, double q) {return p <q? P: q;} inline double dis (Point p, Point q) {double x1 = p. x-q. x; double y1 = p. y-q. y; return sqrt (x1 * x1 + y1 * y1);} int cmp_x (const void * p, const void * q) {double temp = (Point *) p) -> x-(Point *) q)-> x; if (temp> 0) {return 1;} else if (fabs (temp) <eps) {return 0;} else {return-1 ;}} int cmp_y (const void * p, const void * q) {double temp = (Point *) p) -> y-(Point *) q)-> y; if (temp> 0 ){ Return 1;} else if (fabs (temp) <eps) {return 0;} else {return-1 ;}} void merge (Point p [], Point q [], int s, int m, int t) {int I, j, k; for (I = s, j = m + 1, k = s; I <= m & j <= t;) {if (q [I]. y> q [j]. y) {p [k ++] = q [j]; ++ j ;}else {p [k ++] = q [I]; ++ I ;}} while (I <= m) {p [k ++] = q [I]; ++ I ;}while (j <= t) {p [k ++] = q [j]; ++ j ;}} double closest (Point * a, Point * B, Point * c, int p, int q) {if (q-p = 1 ){ If (a [p]. set! = A [q]. set) return dis (a [p], a [q]); else return MAX;} if (q-p = 2) {double x1, x2, x3; if (a [p]. set! = A [q]. set) x1 = dis (a [p], a [q]); else x1 = MAX; if (a [p + 1]. set! = A [q]. set) x2 = dis (a [p + 1], a [q]); else x2 = MAX; if (a [p]. set! = A [p + 1]. set) x3 = dis (a [p], a [p + 1]); else x3 = MAX; double m = min (x1, x2); return min (m, x3);} int I, j, k, m = (p + q)> 1; double d1, d2; for (I = p, j = p, k = m + 1; I <= q; ++ I) {if (B [I]. index <= m) {c [j ++] = B [I];} else {c [k ++] = B [I] ;}} d1 = closest (a, c, B, p, m); d2 = closest (a, c, B, m + 1, q); double dm = min (d1, d2); merge (B, c, p, m, q); for (I = p, k = p; I <= q; ++ I) {if (fabs (B [I]. x-B [m]. x) <dm) {C [k ++] = B [I] ;}} for (I = p; I <k; ++ I) {for (j = I + 1; j <k & c [j]. y-c [I]. y <dm; ++ j) {if (c [j]. set! = C [I]. set) {double temp = dis (c [j], c [I]); if (temp <dm) {dm = temp ;}}} return dm ;} // model endint main () {// freopen ("in.txt", "r", stdin); int n, I, t; double ans; scanf ("% d ", & t); while (t --) {scanf ("% d", & n); int mid = n; n = n <1; for (I = 0; I <n; ++ I) {scanf ("% lf", & a [I]. x, & a [I]. y); if (I <mid) a [I]. set = 0; else a [I]. set = 1 ;}qsort (a, n, sizeof (a [0]), cmp_x); for (I = 0; I <n; ++ I) {a [I]. index = I;} memcpy (B, a, sizeof (a [0]) * n); qsort (B, n, sizeof (B [0]), cmp_y ); ans = closest (a, B, c, 0, n-1); printf ("%. 3lf \ n ", ans);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.