POJ 2069 Super Start (最小球覆盖,退火算法)

POJ 2069 Super Start (最小球覆盖,退火算法)

退火法

主要参考这篇文章

Description

During a voyage of the starship Hakodate-maru (see Problem 1406), researchers found strange synchronized movements of stars. Having heard these observations, Dr. Extreme proposed a theory of “super stars”. Do not take this term as a description of actors or singers. It is a revolutionary theory in astronomy.
According to this theory, starts we are observing are not independent objects, but only small portions of larger objects called super stars. A super star is filled with invisible (or transparent) material, and only a number of points inside or on its surface shine. These points are observed as stars by us.

In order to verify this theory, Dr. Extreme wants to build motion equations of super stars and to compare the solutions of these equations with observed movements of stars. As the first step, he assumes that a super star is sphere-shaped, and has the smallest possible radius such that the sphere contains all given stars in or on it. This assumption makes it possible to estimate the volume of a super star, and thus its mass (the density of the invisible material is known).

You are asked to help Dr. Extreme by writing a program which, given the locations of a number of stars, finds the smallest sphere containing all of them in or on it. In this computation, you should ignore the sizes of stars. In other words, a star should be regarded as a point. You may assume the universe is a Euclidean space.
Input

The input consists of multiple data sets. Each data set is given in the following format.

n
x1 y1 z1
x2 y2 z2
. . .
xn yn zn

The first line of a data set contains an integer n, which is the number of points. It satisfies the condition 4 <= n <= 30.

The location of n points are given by three-dimensional orthogonal coordinates: (xi, yi, zi) (i = 1, …, n). Three coordinates of a point appear in a line, separated by a space character. Each value is given by a decimal fraction, and is between 0.0 and 100.0 (both ends inclusive). Points are at least 0.01 distant from each other.

The end of the input is indicated by a line containing a zero.
Output

For each data set, the radius of the smallest sphere containing all given points should be printed, each in a separate line. The printed values should have 5 digits after the decimal point. They may not have an error greater than 0.00001.
Sample Input

4
10.00000 10.00000 10.00000
20.00000 10.00000 10.00000
20.00000 20.00000 10.00000
10.00000 20.00000 10.00000
4
10.00000 10.00000 10.00000
10.00000 50.00000 50.00000
50.00000 10.00000 50.00000
50.00000 50.00000 10.00000
0
Sample Output

7.07107
34.64102
Source

Japan 2001
题目的大概意思就是说,给你几个空间中的点,要找到最小 包含所有的点 的球体半径。
思路就是这么个思路:先找到最远的,然后慢慢变小,找到合适的点。
最开始我的思路是硬算,看了一下数据,30,能过。然后要去解方程,列算式,有点麻烦。这个思路类似这样
后来百度看了别人的算法。

//
//  main.cpp
//  Problem
//
//  Copyright © 2020 Kylin. All rights reserved.

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <bitset>
#include <set>
/*
#include <unistd.h>     //包含<unistd.h>
#include <sys/types.h>  //包含<sys/types.h>
 */

#pragma GCC optimize("Ofast")


using namespace std;
#define ll  long long
#define lowbit(x) (x)&(-x)
#define print_str(a) do{for(int i=0;i<strlen(a);i++)printf("%c",a[i]);printf("\n");}while(0)
#define ms(a,b) memset(a,b,sizeof a)
#define INF 0x3f3f3f3f

const int N = 50;
const double eps = 1e-7;
int n;
struct P{
    double x,y,z;
}a[N],z;
double ans;

double dis(P a,P 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));
}
void solve()
{
    double step=100,mt;
    z.x=z.y=z.z=0;
    int s=0;
    while(step>eps)
    {
        for(int i=0; i<n; i++)
            if(dis(z,a[s])<dis(z,a[i])) s=i;
        mt=dis(z,a[s]);
        ans=min(ans,mt);
        z.x+=(a[s].x-z.x)/mt*step;
        z.y+=(a[s].y-z.y)/mt*step;
        z.z+=(a[s].z-z.z)/mt*step;
        step*=0.98;
    }
}

int main(){
    while(scanf("%d",&n)&&n){
        ans = INF;
        for(int i=0;i<n;i++)scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z);
        solve();
        printf("%.5lf\n",ans);
    }
   
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
贪心算法最小区间覆盖是指给定一个大区间和多个小区间,要求找到最少的小区间来完全覆盖大区间的问题。其中,每个小区间都有一个开始时间和结束时间。贪心算法的思路是按照开始时间递增排序,然后从第一个小区间开始,选择与当前区间结束时间不重叠且结束时间最早的下一个区间,直到覆盖了整个大区间为止。 具体的解法可以按照以下步骤进行: 1. 将所有小区间按照开始时间递增排序,如果开始时间相同,则按照结束时间递增排序。 2. 初始化一个变量count,用于记录覆盖大区间所需要的小区间的数量。 3. 设定一个变量end,表示当前选择的小区间的结束时间。 4. 遍历排序后的小区间列表,对于每个小区间: - 如果小区间的开始时间大于end,则说明当前小区间与之前选择的小区间不重叠,可以选择该小区间来覆盖大区间。 将该小区间的结束时间赋值给end,同时将count加1。 - 如果小区间的开始时间小于等于end,说明当前小区间与之前选择的小区间重叠,需要进行下一次遍历。 5. 如果end大于大区间的结束时间,则表示已经找到了能够完全覆盖大区间的最小区间集合,返回count的值;否则,返回-1,表示无法找到满足条件的最小区间覆盖。 这个问题可以使用贪心算法来解决,通过排序和遍历的方式选择合适的小区间来覆盖大区间,以达到最小覆盖的目标。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [贪心算法.doc](https://download.csdn.net/download/hqztrue2/11338380)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [贪心算法 --- 最小区间覆盖问题(POJ2376)](https://blog.csdn.net/WSSB____/article/details/126682938)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [贪心算法——最小区间覆盖问题](https://blog.csdn.net/mashizuren/article/details/113345347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值