zoukankan      html  css  js  c++  java
  • 最小圆覆盖

    求一个半径最小的圆使其内部至少包含 m 个点

    #include <bits/stdc++.h>
    using namespace std;
     
    #define Mn 305
    const double eps = 1e-7;
    const double pi = acos(-1.0);
    struct Point
    {
    	double x,y;
    	Point() {}
    	Point(double tx,double ty)
    	{
    		x=tx;
    		y=ty;
    	}
    } p[Mn+5];
    struct Node
    {
    	double angle;
    	bool in;
    } arc[Mn * Mn + 5];
    int n;
    double dist(Point p1,Point p2)
    {
    	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
    }
    bool cmp(Node &n1,Node &n2)
    {
    	return n1.angle<n2.angle;
    }
    double R;
     
    int MaxCircleCover()
    {
        int ans=1;
        for(int i=0;i<n;i++)
    	{
            int cnt=0;
            for(int j=0;j<n;j++)
    		{
                if(i==j) continue;
                double D = dist(p[i],p[j]);
                if(D>2.0*R) continue;
                
                double angle=atan2(p[i].y-p[j].y,p[i].x-p[j].x);
                if(angle < 0)
                    angle += 2 * pi;
                    
                double phi=acos(D/(2.0*R));
                arc[cnt].angle=angle-phi+ 2 * pi;arc[cnt++].in=true;
                arc[cnt].angle=angle+phi+ 2 * pi;arc[cnt++].in=false;
            }
            sort(arc,arc+cnt,cmp);
            int tmp=1;
            for(int j=0;j<cnt;j++)
    		{
                if(arc[j].in)  tmp++;
                else tmp--;
                ans=max(ans,tmp);
            }
        }
        return ans;
    }
     
    int main()
    {
    	int t;scanf("%d",&t);
    	while(t--)
    	{
    		int s;
    		scanf("%d%d",&n,&s);
    		for(int i=0;i<n;i++)
    			scanf("%lf%lf",&p[i].x,&p[i].y);
    		double r;
    		scanf("%lf",&r);
    		double l1=0,r1=100000;	
    		while(abs(r1-l1)>eps)
    		{
    			double mid=(l1+r1)*0.5;
    			R=mid;
    			int p=MaxCircleCover();
    			if(p>=s)
    			{
    				r1=mid;
    			}
    			else
    			{
    				l1=mid;
    			}
    		}
    		printf("%.4lf
    ",l1+r);
    	}
    	return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    adt 下载有时候下载不下来
    phonegap 2.5.0 创建项目
    jquerymobile tap事件被触发两次。
    phonegap Resource ID #0x0
    淘宝客淘宝开放平台要UTF编码才能获取数据
    js document.addEventListener 注册事件,
    jquerymobile 转场之后不执行onload事件
    我的第一篇博客
    心情
    箭头css
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9643881.html
Copyright © 2011-2022 走看看