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!
  • 相关阅读:
    浏览器 页面报错
    TP单字母函数
    TP系统常量、项目后台的搭建、模板中常量字符串替换
    ThinkPHP3.2的简单知识
    面向对象基本概念,关键字,类的组成,静态,三大特性,创建类和对象,设计模式
    layui(弹出层)
    头像文件上传 方法一:from表单 方法二:ajax
    上传文件
    流程增加,发起事件,流程显示,处理。
    分页,条件查找后再分页
  • 原文地址:https://www.cnblogs.com/LH2000/p/14977487.html
Copyright © 2011-2022 走看看