【分治】【二分】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.  

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

题目大意

给出平面上的N个发电厂和N个特工的坐标,求出离某个发电厂最近的特工与发电厂的距离。

思路

这里涉及到一个重要知识点:求平面上最近点对
知道了这个,就很容易做了(不是一模一样吗……),每个点多一个属性:阵营(flag),电厂的flag为0,特工为1,计算dis时,如果两个点的flag相同返回极大值即可。

代码

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

#define MAXN 100000
#define INF 0x7fffffff
struct point
{
    double x,y;
    int f;//点的“阵营”
}a[MAXN*2+5];//注意是两倍
int N;
int t[MAXN*2+5];
/*以下是“求平面最小点对”的模板*/
bool cmp1(point x,point y){return x.x<y.x;}
bool cmp2(int x,int y){return a[x].y<a[y].y;}

double dis(point x,point y)
{
    if(x.f==y.f) return INF;//f一样则返回极大值
    return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}

double F(int l,int r)
{
    if(r-l==0)
        return INF;
    if(r-l==1)
        return dis(a[l],a[r]);
    int mid=(l+r)>>1;
    double ans=min(F(l,mid),F(mid+1,r));
    int cnt=0;
    for(int i=l;i<=r;i++)
        if(a[i].x>=a[mid].x-ans&&a[i].x<=a[mid].x+ans)
            t[++cnt]=i;
    sort(t+1,t+cnt+1,cmp2);
    for(int i=1;i<=cnt;i++)
        for(int j=i+1;j<=cnt;j++)
        {
            if(a[t[j]].y>=a[t[i]].y+ans) break;
            ans=min(ans,dis(a[t[i]],a[t[j]]));
        }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        for(int i=1;i<=N;i++)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
            a[i].f=0;//电厂
        }
        for(int i=1;i<=N;i++)
        {
            scanf("%lf%lf",&a[N+i].x,&a[N+i].y);
            a[N+i].f=1;//特工
        }
        N*=2;
        sort(a+1,a+N+1,cmp1);
        printf("%.3lf\n",F(1,N));
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是 poj3714 突袭的代码实现,使用了 Kruskal 算法求解最小生成树: ```c++ #include <iostream> #include <algorithm> #include <vector> using namespace std; const int MAXN = 20005; const int MAXM = 100005; struct Edge { int u, v, w; bool operator<(const Edge& other) const { return w < other.w; } }; int n, m, p; int parent[MAXN], depth[MAXN]; Edge edges[MAXM]; void make_set(int v) { parent[v] = v; depth[v] = 0; } int find_set(int v) { if (v == parent[v]) { return v; } return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (depth[a] < depth[b]) { swap(a, b); } parent[b] = a; if (depth[a] == depth[b]) { depth[a]++; } } } int kruskal() { int ans = 0; for (int i = 1; i <= n; i++) { make_set(i); } sort(edges, edges + m); for (int i = 0; i < m; i++) { int u = edges[i].u, v = edges[i].v, w = edges[i].w; if (find_set(u) != find_set(v)) { union_sets(u, v); ans += w; } } return ans; } int main() { while (cin >> n >> m >> p) { for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; edges[i] = {u, v, w}; } int ans1 = kruskal(); for (int i = 1; i <= n; i++) { make_set(i); } for (int i = 0; i < p; i++) { int u, v, w; cin >> u >> v >> w; edges[i] = {u, v, -w}; } int ans2 = kruskal(); cout << ans2 - ans1 << endl; } return 0; } ``` 在这个实现中,使用了一个 `Edge` 结构体来表示一条边,包括起点、终点和边权。然后使用 Kruskal 算法求解最小生成树,分别计算突袭前和突袭后的最小生成树的权值和,最终答案为突袭后的权值和减去突袭前的权值和。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值