POJ 2659 Raid|分治法|平面最近点对

题目描述

总时间限制: 1000ms 内存限制: 65536kB

描述

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?

输入

The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 1000).
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.  

输出

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

样例输入

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

样例输出

1.414
0.000

问题解决

1.暴力

这到底第一个想到的必然是暴力哈哈哈,只要O(n^2)就可以轻松水过:

#include <iostream>
#include <algorithm>
using namespace std;
struct node{
    int x,y;
};
node station[1005];
node agent[1005];
int T,N;


int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        for(int i=0;i<N;i++){
            scanf("%d%d",&station[i].x,&station[i].y);
        }
        for(int i=0;i<N;i++){
            scanf("%d%d",&agent[i].x,&agent[i].y);
        }
        double mindist=10000000;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                double dist=sqrt((station[i].x-agent[j].x)*(station[i].x-agent[j].x)+(station[i].y-agent[j].y)*(station[i].y-agent[j].y));
                if(dist<mindist) mindist=dist;
            }
        }
        printf("%.3f\n",mindist);

    }
    return 0;
}

2.分治法

But!老师说了不能水过,所以就用了课上讲的closest pair算法,重新写了一遍,这个算法主要就是将所有点按照横坐标排序,然后从中间一分为二,找到左右两边的最短距离,然后在最短距离内遍历所有的pair,如有更小的就更新。

#include <iostream>
#include <algorithm>
#define station 0
#define agent 1
using namespace std;

typedef long long LL;
const LL INF=10000000000;

struct node{
    LL x,y;
    int id;
    node(LL x=0,LL y=0, int id=0):x(x),y(y),id(id){}
    const bool operator <(const node other)const{
        if(x==other.x) return y<other.y;
        else return x<other.x;
    }
}nodes[2005];

double dis(int a, int b) {
    return sqrt((double)((nodes[a].x - nodes[b].x)*(nodes[a].x - nodes[b].x) + (nodes[a].y - nodes[b].y)*(nodes[a].y - nodes[b].y)));
}
int T,N;

double closestpair(int l,int r){
    if (l==r) return INF;
    int mid=(l+r)>>1;
    double d1=closestpair(l,mid);
    double d2=closestpair(mid+1,r);
    double d=min(d1,d2);
    for(int i=mid;i>=1;i--){
        if(nodes[mid].x-nodes[i].x > d) break;
        for(int j=mid+1;j<=r;j++){
            if(nodes[j].x-nodes[mid].x > d) break;
            double tmp = dis(i,j);
            if(nodes[i].id!=nodes[j].id && tmp<d) d=tmp;
        }
    }
    return d;
}

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        for(int i=0;i<N;i++){
            scanf("%lld%lld",&nodes[i].x,&nodes[i].y);
            nodes[i].id=station;
        }
        for(int i=N;i<2*N;i++){
            scanf("%lld%lld",&nodes[i].x,&nodes[i].y);
            nodes[i].id=agent;
        }
        sort(nodes,nodes+2*N);
        double mindist = closestpair(0,2*N-1);
        printf("%.3lf\n",mindist);

    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值