zoukankan      html  css  js  c++  java
  • 模拟退火求最小覆盖圆和最小覆盖球

    //最小覆盖球
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <math.h>
    #define N 150
    #define eps 1e-8
    #define T 100
    #define delta 0.98
    #define INF 1e99
     
    using namespace std;
     
    struct Point
    {
        double x, y, z;
    };
     
    Point p[N];
     
    double dist(Point A, Point B)
    {
        return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y) + (A.z - B.z) * (A.z - B.z));
    }
     
    double Search(Point p[], int n)
    {
        Point s;
        s.x=s.y=s.z=0;
        double t = 10000;
        double ans = INF;
        while(t > eps)
        {
            int k = 0;
            for(int i = 0; i < n; i++)
                if(dist(s, p[i]) > dist(s, p[k]))
                    k = i;
            double d = dist(s, p[k]);
            ans = min(ans, d);
            s.x += (p[k].x - s.x) / d * t;
            s.y += (p[k].y - s.y) / d * t;
            s.z += (p[k].z - s.z) / d * t;
            t *= delta;
        }
        return ans;
    }
     
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i = 0; i < n; i++)
                cin >> p[i].x >> p[i].y >> p[i].z;
            double ans = Search(p, n);
            printf("%.7lf
    ", ans);
        }
        return 0;
    }
    //最小覆盖圆
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <math.h>
     
    #define N 1000
    #define eps 1e-8
     
    #define delta 0.98
    #define INF 1e99
     
    using namespace std;
     
    struct Point
    {
        double x, y, z;
    };
     
    Point p[N];
     
    double dist(Point A, Point B)
    {
        return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
    }
     
    double Search(Point p[], int n)
    {
        Point s;
        s.x=s.y=s.z=0;
        double t = 1000;
        double ans = INF;
        while(t > eps)
        {
            int k = 0;
            for(int i = 0; i < n; i++)
                if(dist(s, p[i]) > dist(s, p[k]))
                    k = i;
            double d = dist(s, p[k]);
            ans = min(ans, d);
            s.x += (p[k].x - s.x) / d * t;
            s.y += (p[k].y - s.y) / d * t;
        //    s.z += (p[k].z - s.z) / d * t;
            t *= delta;
        }
        printf("%.2f %.2f %.2f
    ",s.x,s.y,ans);
    }
     
    int main()
    {
        int n;
        while(scanf("%d",&n)&&n!=0)
        {
            for(int i = 0; i < n; i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
            double ans = Search(p, n);
        }
        return 0;
    }
    rush!
  • 相关阅读:
    一条痛并快乐的路
    Daily Scrum 11.1
    Daily Scrum 10.31
    Daily Scrum 10.30
    Daily Scrum 10.29
    Daily Scrum 10.28
    Daily Scrum 10.27
    (Alpha)Let's-Chronos分数分配规则
    Daily Scrum 10.26
    Daily Scrum 10.25
  • 原文地址:https://www.cnblogs.com/LH2000/p/14977487.html
Copyright © 2011-2022 走看看