POJ 3714 Raid
来源:互联网 发布:淘宝美工网站 编辑:程序博客网 时间:2024/05/16 16:17
Raid
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9636 Accepted: 2941Description
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 0Sample Output
1.414
0.000Source
POJ Founder Monthly Contest – 2008.12.28, Dagger
这道题就是套用的BZOJ2648(http://blog.csdn.net/javays1/article/details/50432064)的解题思路,而且更简单,因为不用新增点。 直接上代码:
#include <stdio.h>#include <algorithm>#include<math.h>using namespace std;#define N 100000short currentDim;int nextIndex = 0;struct point{ int pos[2]; int left; int right; int Min[2], Max[2]; bool operator < (const point &u) const { return pos[currentDim] < u.pos[currentDim]; }} point_set[N];point kd_tree[N];pair<double, point> nearest_point;double square(int x){ return (double)x * (double)x;}inline void construct_kdTree(int p, int r, int index, short depth){ if(index == -1) return; if(p <= r) { currentDim = depth % 2; int mid = (p + r) / 2; nth_element(point_set + p, point_set + mid, point_set+r+1); kd_tree[index] = point_set[mid]; nextIndex = index + 1; kd_tree[index].Min[0] = kd_tree[index].Max[0] = kd_tree[index].pos[0]; kd_tree[index].Min[1] = kd_tree[index].Max[1] = kd_tree[index].pos[1]; if(p > mid - 1) kd_tree[index].left = -1; else if(nextIndex < N) kd_tree[index].left = nextIndex++; else kd_tree[index].left = -1; construct_kdTree(p, mid-1, kd_tree[index].left, depth+1); if(mid + 1 > r) kd_tree[index].right = -1; else if(nextIndex < N) kd_tree[index].right = nextIndex++; else kd_tree[index].right = -1; construct_kdTree(mid+1, r, kd_tree[index].right, depth+1); if(kd_tree[index].left != -1) { int leftChild = kd_tree[index].left; if(kd_tree[index].Max[0] < kd_tree[leftChild].Max[0]) kd_tree[index].Max[0] = kd_tree[leftChild].Max[0]; if(kd_tree[index].Max[1] < kd_tree[leftChild].Max[1]) kd_tree[index].Max[1] = kd_tree[leftChild].Max[1]; if(kd_tree[index].Min[0] > kd_tree[leftChild].Min[0]) kd_tree[index].Min[0] = kd_tree[leftChild].Min[0]; if(kd_tree[index].Min[1] > kd_tree[leftChild].Min[1]) kd_tree[index].Min[1] = kd_tree[leftChild].Min[1]; } if(kd_tree[index].right != -1) { int rightChild = kd_tree[index].right; if(kd_tree[index].Max[0] < kd_tree[rightChild].Max[0]) kd_tree[index].Max[0] = kd_tree[rightChild].Max[0]; if(kd_tree[index].Max[1] < kd_tree[rightChild].Max[1]) kd_tree[index].Max[1] = kd_tree[rightChild].Max[1]; if(kd_tree[index].Min[0] > kd_tree[rightChild].Min[0]) kd_tree[index].Min[0] = kd_tree[rightChild].Min[0]; if(kd_tree[index].Min[1] > kd_tree[rightChild].Min[1]) kd_tree[index].Min[1] = kd_tree[rightChild].Min[1]; } }}inline double dist(int index, point p){ int x = 0, y = 0; if(p.pos[0] < kd_tree[index].Min[0]) x = kd_tree[index].Min[0] - p.pos[0]; if(p.pos[0] > kd_tree[index].Max[0]) x = p.pos[0] - kd_tree[index].Max[0]; if(p.pos[1] < kd_tree[index].Min[1]) y = kd_tree[index].Min[1] - p.pos[1]; if(p.pos[1] > kd_tree[index].Max[1]) y = p.pos[1] - kd_tree[index].Max[1]; return square(x) + square(y);}inline void query_kdTree(point p, int index, short depth){ if(index == -1) return; pair<double, point> current_node(0, kd_tree[index]); current_node.first = square(p.pos[0] - current_node.second.pos[0]) + square(p.pos[1] - current_node.second.pos[1]); //printf("%lf %d %d %d %d\n", current_node.first, p.pos[0], p.pos[1], //current_node.second.pos[0], current_node.second.pos[1]); if(current_node.first < nearest_point.first) { nearest_point.first = current_node.first; nearest_point.second = current_node.second; } int lchild = kd_tree[index].left; int rchild = kd_tree[index].right; //printf("left:%d %d %d\n",lchild, kd_tree[lchild].pos[0], kd_tree[lchild].pos[1]); //printf("right:%d %d %d\n",rchild, kd_tree[rchild].pos[0], kd_tree[rchild].pos[1]); if(lchild != -1 && rchild != -1) { double ldist = dist(lchild, p); double rdist = dist(rchild, p); //printf("ldist:%lf\n", ldist); //printf("rdist:%lf\n", rdist); if(ldist < rdist) { if(ldist < nearest_point.first) query_kdTree(p, lchild, depth+1); if(rdist < nearest_point.first) query_kdTree(p, rchild, depth+1); } else { if(rdist < nearest_point.first) query_kdTree(p, rchild, depth+1); if(ldist < nearest_point.first) query_kdTree(p, lchild, depth+1); } } else if(lchild != -1) { double ldist = dist(lchild, p); if(ldist < nearest_point.first) query_kdTree(p, lchild, depth+1); } else if(rchild != -1) { double rdist = dist(rchild, p); if(rdist < nearest_point.first) query_kdTree(p, rchild, depth+1); }}int main(){ int T,n; scanf("%d", &T); for(int t = 0; t < T; t++) { nearest_point.first = 9999999999999999999999999999.99999; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d%d", &point_set[i].pos[0], &point_set[i].pos[1]); } memset(kd_tree, 0, N*sizeof(point)); construct_kdTree(0, n-1, 1, 0); point tmpv; for(int i = 0; i < n; i++) { scanf("%d%d", &tmpv.pos[0], &tmpv.pos[1]); query_kdTree(tmpv, 1, 0); } printf("%.3lf\n", sqrt(nearest_point.first)); } return 0;}
值得提醒的一点是如果提交使用g++,那么最后printf的输出格式需要使用”%3.f\n”, 而非”%3.lf\n”,这是g++编译器的一个问题。
- poj 3714 Raid
- POJ 3714 raid
- POJ 3714 Raid
- POJ 3714 Raid
- poj 3714 Raid
- POJ 3714 Raid
- poj 3714 Raid 分治
- POJ 3714 Raid
- [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 计算几何
- 安卓Fragments入门
- unity Material之tilling和offset属性
- openGl代码入门笔记
- hdu 3065 病毒侵袭持续中(ac自动机)
- Ubuntu 14 配置java环境
- POJ 3714 Raid
- A*算法入门
- UNIX的各种文字游戏
- <LeetCode><Easy> 328. Odd Even Linked List
- POJ 1077 Eight(八数码第八境界|IDA*+曼哈顿距离+判断是否有解)
- 八数码的几种做法的总结以及是否有解的判断
- leetCode 226. Invert Binary Tree
- **LeetCode 60. Permutation Sequence
- (一二二)运算符重载——第十一章