POJ 3714 Raid(计算几何の最近点对)
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 integers 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.
题目大意:给一个点集A,一个点集B,求min(distance(x, y))(x∈A,y∈B)
思路1:分治法。把点集按x坐标从小到大排序,mid = (left + right)/2,递归分治计算出左边部分和右边部分的最小距离mind,那么,若左半部分和右半部分存在一对点距离小于mind,那么这两个点一定在范围(x[mid] -mind ,x[mid] -mind)之间(因为在这之外的点与对面的点的距离必然大于mind)
思路2:暴力枚举+剪枝。暴力枚举每两个点之间的距离,若y[j] - y[i] >= mind则break(这个不用解释了吧……)
PS:要保留3位小数,题目没说,可以看样例
分治算法:1141MS
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const double MAX_DIST = 1e100;
const int MAXN = ; struct Point {
double x, y;
bool flag;
}; inline double dist(const Point &a, const Point &b) {
if(a.flag != b.flag)
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
else
return MAX_DIST;
} Point pt[MAXN];
int y_sort[MAXN]; inline bool x_cmp(const Point &a, const Point &b) {
return a.x < b.x;
} inline bool y_cmp(const int &a, const int &b) {
return pt[a].y < pt[b].y;
} double shortest_distance(int left, int right) {
if(right - left == )
return dist(pt[left], pt[right]);
else if(right - left == )
return min(min(dist(pt[left], pt[left+]), dist(pt[left], pt[left+])),
dist(pt[left+], pt[left+]));
int mid = (left + right) >> ;
double mind = min(shortest_distance(left, mid), shortest_distance(mid+, right));
if(mind == ) return ;
int yn = ;
for(int i = mid; pt[mid].x - pt[i].x < mind && i >= left; --i)
y_sort[yn++] = i;
int y_mid = yn;
for(int i = mid+; pt[i].x - pt[mid].x < mind && i <= right; ++i)
y_sort[yn++] = i;
for(int i = ; i < y_mid; ++i) for(int j = y_mid; j < yn; ++j)
mind = min(mind, dist(pt[y_sort[i]], pt[y_sort[j]]));
return mind;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = false;
}
for(int i = n; i < *n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = true;
}
sort(pt, pt + *n, x_cmp);
printf("%.3f\n", shortest_distance(, * n - ));
}
}
分治算法:1485MS
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const double MAX_DIST = 1e100;
const int MAXN = ; struct Point {
double x, y;
bool flag;
}; inline double dist(const Point &a, const Point &b) {
if(a.flag != b.flag)
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
else
return MAX_DIST;
} Point pt[MAXN];
int y_sort[MAXN]; inline bool x_cmp(const Point &a, const Point &b) {
return a.x < b.x;
} inline bool y_cmp(const int &a, const int &b) {
return pt[a].y < pt[b].y;
} void _min(double &a, const double &b) {
if(a > b) a = b;
} double shortest_distance(int left, int right) {
if(right - left == )
return dist(pt[left], pt[right]);
else if(right - left == )
return min(min(dist(pt[left], pt[left+]), dist(pt[left], pt[left+])),
dist(pt[left+], pt[left+]));
int mid = (left + right) >> ;
double mind = min(shortest_distance(left, mid), shortest_distance(mid+, right));
if(mind == ) return ;
int yn = ;
for(int i = mid; pt[mid].x - pt[i].x < mind && i >= left; --i)
y_sort[yn++] = i;
for(int i = mid+; pt[i].x - pt[mid].x < mind && i <= right; ++i)
y_sort[yn++] = i;
sort(y_sort, y_sort + yn);
for(int i = ; i < yn; ++i) {
for(int j = i + ; j < yn; ++j) {
if(pt[y_sort[j]].y - pt[y_sort[i]].y >= mind) break;
_min(mind, dist(pt[y_sort[i]], pt[y_sort[j]]));
}
}
return mind;
} int main() {
freopen("f:/data.in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = false;
}
for(int i = n; i < *n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = true;
}
sort(pt, pt + *n, x_cmp);
printf("%.3f\n", shortest_distance(, * n - ));
}
}
暴力枚举+剪枝:1032MS
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const int MAXN = ; struct Point {
double x, y;
bool flag;
}; inline double dist(const Point &a, const Point &b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} Point pt[MAXN]; inline bool cmp(const Point &a, const Point &b) {
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
} double shortest_distance(int pn) {
double mind = dist(pt[], pt[pn/]);
sort(pt, pt + pn, cmp);
for(int i = ; i < pn; ++i) {
for(int j = i + ; j < pn; ++j) {
if(pt[i].flag == pt[j].flag) continue;
double t = dist(pt[i], pt[j]);
if(mind > t) mind = t;
if(pt[j].x - pt[i].x >= mind) break;
}
}
return mind;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = false;
}
for(int i = n; i < *n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = true;
}
printf("%.3f\n", shortest_distance( * n));
}
}
后记:这分治法能卡(暴力剪枝肯定能卡……),下面是生成卡分治数据的代码……
void gen1()
{
int N = , i;
printf("%d\n", N);
for( i = ; i < N; ++i )
printf("%d %d\n", -+i, i+);
for( i = ; i < N; ++i )
printf("%d %d\n", +i, i+); printf("%d\n", N);
for( i = ; i < N; ++i )
printf("%d %d\n", i-, -+i);
for( i = ; i < N; ++i )
printf("%d %d\n", i-, +i);
}
然后别人给的代码,待研究,出处不明……某份数据依然要4秒之多,但是要是用上面我的代码直接就不会动了……
初步估计,下面的代码用到了那个啥K-D tree,就是把一个二维空间先分x轴、再分y轴、再分x轴、再分y轴……分成多个平面,而不是像上面那样只分x(或y)轴……然后还是分治……代码我就懒得看了……超出我的水平上限了……
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <iostream>
#include <cmath> using namespace std; //维数
int K = ;
long long ans; struct kdNode
{
long long x[];
int div;
}; struct qNode
{
long long r;
kdNode p;
qNode( long long _r, kdNode _p )
{
r = _r;
p = _p;
}
bool operator<( const qNode& x ) const
{
return r < x.r;
}
}; int cmpNo;
int cmp( kdNode a, kdNode b )
{
return a.x[cmpNo] < b.x[cmpNo];
} long long dis2( kdNode& a, kdNode& b )
{
long long res = ;
for( int i = ; i < K; ++i )
res += (a.x[i]-b.x[i])*(a.x[i]-b.x[i]);
return res;
} void buildKD( int s, int e, kdNode* p, int d )
{
if( s > e ) return;
int mid = (s+e)/;
cmpNo = d;
nth_element(p+s, p+mid, p+e+, cmp);
p[mid].div = d;
buildKD(s, mid-, p, (d+)%K);
buildKD(mid+, e, p, (d+)%K);
} priority_queue<qNode> Q; void findKD( int s, int e, kdNode tar, kdNode* p, int cnt )
{
if( s > e ) return;
int mid = (s+e)/;
long long r = dis2(p[mid], tar);
ans = min(ans, r);
//t也许需要改成long long
long long t = tar.x[ p[mid].div ] - p[mid].x[ p[mid].div ];
if( t <= )
{
findKD(s, mid-, tar, p, cnt);
if( ans > t*t )
findKD(mid+, e, tar, p, cnt);
}
else if( t > )
{
findKD(mid+, e, tar, p, cnt);
if( ans > t*t )
findKD(s, mid-, tar, p, cnt);
}
} kdNode p[], q; int main()
{
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
int T, n, i, j;
int x, y;
scanf("%d", &T);
while( T-- )
{
scanf("%d", &n);
for( i = ; i < n; ++i )
{
scanf("%d %d", &x, &y);
p[i].x[] = x;
p[i].x[] = y;
}
ans = 4100000000000000000LL;
buildKD(, n-, p, K-);
for( i = ; i < n; ++i )
{
scanf("%d %d", &x, &y);
q.x[] = x;
q.x[] = y;
findKD(, n-, q, p, );
}
printf("%.3f\n", sqrt(ans+.));
}
return ;
}
POJ 3714 Raid(计算几何の最近点对)的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- poj 3714 Raid(平面最近点对)
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7473 Accepted: 2221 Description ...
- POJ 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- POJ 3714 Raid 近期对点题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- POJ 3714 Raid(平面近期点对)
解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...
- (洛谷 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 ...
- 【POJ 3714】 Raid
[题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...
- 【POJ 3714】Raid
[题目链接]:http://poj.org/problem?id=3714 [题意] 给你两类的点; 各n个; 然后让你求出2*n个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...
随机推荐
- Linux 运维工程师学习成长路线上要经历哪四个阶段?
之前曾看到一篇新闻,Linux之父建议大家找一份基于Linux和开源环境的工作.今天就来聊一聊我的想法,本人8年Linux运维一线经验,呆过很多互联网公司,从一线运维做到运维架构师一职,也见证了中国运 ...
- js判断当前浏览器是否是源生app的webview
有些时候,我们在开发过程中需要判断,当前页面被打开是否是处于源生的webview里面,或者NODEJS做服务器后端支持的时候,判断请求来源是否来至于源生webview里面被打开的页面请求GET/POS ...
- Java敲地鼠代码
package test; import java.awt.EventQueue; import java.awt.event.MouseAdapter; import java.awt.event. ...
- [转]关于sdk更新Android SDK Tools 25.3.1版本后使用sdk manager闪退
昨天这两个manager还工作正常,今天更新了一下,发现不可用了,运行avd manager和sdk manager没反应,搜了好多文章,然后看到了下这篇文章<关于sdk更新Android SD ...
- yii学习笔记(6),数据库操作(增删改)
数据库增删改操作通过活动记录实例来完成 插入记录 /* ----------添加记录---------- */ // 创建活动记录对象 $article = new Article(); $artic ...
- day 18 类与类之间的关系
类与类之间的关系 在我们的世界中事物和事物之间总会有一些联系. 在面向对象中,类和类之间也可以产生相关的关系 1.依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作, ...
- netfilter 学习摘要
netfilter 子系入口在L3,完成后把数据包发往L4 netfilter 主要功能: 数据包选择(iptables) 数据包过滤 网络地址转换(NAT) 数据包操纵(在路由选择之前或之后修改数据 ...
- python学习——函数进阶
首先来看下面这个函数. def func(x,y): bigger = x if x > y else y return bigger ret = func(10,20) print(ret) ...
- linux进程篇 (三) 进程间的通信1 管道通信
通信方式分4大类: 管道通信:无名管道 有名管道 信号通信:发送 接收 和 处理 IPC通信:共享内存 消息队列 信号灯 socke 网络通信 用户空间 进程A <----无法通信----> ...
- MongoDB入门---文档操作之增删改
之前的两篇文章,已经分享过关于MongoDB的集合还有数据库的各种操作,接下来就涉及到最主要的喽,那就是数据方面的操作,在这里叫做文档操作.话不多说,大家来看正文. 首先来看一下它的数据结构: ...