UVA10245 - The Closest Pair Problem

题意:找出距离最近的两个点

思路:直接暴力搜索,但要剪枝才能过,不然会超时

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

struct node{
    double x, y;
}n[10005];

int cmp(node a, node b) {
    return a.x < b.x;
}

int main() {
    int t;
    while (scanf("%d", &t) && t) {
        for (int i = 0; i < t; i++) 
            scanf("%lf %lf", &n[i].x, &n[i].y);
        sort(n, n + t, cmp);
        double min = 10000;
        for (int i = 0; i < t; i++) 
            for (int j = i + 1; j < t; j++) {
                if (fabs(n[i].x - n[j].x) >= min) 
                    break; 
                double x = (n[i].x - n[j].x) * (n[i].x - n[j].x);
                double y = (n[i].y - n[j].y) * (n[i].y - n[j].y);
                double z = sqrt(x + y);
                if (min > z) 
                    min = z;
            }
        if (min >= 10000)
            printf("INFINITY\n");
        else 
            printf("%.4lf\n", min);
    }
    return 0;
}


你可能感兴趣的:(UVA10245 - The Closest Pair Problem)