24h購物| | PChome| 登入
2012-01-26 21:45:11| 人氣4,276| 回應0 | 上一篇 | 下一篇

[UVA] 10245 - The Closest Pair Problem

推薦 0 收藏 0 轉貼0 訂閱站台

Problem J
The Closest Pair Problem
Input: standard input
Output: standard output
Time Limit: 8 seconds
Memory Limit: 32 MB

 

Given a set of points in a two dimensional space, you will have to find the distance between the closest two points.

 

Input

 

The input file contains several sets of input. Each set of input starts with an integer N (0<=N<=10000), which denotes the number of points in this set. The next N line contains the coordinates of N two-dimensional points. The first of the two numbers denotes the X-coordinate and the latter denotes the Y-coordinate. The input is terminated by a set whose N=0. This set should not be processed. The value of the coordinates will be less than 40000 and non-negative.

 

Output

 

For each set of input produce a single line of output containing a floating point number (with four digits after the decimal point) which denotes the distance between the closest two points. If there is no such two points in the input whose distance is less than 10000, print the line INFINITY.

 

Sample Input

3
0 0
10000 10000
20000 20000
5
0 2
6 67
43 71
39 107
189 140
0

 

Sample Output

INFINITY
36.2215



作法 : D&C
雖然說是 O(nlogn)
但是做起來感覺不止, 這個係數還真的頗大的, 這題 N^2 也會過, 也不多說什麼

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define oo 100000000
typedef struct {
    double x, y;
}Point;
Point D[10000];
int cmp(const void *i, const void *j) {
    Point *x, *y;
    x = (Point *)i, y = (Point *)j;
    if(x->x != y->x)    
        return x->x - y->x < 0 ? 1 : -1;
    return x->y - y->y < 0 ? 1 : -1;
}
double Min(double x, double y) {
    return x < y ? x : y;
}
double Merge(int l, int m, int r, double d) {
    double midL = D[m].x;
    int i, j;
    for(i = m; i >= l; i--) {
        if((D[i].x-midL)*(D[i].x-midL) >= d)    break;
        for(j = m+1; j <= r; j++) {
            if((D[i].x-D[j].x)*(D[i].x-D[j].x) >= d)    break;
            d = Min(d, (D[i].x-D[j].x)*(D[i].x-D[j].x)+(D[i].y-D[j].y)*(D[i].y-D[j].y));
        }        
    }
    return d;
}
double ClosestPair(int l, int r, double *dis) {
    if(l < r) {
        if(l == r-1) {
            return (D[l].x-D[r].x)*(D[l].x-D[r].x) + (D[l].y-D[r].y)*(D[l].y-D[r].y);
        }
        int m = (l+r)>>1;
        *dis = Min(*dis, ClosestPair(l, m, dis));
        *dis = Min(*dis, ClosestPair(m+1, r, dis));
        *dis = Min(*dis, Merge(l, m, r, *dis));
        return *dis;
    }
    return oo;
}
int main() {
    int N, i;
    while(scanf("%d", &N) == 1 && N) {
        for(i = 0; i < N; i++)
            scanf("%lf %lf", &D[i].x, &D[i].y);
        qsort(D, N, sizeof(Point), cmp);
        double distance = oo;
        distance = Min(distance, ClosestPair(0, N-1, &distance));
        distance = sqrt(distance);
        if(distance >= 10000)
            puts("INFINITY");
        else
            printf("%.4lf\n", distance);
    }
    return 0;
}

台長: Morris
人氣(4,276) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 793 - Network Connections
此分類上一篇:[UVA] 673 - Parentheses Balance

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文