poj 3714 Raid(最近点对)
2013-12-16 13:08
417 查看
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
Sample Output
Source
POJ Founder Monthly Contest – 2008.12.28, Dagger
题意:求2个点集最近点的距离
题解:
我的K-D树模板4900+ms卡时间过。。。难道是 long long 耗时,不过中间不开根号会爆int的
暴力+剪枝1400+ms过。。。哎
暴力剪枝
K-D树
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 7884 | Accepted: 2355 |
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
Source
POJ Founder Monthly Contest – 2008.12.28, Dagger
题意:求2个点集最近点的距离
题解:
我的K-D树模板4900+ms卡时间过。。。难道是 long long 耗时,不过中间不开根号会爆int的
暴力+剪枝1400+ms过。。。哎
暴力剪枝
#include<stdio.h> #include<stdlib.h> #include<math.h> #define eps 1e-8 #define MAX 200003 struct point{ double x,y; int flag; }p[MAX]; int cmp(const void *a,const void *b) { struct point c=*(struct point *)a; struct point d=*(struct point *)b; if(fabs(c.x-d.x)<eps) return c.y<d.y?-1:1; return c.x<d.x?-1:1; } double MIN(double x,double y){ return x<y?x:y; } double dis(struct point p1,struct point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } double solve(int n) { int i,j; double res=0xfffff; qsort(p,n,sizeof(p[0]),cmp); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(p[i].flag==p[j].flag) continue; res=MIN(res,dis(p[i],p[j])); if(p[j].x-p[i].x>=res) break; } } return res; } int main() { int t,i,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); p[i].flag=0; } for(i=n,n=2*n;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); p[i].flag=1; } printf("%.3lf\n",solve(n)+eps); } }
K-D树
#include<cstdio> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; typedef pair<int, LL> PII; typedef pair<int, int> pii; const int maxn = 211222; const int maxD = 2; const int maxM = 12; const LL INF = 4611686018427387903LL; int now; struct TPoint { int x[maxD]; void read() { for (int i = 0; i < 2; ++i) scanf("%d", x + i); } } p[maxn]; bool cmp(const TPoint& a, const TPoint& b) { return a.x[now] < b.x[now]; } template<typename T> T sqr(T n) { return n * n; } struct KDtree { int K, n, top; int split[maxn]; LL dis2[maxM]; TPoint stk[maxn]; TPoint kp[maxn]; TPoint mp; void build(int l, int r) { if (l >= r) return; int i, j, mid = (l + r) >> 1; LL dif[maxD], mx; for (i = 0; i < K; ++i) { mx = dif[i] = 0; for (j = l; j < r; ++j) mx += kp[j].x[i]; mx /= r - l; for (j = l; j < r; ++j) dif[i] += sqr(kp[j].x[i] - mx); } now = 0; for (i = 1; i < K; ++i) if (dif[now] < dif[i]) now = i; split[mid] = now; nth_element(kp + l, kp + mid, kp + r, cmp); build(l, mid); build(mid + 1, r); } void update(const TPoint& p, int M) { int i, j; LL tmp = dist(p, mp); for (i = 0; i < M; ++i) if (dis2[i] > tmp) { for (j = M - 1; j > i; --j) { stk[j] = stk[j - 1]; dis2[j] = dis2[j - 1]; } stk[i] = p; dis2[i] = tmp; break; } } void nearest_search(int l, int r, int M) { if (l >= r) return; int mid = (l + r) >> 1; update(kp[mid], M); if (l + 1 == r) return; LL d = mp.x[split[mid]] - kp[mid].x[split[mid]]; if (d <= 0) { nearest_search(l, mid, M); if (sqr(d) < dis2[M - 1]) nearest_search(mid + 1, r, M); } else { nearest_search(mid + 1, r, M); if (sqr(d) < dis2[M - 1]) nearest_search(l, mid, M); } } void find_nearest(TPoint p, int M) { for (int i = 0; i < M; ++i) { dis2[i] = INF; } mp = p; nearest_search(0, n, M); } LL dist(const TPoint& a, const TPoint& b) { LL res = 0; for (int i = 0; i < K; ++i) res += sqr<LL>(a.x[i] - b.x[i]); return res; } } KD; int main() { int i, n, T; long long res; scanf("%d", &T); while (T--) { scanf("%d", &n); KD.n = n; KD.K = 2; for (i = 0; i < n; ++i) { p[i].read(); KD.kp[i] = p[i]; } for(i=n;i<2*n;i++) { p[i].read(); } KD.build(0, n); res=INF; for (i = 0; i < n; ++i) { KD.find_nearest(p[i+n], 2); res=min((LL)res,KD.dis2[0]); } printf("%.3f\n",sqrt(1.0*res)); } return 0; }
相关文章推荐
- POJ 3714 Raid(计算几何の最近点对)
- POJ 3714 Raid 平面最近点对
- poj 3714 Raid(平面最近点对)
- POJ 3714 Raid(求两集合之间的最近点对)
- Raid POJ - 3714 (平面最近点对,分治)
- poj 3714 Raid 分治法求平面最近点对
- POJ_3714_Raid(平面最近点对)
- POJ 3714 Raid 最近点对
- POJ 3714 Raid(最近点对)
- POJ 3714 Raid(平面最近点对)
- POJ 3714(Raid-平面最近点对)
- POJ-3714 Raid 平面最近点对
- poj 3714Raid(最近点对)
- poj 3714 Raid (类型不同点间的最近点对)
- POJ 3714 Raid (最近点对)感觉数据怪怪的
- POJ 3714 Raid 最近对点题解
- poj 3714 Raid (类型不同点间的最近点对)
- POJ 3714 Raid(变种最近点对问题:分治+剪枝)
- Raid POJ - 3714 分治最近点对模板
- POJ 3714 Raid 最近点对