HDU 4145: Cover The Enemy

来源:互联网 发布:彩虹源码安装 编辑:程序博客网 时间:2024/05/19 03:23

题目链接 : HDU 4145    BUCT Coer 2082



                                         Cover The Enemy

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


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
 

Author
mark1989@BJTU
 

Source
2011百校联动“菜鸟杯”程序设计公开赛


    本题应用穷举的方法计算出每种情况,选择最优解。

    将所有敌人距离A的距离从大到小排序,然后枚举出A的半径,则B的半径就是A无法覆盖的点中距离B最远的距离。然后用一个变量维护A和B距离的最小值即可。



#define LOCAL#include<iostream>#include<algorithm>using namespace std;#define MAX 1000000int t,x[2],y[2];int n;struct Enemy{int x,y,da,db;};Enemy En[MAX];int Dist(int m,int n,int Flag)//点(m,n)距离A(Flag=0)或B(Flag=1)的距离的平方,即cost值{return ((m-x[Flag])*(m-x[Flag]) + (n-y[Flag])*(n-y[Flag]));}bool comp(Enemy a,Enemy b){return a.da<b.da;}int main(){#ifdef LOCALfreopen("test.txt","r",stdin);freopen("tested.txt","w",stdout);        #endifcin>>t;while(t--){cin>>x[0]>>y[0]>>x[1]>>y[1];cin>>n;for(int i=0;i<n;i++){cin>>En[i].x>>En[i].y;En[i].da=Dist(En[i].x,En[i].y,0);//点到A的costEn[i].db=Dist(En[i].x,En[i].y,1);//点到B的cost}sort(En,En+n,comp);int ans=En[n-1].da;//只用Aint db_MAX=0;//db_MAX表示B的半径,A不覆盖的点中距离B最远的点的cost值for(int i=n-2;i>=0;i--){if(En[i+1].db>db_MAX) db_MAX=En[i+1].db;//维护db_MAXans=ans > En[i].da+db_MAX ? En[i].da+db_MAX : ans;//维护ans值}if(En[0].db > db_MAX) db_MAX=En[0].db;//不用Aif(db_MAX < ans) ans=db_MAX;cout<<ans<<endl;}return 0;}

0 0