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.
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.
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
2 -1 0 5 3 0 2 5 2
6
4 0 0 5 0 9 4 8 3 -1 0 1 4
33
The first sample is (r12 = 5, r22 = 1):The second sample is (r12 = 1, r22 = 32):
O(n^2)枚举每一个花朵在第一个喷泉的距离,第一个喷泉距离比它小的 就不用管了。第一个喷泉距离比它大的,那就一定在第二个喷泉范围里面,求最大值。
枚举第一个距离的每一种情况,求最小值。(其中有可能每一个花朵都在第二个喷泉里,这种情况要考虑到。)
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
#define INF ((1LL<<63)-1)
typedef long long ll;
const int maxn = 2005;
ll n, xx1, xx2, yy1, yy2;
ll flo_x[maxn], flo_y[maxn];
struct no
{
ll d1;
ll d2;
}node[maxn];
void solve()
{
int i, j;
scanf("%I64d%I64d%I64d%I64d%I64d", &n, &xx1, &yy1, &xx2, &yy2);
for (i = 1; i <= n; i++)
{
scanf("%I64d%I64d", &flo_x[i], &flo_y[i]);
node[i].d1 = (flo_x[i] - xx1)*(flo_x[i] - xx1) + (flo_y[i] - yy1)*(flo_y[i] - yy1);
node[i].d2 = (flo_x[i] - xx2)*(flo_x[i] - xx2) + (flo_y[i] - yy2)*(flo_y[i] - yy2);
}
ll ans = INF;
for (i = 1; i <= n + 1; i++)//这里是n+1,因为可能都在第二个圆圈里面。
{
ll r2 = 0;
for (j = 1; j <= n; j++)
{
if (node[j].d1 > node[i].d1)//盖不住这个元素
{
r2 = max(r2, node[j].d2);
}
}
ans = min(ans, node[i].d1 + r2);
}
printf("%I64d", ans);
}
int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
solve();
return 0;
}