hdu 3932 Groundhog Build Home

 http://acm.hdu.edu.cn/showproblem.php?pid=3932

                                          Groundhog Build Home

                 Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                           Total Submission(s): 185    Accepted Submission(s): 101

Problem Description
Groundhogs are good at digging holes, their home is a hole, usually a group of groundhogs will find a more suitable area for their activities and build their home at this area .xiaomi has grown up, can no longer live with its parents.so it needs to build its own home.xiaomi like to visit other family so much, at each visit it always start from the point of his own home.Xiaomi will visit all of the groundhogs' home in this area(it will chose the linear distance between two homes).To save energy,xiaomi would like you to help it find where its home built,so that the longest distance between xiaomi's home and the other groundhog's home is minimum.
 

 

Input
The input consists of many test cases,ending of eof.Each test case begins with a line containing three integers X, Y, N separated by space.The numbers satisfy conditions: 1 <= X,Y <=10000, 1 <= N<= 1000. Groundhogs acivity at a rectangular area ,and X, Y is the two side of this rectangle, The number N stands for the number of holes.Then exactly N lines follow, each containing two integer numbers xi and yi (0 <= xi <= X, 0 <= yi <= Y) indicating the coordinates of one home.
 

 

Output
Print exactly two lines for each test case.The first line is the coordinate of xiaomi's home which we help to find. The second line is he longest distance between xiaomi's home and the other groundhog's home.The output round to the nearest number with exactly one digit after the decimal point (0.05 rounds up to 0.1).
 

 

Sample Input
1000 50 1
10 10
1000 50 4
0 0
1 0
0 1
1 1
 
Sample Output
(10.0,10.0).
0.0
(0.5,0.5).
0.7
 題意:給定n個點的座標(在一個矩形內)求一個點到其他點的最大距離最小。
最小圓覆蓋,找了個模板稀裏糊塗的過了,有時間再去了解。
#include <stdio.h>
#include <math.h>

const int maxn = 1005;
//const double eps = 1e-6;

struct TPoint
{
	double x, y;
	TPoint operator-(TPoint &a)
	{
		TPoint p1;
		p1.x = x - a.x;
		p1.y = y - a.y;
		return p1;
	}
};

struct TCircle
{
	double r;
	TPoint centre;
};

struct TTriangle
{
	TPoint t[3];
};

TCircle c;
TPoint a[maxn];

double distance(TPoint p1, TPoint p2)
{
	TPoint p3;
	p3.x = p2.x - p1.x;
	p3.y = p2.y - p1.y;
	return sqrt(p3.x * p3.x + p3.y * p3.y);
}

double triangleArea(TTriangle t)
{
	TPoint p1, p2;
	p1 = t.t[1] - t.t[0];
	p2 = t.t[2] - t.t[0];
	return fabs(p1.x * p2.y - p1.y * p2.x) / 2;
}

TCircle circumcircleOfTriangle(TTriangle t)
{
    //三角形的外接圓
    TCircle tmp;
    double a, b, c, c1, c2;
    double xA, yA, xB, yB, xC, yC;
    a = distance(t.t[0], t.t[1]);
    b = distance(t.t[1], t.t[2]);
    c = distance(t.t[2], t.t[0]);
    //根據S = a * b * c / R / 4;求半徑R 
    tmp.r = a * b * c / triangleArea(t) / 4;
    
    xA = t.t[0].x; yA = t.t[0].y;
    xB = t.t[1].x; yB = t.t[1].y;
    xC = t.t[2].x; yC = t.t[2].y;
    c1 = (xA * xA + yA * yA - xB * xB - yB * yB) / 2;
    c2 = (xA * xA + yA * yA - xC * xC - yC * yC) / 2;
    
    tmp.centre.x = (c1 * (yA - yC) - c2 * (yA - yB)) / 
         ((xA - xB) * (yA - yC) - (xA - xC) * (yA - yB)); 
    tmp.centre.y = (c1 * (xA - xC) - c2 * (xA - xB)) / 
         ((yA - yB) * (xA - xC) - (yA - yC) * (xA - xB)); 
         
    return tmp;     
}

TCircle MinCircle2(int tce, TTriangle ce)
{
	TCircle tmp;
	if(tce == 0) tmp.r = -2;
	else if(tce == 1) 
	{
		tmp.centre = ce.t[0];
		tmp.r = 0;
	}
	else if(tce == 2)
	{
		tmp.r = distance(ce.t[0], ce.t[1]) / 2;
		tmp.centre.x = (ce.t[0].x + ce.t[1].x) / 2;
		tmp.centre.y = (ce.t[0].y + ce.t[1].y) / 2; 	
	}
	else if(tce == 3) tmp = circumcircleOfTriangle(ce);
	return tmp;
}

void MinCircle(int t, int tce, TTriangle ce)
{
	int i, j;
	TPoint tmp;
	c = MinCircle2(tce, ce);
	if(tce == 3) return;
	for(i = 1;i <= t;i++)
	{
		if(distance(a[i], c.centre) > c.r)
		{
			ce.t[tce] = a[i];
			MinCircle(i - 1, tce + 1, ce);
			tmp = a[i];
			for(j = i;j >= 2;j--)
			{
				a[j] = a[j - 1];
			}
			a[1] = tmp;
		}
	}
}

void run(int n)
{
	TTriangle ce;
	MinCircle(n, 0, ce);
	printf("(%.1lf,%.1lf).\n%.1lf\n", c.centre.x, c.centre.y, c.r);
}

int main()
{
    int X,Y,n;
	while(scanf("%d%d%d", &X,&Y,&n) != EOF)
	{
		for(int i = 1;i <= n;i++)
			scanf("%lf%lf", &a[i].x, &a[i].y);
		run(n);
	}
	return 0;
}


 

發佈了60 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章