cf340 C. Watering Flowers

C. Watering Flowers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A flowerbed has many flowers and two fountains.

You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1, or the distance to the second fountain doesn't exceed r2. It's OK if some flowers are watered by both fountains.

You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and the r12 + r22 is minimum possible. Find this minimum value.

Input

The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000,  - 107 ≤ x1, y1, x2, y2 ≤ 107) — the number of flowers, the coordinates of the first and the second fountain.

Next follow n lines. The i-th of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower.

It is guaranteed that all n + 2 points in the input are distinct.

Output

Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.

Sample test(s)
Input
2 -1 0 5 3
0 2
5 2
Output
6
Input
4 0 0 5 0
9 4
8 3
-1 0
1 4
Output
33
Note

The first sample is (r12 = 5, r22 = 1): The second sample is (r12 = 1, r22 = 32):

 

思路:直接枚举就可以了,r1可以是0或者是到其他任意一个点的距离,然后就枚举r2,条件是到1的距离大于r1中最大,

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const long long INF = 10e18;
const int MAX = 2000 + 10;
long long Distance1[MAX],Distance2[MAX];
long long get_distance(long long x1,long long y1,long long x2, long long y2)
{
    return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
}
int main()
{
    int n;
    long long x1,x2,y1,y2,x,y;
    long long r1 = 0,r2 = 0;
    long long ans = INF;
    scanf("%d%I64d%I64d%I64d%I64d", &n,&x1,&y1,&x2,&y2);
    Distance1[0] = Distance2[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        scanf("%I64d%I64d",&x,&y);
        Distance1[i] = get_distance(x1,y1,x,y);
        Distance2[i] = get_distance(x2,y2,x,y);
    }

    for(int i = 0; i <= n; i++)
    {
        r1 = Distance1[i];
        r2 = 0;
        for(int j = 1; j <= n; j++)
        {
            if(Distance1[j] > r1 && Distance2[j] > r2)
            {
                r2 = Distance2[j];
            }
        }
        ans = min(ans, r1 + r2);
    }

    printf("%I64d\n",ans);
    return 0;
}
View Code

 

posted @ 2016-01-24 15:48  zhaop  阅读(301)  评论(0编辑  收藏  举报