uva 10245 The Closest Pair Problem
2013-09-03 12:21
295 查看
题意:求最短距离的一对点的距离,枚举每种情况就行了,可以优化的地方是我们按X的坐标从小到大排序,一旦出现a.x-b.x的绝对值大于Min的话,那么接下来的就不用再计算了
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 10005; struct point { double x,y; }arr[MAXN]; int cmp(point a,point b) { return a.x < b.x; } int main() { int n; while (scanf("%d",&n) != EOF && n) { double Min = 10000; for (int i = 0; i < n; i++) scanf("%lf %lf",&arr[i].x,&arr[i].y); sort(arr,arr+n,cmp); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { double dis1 = arr[i].x - arr[j].x; double dis2 = arr[i].y - arr[j].y; double dis = sqrt(dis1*dis1 + dis2*dis2); if (abs(arr[j].x - arr[i].x) >= Min) break; if (dis < Min) Min = dis; } } if (Min >= 10000) printf("INFINITY\n"); else printf("%.4lf\n",Min); } return 0; }
相关文章推荐
- uva 10245 The Closest Pair Problem (暴力+剪枝)
- UVa 10245 The Closest Pair Problem 分治
- UVa 10245 - The Closest Pair Problem
- The Closest Pair Problem UVA - 10245(暴力)
- 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
- Uva10245-The Closest Pair Problem
- uva-10245-The Closest Pair Problem-分治算法
- The Closest Pair Problem(UVa 10245) 计算几何+最近点对问题
- UVA-10245 The Closest Pair Problem(最近点对问题)
- uva 10245 The Closest Pair Problem
- UVA 10245 - The Closest Pair Problem
- UVa Problem 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