UVA 10245 - The Closest Pair Problem
2013-06-12 11:51
351 查看
看到后面的提示,我以为直接暴力会超时,想用什么方法优化一下,但是没想出来,参考别人代码才知道优化很简单,排序,然后在遍历时,两个横坐标的差如果比当前最短距离还要大,就break;这样既可以了,另外看解题报告有更优的方法,在算法导论第二版第33章第四小节,尽快看一下。
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> using namespace std; typedef struct point { double x,y; } Point; int cmp(const void *_a,const void *_b) { Point *a=(Point *)_a; Point *b=(Point *)_b; if(a->x>b->x) return 1; else return 0; } int main() { //freopen("in.txt","r",stdin); int n; double dis; Point point[10010]; while(scanf("%d",&n)!=EOF&&n) { for(int i=0; i<n; i++) scanf("%lf%lf",&point[i].x,&point[i].y); dis=100000001; qsort(point,n,sizeof(point[0]),cmp); for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { double d=(point[j].x-point[i].x)*(point[j].x-point[i].x)+(point[j].y-point[i].y)*(point[j].y-point[i].y); if(d<dis) dis=d; if(point[j].x-point[i].x>sqrt(dis)) break; } } if(dis>100000000) printf("INFINITY\n"); else printf("%.4lf\n",sqrt(dis)); } return 0; }
相关文章推荐
- UVA_10245_ The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- uva 10245 The Closest Pair Problem
- uva 10245 - The Closest Pair Problem(暴力剪枝)
- UVA - 10245 The Closest Pair Problem
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
- UVA - 10245 The Closest Pair Problem 直接暴力(数据很水)
- UVa 10245 - The Closest Pair Problem
- [分治]UVA10245 The Closest Pair Problem
- UVA 10245 The Closest Pair Problem【分治】
- UVA 10245 The Closest Pair Problem
- UVA 10245 The Closest Pair Problem【分治】
- UVa 10245 The Closest Pair Problem (分治)
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
- UVa 10245 - The Closest Pair Problem
- UVA 10245 - The Closest Pair Problem
- UVA 10245 The Closest Pair Problem
- uva10245 The Closest Pair Problem
- Uva10245-The Closest Pair Problem
- UVA - 10245 The Closest Pair Problem