您的位置:首页 > 大数据 > 人工智能

poj3714 Raid(分治法…

2013-08-19 18:25 357 查看
Description

After successive failures in the battlesagainst the Union, the Empire retreated to its last stronghold.Depending on its powerful defense system, the Empire repelled thesix waves of Union's attack. After
several sleepless nights ofthinking, Arthur, General of the Union, noticed that the onlyweakness of the defense system was its energy supply. The systemwas charged by N nuclear
power stations andbreaking down any of them would disable the system.
Thegeneral soon started a raid to the stationsby N special agents who wereparadroped into the stronghold.
Unfortunately they failed to landat the expected positions due to the attack by the Empire AirForce. As an experienced general, Arthur soon realized that heneeded to rearrange the plan. The first thing he wants to know nowis that which agent is the nearest
to any power station. Could you,the chief officer, help the general to calculate the minimumdistance between an agent and a station?

Input

Thefirst line is a integer T representing the number oftest cases.

Each test case begins with aninteger N (1≤ N ≤
100000).

Thenext N lines describe thepositions of the stations. Each line consists of twointegers X (0≤ X ≤
1000000000)and Y (0≤ Y ≤ 1000000000) indicatingthe positions of the
station.

The nextfollowing N lines describe thepositions of the agents. Each line consists of twointegers X (0≤ X ≤
1000000000)and Y (0≤ Y ≤ 1000000000) indicatingthe positions of the
agent.  

Output

Foreach test case output the minimum distance with precision of threedecimal 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
POJ Founder Monthly Contest – 2008.12.28, Dagger

解析:
题目大意:给你两个集合A和B,求出最近点对,前提是两个点分别在不同的集合。典型的分治法求解最近点对。

代码:

#include
#include
#include
#include
using namespace std;
const int Max=200001;
const double inf=1e-8;
const double off=1e100;
struct Point{
double x,y;
bool flag;
};
Point p[Max];
inline int dbcmp(double tp){return tp<-inf?-1:tp>inf;}
inline double Distance(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmpx(Point a,Point b){return a.x
bool cmpy(int a,int b){return p[a].y
int ypos[Max];
double closedis(Point *p,int l,int r)
{
if(l==r) return off;
if(l+1==r){
if(p[l].flag==p[r].flag) return off;
else return Distance(p[l],p[r]);
}
int mid=(l+r)/2;
double ans=min(closedis(p,l,mid),closedis(p,mid+1,r)); //不要使用__min函数,会超时
int num=0;
for(int i=l;i<=r;i++) //将与mid点横坐标差距不超过ans的点收集起来
{
if(dbcmp(fabs(p[i].x-p[mid].x)-ans)<=0)
ypos[num++]=i;
}
sort(ypos,ypos+num,cmpy);
for(int i=0;i
{
for(int j=i+1;j
{
if(dbcmp(fabs(p[ypos[i]].y-p[ypos[j]].y)-ans)>=0||
else if(p[ypos[i]].flag!=
p[ypos[j]].flag)

ans=min(ans,Distance(p[ypos[i]],p[ypos[j]]));

}
}
return ans;
}

int main()
{
int n,m;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
for(int i=0;i
scanf("%lf%lf",&p[i].x,&p[i].y), p[i].flag=(ifalse:true);
sort(p,p+n*2,cmpx);
printf("%.3lf\n",closedis(p,0,n+n-1));

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: