码迷,mamicode.com
首页 > 其他好文 > 详细

hdu2215(最小覆盖圆)

时间:2014-08-09 15:33:48      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

Maple trees

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1578    Accepted Submission(s): 488


Problem Description
There are a lot of trees in HDU. Kiki want to surround all the trees with the minimal required length of the rope . As follow,
bubuko.com,布布扣

To make this problem more simple, consider all the trees are circles in a plate. The diameter of all the trees are the same (the diameter of a tree is 1 unit). Kiki can calculate the minimal length of the rope , because it‘s so easy for this smart girl.
But we don‘t have a rope to surround the trees. Instead, we only have some circle rings of different radius. Now I want to know the minimal required radius of the circle ring. And I don‘t want to ask her this problem, because she is busy preparing for the examination.
As a smart ACMer, can you help me ?
bubuko.com,布布扣
 

 

Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set n (1 <= n <= 100), it is followed by n coordinates of the trees. Each coordinate is a pair of integers, and each integer is in [-1000, 1000], it means the position of a tree’s center. Each pair is separated by blank.
Zero at line for number of trees terminates the input for your program.
 

 

Output
Minimal required radius of the circle ring I have to choose. The precision should be 10^-2.
 

 

Sample Input
2
1 0
-1 0
0
 

 

Sample Output
1.50
 
题意:用一个最小的圆把所有的点都圈在里边,点可以在圆上,每个点的半径为0.50
bubuko.com,布布扣
  1 #include<stdio.h>
  2 #include<math.h>
  3 #define PI acos(-1.0)
  4 struct   TPoint
  5 {
  6     double x,y;
  7 }a[1005],d;
  8 double r;
  9 double   distance(TPoint   p1,   TPoint   p2)
 10 {
 11     return (sqrt((p1.x-p2.x)*(p1.x -p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
 12 }
 13 double multiply(TPoint   p1,   TPoint   p2,   TPoint   p0)
 14 {
 15     return   ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
 16 }
 17 void MiniDiscWith2Point(TPoint   p,TPoint   q,int   n)
 18 {
 19     d.x=(p.x+q.x)/2.0;
 20     d.y=(p.y+q.y)/2.0;
 21     r=distance(p,q)/2;
 22     int k;
 23     double c1,c2,t1,t2,t3;
 24     for(k=1; k<=n; k++)
 25     {
 26         if(distance(d,a[k])<=r)
 27             continue;
 28         if(multiply(p,q,a[k])!=0.0)
 29         {
 30             c1=(p.x*p.x+p.y*p.y-q.x*q.x-q.y*q.y)/2.0;
 31             c2=(p.x*p.x+p.y*p.y-a[k].x*a[k].x-a[k].y*a[k].y)/2.0;
 32 
 33             d.x=(c1*(p.y-a[k].y)-c2*(p.y-q.y))/((p.x-q.x)*(p.y-a[k].y)-(p.x-a[k].x)*(p.y-q.y));
 34             d.y=(c1*(p.x-a[k].x)-c2*(p.x-q.x))/((p.y-q.y)*(p.x-a[k].x)-(p.y-a[k].y)*(p.x-q.x));
 35             r=distance(d,a[k]);
 36         }
 37         else
 38         {
 39             t1=distance(p,q);
 40             t2=distance(q,a[k]);
 41             t3=distance(p,a[k]);
 42             if(t1>=t2&&t1>=t3)
 43             {
 44                 d.x=(p.x+q.x)/2.0;
 45                 d.y=(p.y+q.y)/2.0;
 46                 r=distance(p,q)/2.0;
 47             }
 48             else if(t2>=t1&&t2>=t3)
 49             {
 50                 d.x=(a[k].x+q.x)/2.0;
 51                 d.y=(a[k].y+q.y)/2.0;
 52                 r=distance(a[k],q)/2.0;
 53             }
 54             else
 55             {
 56                 d.x=(a[k].x+p.x)/2.0;
 57                 d.y=(a[k].y+p.y)/2.0;
 58                 r=distance(a[k],p)/2.0;
 59             }
 60         }
 61     }
 62 }
 63 
 64 void MiniDiscWithPoint(TPoint   pi,int   n)
 65 {
 66     d.x=(pi.x+a[1].x)/2.0;
 67     d.y=(pi.y+a[1].y)/2.0;
 68     r=distance(pi,a[1])/2.0;
 69     int j;
 70     for(j=2; j<=n; j++)
 71     {
 72         if(distance(d,a[j])<=r)
 73             continue;
 74         else
 75         {
 76             MiniDiscWith2Point(pi,a[j],j-1);
 77         }
 78     }
 79 }
 80 int main()
 81 {
 82     int i,n;
 83     while(scanf("%d",&n)&&n)
 84     {
 85         for(i=1; i<=n; i++)
 86             scanf("%lf %lf",&a[i].x,&a[i].y);
 87         if(n==1)
 88         {
 89             printf("0.50\n");
 90             continue;
 91         }
 92 
 93         r=distance(a[1],a[2])/2.0;
 94         d.x=(a[1].x+a[2].x)/2.0;
 95         d.y=(a[1].y+a[2].y)/2.0;
 96         for(i=3; i<=n; i++)
 97         {
 98             if(distance(d,a[i])<=r)
 99                 continue;
100             else
101                 MiniDiscWithPoint(a[i],i-1);
102         }
103         printf("%.2lf\n",r+0.5);
104     }
105     return 0;
106 }
View Code

 

hdu2215(最小覆盖圆),布布扣,bubuko.com

hdu2215(最小覆盖圆)

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/lxm940130740/p/3900872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!