zoukankan      html  css  js  c++  java
  • 最小费马点

    以POJ的2420为例来说明一下,我感觉这个应该算得上是二分吧.先进行点的变换,确定当前最优,在二分距离,这样应该就能得出答案了吧!

    #include <iostream>
    #include<cstdio>
    #include<string.h>
    #include<cmath>
    using namespace std;
    struct point
    {
        double x,y;
        point (double a=0,double b=0){x=a;y=b;}
    }p[105];
    double work1(point a,int n)
    {
        int i;
        double ans;
        ans=0;
        for(i=0;i<n;i++)
        ans+=sqrt((a.x-p[i].x)*(a.x-p[i].x)+(a.y-p[i].y)*(a.y-p[i].y));
        return ans;
    }
    int main()
    {
        int n,i;
        double std,ans,temp;
        bool f;
        point pp,tt,R;
        while(scanf("%d",&n)==1)
        {
            std=100;
            for(i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
            pp=p[0];
            ans=work1(pp,n);
            while(std>0.1)
            {
                f=1;
                while(f)
                {
                    f=0;
                    R=point(pp.x+std,pp.y);
                    temp=work1(R,n);
                    if(ans>temp)
                    {
                        f=true;
                        tt=R;
                        ans=temp;
                    }
                    R=point(pp.x-std,pp.y);
                    temp=work1(R,n);
                    if(ans>temp)
                    {
                        f=true;
                        tt=R;
                        ans=temp;
                    }
                    R=point(pp.x,pp.y+std);
                    temp=work1(R,n);
                    if(ans>temp)
                    {
                        f=true;
                        tt=R;
                        ans=temp;
                    }
                    R=point(pp.x,pp.y-std);
                    temp=work1(R,n);
                    if(ans>temp)
                    {
                        f=true;
                        tt=R;
                        ans=temp;
                    }
                    if(f)
                     pp=tt;
                }
                std=std/2;
            }
            printf("%0.lf
    ",ans);
        }
        return 0;
    }
    


     

  • 相关阅读:
    建立文件结构
    PCL类的设计结构
    如何编写新的PCL类
    PCL推荐的命名规范(2)
    PCL推荐的命名规范(1)
    PCL中异常处理机制
    如何增加新的PointT类型
    hdoj 1728 逃离迷宫
    ny710 外星人的供给站
    ny714 Card Trick
  • 原文地址:https://www.cnblogs.com/Opaser/p/3662071.html
Copyright © 2011-2022 走看看