POJ 3714 Raid
2016-01-23 01:41
441 查看
Raid
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 9636 Accepted: 2941
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
Source
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 100000 short 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 ; point kd_tree ; 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++编译器的一个问题。
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题