hdu 3932 Groundhog Build Home

最小覆盖圆 || 模拟退火均可。


之前模拟退火弄得不太清楚,今天好好学了下别人的= = 。。。发现我的退火那么水那么水 = =。。。

不过这个如果掌握不好步长的话,还是会死很惨。

时间差别很大,最小覆盖圆0ms,退火好几百ms。。。

最小覆盖圆:

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")

using namespace std;

const double eps = 1e-6;
const int MAX = 1010;
struct point{double x,y;};
point p[MAX];
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
double disp2p(point a,point b) //  a b 两点之间的距离 
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
point circumcenter(point a,point b,point c)
{
 	point ret;
 	double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1*a1 + b1*b1)/2; 
  	double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2*a2 + b2*b2)/2; 
  	double d = a1 * b2 - a2 * b1; 
  	ret.x = a.x + (c1*b2 - c2*b1)/d; 
  	ret.y = a.y + (a1*c2 - a2*c1)/d; 
 return ret;
}
void min_cover_circle(point p[],int n,point &c,double &r)
{
	random_shuffle(p,p+n);// #include <algorithm>
	c = p[0]; r = 0;
	for(int i=1; i<n; i++)
		if( dy(disp2p(p[i],c),r) )
		{
			c = p[i]; r = 0;
			for(int k=0; k<i; k++)
				if( dy(disp2p(p[k],c),r) )
				{
					c.x = (p[i].x + p[k].x)/2;
					c.y = (p[i].y + p[k].y)/2;
					r = disp2p(p[k],c);
					for(int j=0; j<k; j++)
						if( dy(disp2p(p[j],c),r) )
						{							// 求外接圆圆心,三点必不共线 
							c = circumcenter(p[i],p[k],p[j]);
							r = disp2p(p[i],c);
						}
				}
		}
}

int main()
{
	int ncases,n;
	
	double x,y;
	while( ~scanf("%lf%lf%d",&x,&y,&n) )
	{
		for(int i=0; i<n; i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		double r;
		point c;
		min_cover_circle(p,n,c,r);
		printf("(%.1lf,%.1lf).\n",c.x,c.y);
		printf("%.1lf\n",r);
	}

return 0;
}

模拟退火:

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <time.h>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")

using namespace std;

const int MAX = 1010;
const double inf = 1e30;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int N = 10;
const int L = 40;
bool dy(double x,double y)    {    return x > y + eps;}    // x > y 
bool xy(double x,double y)    {    return x < y - eps;}    // x < y 
bool dyd(double x,double y)    {     return x > y - eps;}    // x >= y 
bool xyd(double x,double y)    {    return x < y + eps;}     // x <= y 
bool dd(double x,double y)     {    return fabs( x - y ) < eps;}  // x == y
struct point { double x,y;
    point (double x,double y):x(x),y(y){}
    point ():x(0),y(0){}
};
double disp2p(point a,point b) //  a b 两点之间的距离 
{
    return sqrt(( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ));
}
point p[MAX];
point rp[MAX];
double len[MAX];
double min_dis(point a,point *p,int n)
{
    double min = 0;
    for(int i=0; i<n; i++)
    {
        double len = disp2p(a,p[i]);
        if( dy(len,min) )
            min = len;
    }
    return min;
}
bool check(point a,double x,double y)
{
    return dyd(a.x,0.0) && dyd(a.y,0) && xyd(a.x,x) && xyd(a.y,y);
}
point Rand(double x,double y)
{
	point c;
	c.x = ( rand()%1000 + 1 ) / 1000.0 * x;
	c.y = ( rand()%1000 + 1 ) / 1000.0 * y;
	return c;
}
int main()
{
    int n,m,ncases;
	double x,y;
	srand(time(NULL));
	while( ~scanf("%lf%lf%d",&x,&y,&n) )
	{
        for(int i=0; i<n; i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        for(int i=0; i<N; i++)
        {
			rp[i] = Rand(x,y);
			len[i] = min_dis(rp[i],p,n);
		}
        point st;
        double step = max(x,y)/sqrt((double)n);
        while( step > 0.001 )
        {
            for(int k=0; k<N; k++)
            {
				st = rp[k];
                for(int i=0; i<L; i++)
                {
					double ang = (rand()%1000+1)/1000.0*10*pi;
					double xx = st.x + step*cos(ang);
					double yy = st.y + step*sin(ang);	
                    point t = point(xx,yy);
                    if( !check(t,x,y) ) continue;
                    double dis = min_dis(t,p,n);
                    if( xy(dis,len[k]) )
                    {
                        rp[k] = t;
                        len[k] = dis;
                    }
                }
			}
            step *= 0.8;
        }
        int ind = 0;
        for(int i=1; i<N; i++)
        	if( len[i] < len[ind] )
        		ind = i;
		printf("(%.1lf,%.1lf).\n",rp[ind].x,rp[ind].y);
		printf("%.1lf\n",len[ind]);
    }

return 0;
}


你可能感兴趣的:(c,struct,null,Random,Build)