hdu 4145 Cover The Enemy

来源:互联网 发布:淘宝男装销量店铺排行 编辑:程序博客网 时间:2024/09/21 09:25

Cover The Enemy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1165    Accepted Submission(s): 293


Problem Description

 

  Now, the war is coming, no one want to fight with others, but you have to when you are facing.
  As the king of a country, you should defend your country. The condition is that “There are N enemy armies around you, you have two towers and you want to build cannon on each tower to cover all the enemy armies”. The cannon has an “attack distance” R with a cost of R^2.
  You are facing a problem, to cover all the armies and make the cost R1^2 + R2^2 minimal, where R1 and R2 are the attack distance of the two towers.
 


 

Input

 

The first line contains an integer t, number of test cases, following with t groups of test cases described as following.
For each case, the first line contains four integers: x1, y1, x2, y2, the Coordinates of the two towers.
The next line is an integer n, the number of enemy armies, following with n lines of coordinates: x, y, which is the coordinate of a group of enemy army, where:
  t <= 100
1 <= n <= 100000, 
-1000 <= x, y <= 1000 
-1000 <= x1, y1, x2, y2 <= 1000
 


 

Output

 

The minimal cost of the two tower to cover all the enemy armies.
 


 

Sample Input

 

20 0 10 02-3 310 00 0 6 05-4 -2-2 34 06 -29 1
 


 

Sample Output

 

1830
 

计算出每个点到两个塔x1,x2的距离,然后,把按照x1的距离从大到小排序。r1先取距x1最远的点的平方,点被完全覆盖。

然后,将r1依次递减,把r1不能覆盖的点让r2覆盖。这样,这些点始终都被完全覆盖着,ans取r1+r2的最小值。

 

#include<stdio.h>#include<string.h>#include<math.h>#include"stdlib.h"#define N 100005struct node{int x,y;int d1,d2;       //记录平方和}f[N];int dis(int x1,int y1,int x2,int y2){int x=x1-x2,y=y1-y2;return x*x+y*y;}int cmp(const void*a,const void*b){return (*(struct node*)a).d1>(*(struct node*)b).d1?-1:1;}int min(int a,int b){return a<b?a:b;}int main(){int T,i,n,r1,r2,ans;int x1,x2,y1,y2;scanf("%d",&T);while(T--){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);scanf("%d",&n);for(i=0;i<n;i++){scanf("%d%d",&f[i].x,&f[i].y);f[i].d1=dis(x1,y1,f[i].x,f[i].y);f[i].d2=dis(x2,y2,f[i].x,f[i].y);}qsort(f,n,sizeof(f[0]),cmp);ans=f[0].d1;              //r1取最大值,r2=0;r2=f[0].d2;for(i=1;i<n;i++){r1=f[i].d1;ans=min(ans,r1+r2);if(r2<f[i].d2)r2=f[i].d2;}ans=min(ans,r2);        //r2取最大值,r1=0即可。printf("%d\n",ans);}return 0;}


 

1 0
原创粉丝点击