The closest point on the plane to Los Valley P1257 solution to a problem

P1257 closest point on the plane

Title Description

Distance given n points in the plane, find the pair of points therein, so that all points of the n points, the distance is the smallest of all the points.

Input Format

The first line: n; 2≤n≤10000

The next n lines: two real numbers per line: XY, indicates a row coordinate and a column coordinate point, separated by a space.

Output Format

Only one row, a real number, the shortest distance, accurate to 4 decimal places behind.

Sample input and output

Input # 1

3
1 1
1 2
2 2

Output # 1

1.0000

Description / Tips

This problem can burst search

[Thinking]

Partition + enumeration
saying I do not know why the label will be above the partition
may use divide and conquer it will run faster
but This question is entirely out of the enumeration had
had a bit difficult
but once you can use pieces For he had lost
and that is a problem of flood

However, there are two noteworthy places of

min and max can only compare two identical typed variables
is no way to compare two types of variables without defining
such as he can compare the two types of double size, you can also compare two type int size
but once comparing a double int type and a size of a compile error
so you need to define the minimum value Min here into double type
as the distance between two points if required by the formula cmath library inside sqrt words
that return value is double type of
float is not OK Oh!
When the output with a precision and,
of course, it is better double

Another notable problem is that it root formula between two points
the distance between two points is equal to \ (\ sqrt {(x_1 - x_2) ^ 2 + (y_1 - y_2) ^ 2} \)

[Complete code]

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

using namespace std;
const int Max = 10005;
struct node
{
    int x,y;
}a[Max];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;++ i)
        scanf("%d%d",&a[i].x,&a[i].y);
    double Min = 0x7fffffff;
    for(int i = 1;i <= n;++ i)
        for(int j = i + 1;j <= n;++ j)
            Min = min(Min,sqrt((a[i].x -a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y)));
    printf("%.4lf\n",Min);
    return 0;
}

Guess you like

Origin www.cnblogs.com/acioi/p/11616768.html