Time Limit: 5000MS   Memory Limit: 65536K

Total Submissions: 7473

  Accepted: 2221

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 charged by N nuclear power stations and breaking down any of them would disable the system.

The general soon started a raid to the stations by N special 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. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

Input

The first line is a integer T representing the number of test cases. Each test case begins with an integer N (1 ≤ N ≤ 100000). The next N lines describe the positions of the stations. Each line consists of two i

ntegers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station. The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 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

2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

Sample Output

1.414
0.000

Source

 
 
 
 
 
 
最近点对,采用分治方法。过程:
1对原数组依据x左标从小到大排序。
2二分数组,左边求出最小值,右边求出最小值,我们求最小的。
3找出对于左右两边的可能小于当前最小值的最近点对,更新最小值。
 
本题很水,直接套模板就秒过!!!
这题目需要区分一下点,让我们求的是闪兵到任意一个核电站的最短距离,加一个标志就可以了。(若是同一类的点就规定距离为oo!!)
 
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring> #define DX(x) ((x) * (x))
using namespace std; const int MAX = + ;
const double INF = 10e100;
struct Point
{
double x, y;
int index,flag;//flag分类!!
}A[MAX], B[MAX], C[MAX]; bool compX(const Point& a, const Point& b)
{
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
} bool compY(const Point& a, const Point& b)
{
if(a.y == b.y)
return a.x < b.x;
return a.y < b.y;
} inline double getMin(double a, double b)
{
return a < b ? a : b;
} double getDist(Point& a, Point& b)
{
if(a.flag==b.flag)return INF;//同类规定距离为oo!!
return sqrt(DX(a.x - b.x) + DX(a.y - b.y));
} void merge(Point p[], Point q[], int s, int m, int t)
{
int i, j, k;
for(i = s, j = m + , 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++];
while(j <= t)
p[k++] = q[j++];
} double closest(Point a[], Point b[], Point c[], int p, int q)
{
if(q - p == )
return getDist(a[p], a[q]);
if(q - p == )
{
double x1 = getDist(a[p], a[q]);
double x2 = getDist(a[p + ], a[q]);
double x3 = getDist(a[p], a[p + ]);
return getMin(x1, getMin(x2, x3));
}
int i, j, k, m = (p + q) / ;
double d1, d2;
for(i = p, j = p, k = m + ; 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 + , q);
double dm = getMin(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 + ; j < k && c[j].y - c[i].y < dm; ++j)
{
double temp = getDist(c[i], c[j]);
if(temp < dm)
dm = temp;
}
return dm;
}
int main()
{
//freopen("in.txt", "r", stdin);
int T,n;
scanf("%d",&T);
while( T--)
{
scanf("%d", &n);
for(int i = ; i < n; ++i)
scanf("%lf%lf", &A[i].x, &A[i].y),A[i].flag=;
for(int i = ; i < n; ++i)
scanf("%lf%lf", &A[i+n].x, &A[i+n].y),A[i+n].flag=;
sort(A, A + *n, compX);
for(int i = ; i < *n; ++i)
A[i].index = i;
memcpy(B, A, *n * sizeof(B[]));
sort(B, B + *n, compY);
double d = closest(A, B, C, , *n - );
printf("%.3f\n", d);
}
return ;
}
 

poj 3714 Raid(平面最近点对)的更多相关文章

  1. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

  2. POJ 3714 Raid

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  3. 『Raid 平面最近点对』

    平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...

  4. POJ 3714 Raid(计算几何の最近点对)

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  5. POJ 3714 Raid(平面近期点对)

    解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...

  6. poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】

    题目:  http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...

  7. POJ-3714 Raid 平面最近点对

    题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...

  8. POJ 3714 Raid 近期对点题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  9. (洛谷 P1429 平面最近点对(加强版) || 洛谷 P1257 || Quoit Design HDU - 1007 ) && Raid POJ - 3714

    这个讲的好: https://phoenixzhao.github.io/%E6%B1%82%E6%9C%80%E8%BF%91%E5%AF%B9%E7%9A%84%E4%B8%89%E7%A7%8D ...

随机推荐

  1. Vue中computed、methods、watch的联系和区别

    computed是计算树形,methods是方法. new Vue({ el: '#example', data: { message: 'Hello' }, computed: { reversed ...

  2. @清晰掉 swap函数

    swap函数估计是一个各种各样程序都会频繁用到的子程序,可是你知道它究竟有多少种不同的写法吗?下面我就列举我知道的几种swap函数来跟大家分享一下. (1)经典型---嫁衣法 无论是写程序还是干其他事 ...

  3. 代码编译时JDK版本和运行时JDK版本不一致启动项目报错

    java编译: java编译就是.java文件变成.class文件的过程,这个过程一般在我们常用的编译器中进行,例如Ecliplse和IDEA等:下面以IDEA举例: 执行上述编译使用的JDK版本就是 ...

  4. 【2】通过Ajax方式上传文件(图片),使用FormData进行Ajax请求

    HTML: <form id= "uploadForm"> <p >指定文件名: <input type="text" name= ...

  5. 003-unity3d 物理引擎-示例2 打箱子

    一.基础知识点 1.坐标.向量等 )) { //1.将鼠标坐标 转化为 世界坐标 由于鼠标z轴 可能不存在,故自定义为3 Vector3 targetPos = Camera.main.ScreenT ...

  6. 中国MOOC_零基础学Java语言_第5周 数组_1多项式加法

    第5周编程题 查看帮助 返回   第5周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截 ...

  7. flutter dialog异常Another exception was thrown: Navigator operation requested with a context that does not include a Navigator

    我在使用flutter里的对话框控件的时候遇到了一个奇怪的错误 Another exception was thrown: Navigator operation requested with a c ...

  8. 虚拟化 RemoteApp 远程接入 源码 免费

    远程接入 RemoteApp 虚拟化 源码 免费 1.终端安装与配置: 此远程接入组件的运行原理与瑞友天翼.异速连.CTBS等市面上常见的远程接入产品一样,是透过Windows的终端服务来实现的,速度 ...

  9. 2019暑假第二周(hadoop在个人电脑上的搭建)

    一,Hadoop和NoSQL数据库的学习,大多需要Linux环境. 搭建Linux环境可以分为两种方式: (1)在电脑上安装双操作系统,即同时安装Linux和Windows操作系统,在电脑启动的时候, ...

  10. IDEA无法导入HttpServlet包解决方法

    1.maven项目 直接在pom.xml中添加对java servlet api的依赖即可,比较常用的一个servlet版本3.1.0的依赖如下: <!-- https://mvnreposit ...