Minimum sphere coverage in poj2069, simulated annealing

Source: Internet
Author: User

I just set up a template. I really don't understand the specific process of simulated annealing.

 1 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler 2 #include <stdio.h> 3 #include <iostream> 4 #include <cstring> 5 #include <cmath> 6 #include <stack> 7 #include <queue> 8 #include <vector> 9 #include <algorithm>10 #define ll long long11 #define Max(a,b) (((a) > (b)) ? (a) : (b))12 #define Min(a,b) (((a) < (b)) ? (a) : (b))13 #define Abs(x) (((x) > 0) ? (x) : (-(x)))14 using namespace std;15 16 const int INF = 0x3f3f3f3f;17 const int MAXN = 220;18 const double eps = 1e-8;19 int n;20 struct POINT{21     double x,y,z;22 }ps[35],q;23 24 double dist(POINT a,POINT b){25     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));26 }27 28 int maxD(POINT p){29     double res = 0;30     int i, k = 0;31     for(i = 0; i < n ; ++i){32         double tmp = dist(p,ps[i]);33         if(tmp > res){34             k = i;35             res = dist(p,ps[i]);36         }37     }38     return k;39 }40 41 double BallCover(){42     double step = 100, ans = INF;43     q.x = q.y = q.z = 0.0;44     while(step > eps){45         int d = maxD(q);46         double tmp = dist(ps[d],q);47         if(tmp < ans) ans=tmp;48         double dx = ps[d].x - q.x;49         double dy = ps[d].y - q.y;50         double dz = ps[d].z - q.z;51         dx /= tmp;52         dy /= tmp;53         dz /= tmp;54         q.x += (dx*step);55         q.y += (dy*step);56         q.z += (dz*step);57         step *= 0.98;58     }59     return ans;60 }61 62 int main(){63     int i;64     while(EOF != scanf("%d",&n)){65         if(0 == n)  break;66         for(i = 0; i < n ; ++i)67             scanf("%lf%lf%lf",&ps[i].x,&ps[i].y,&ps[i].z);68         printf("%.5f\n",BallCover());69     }70     return 0;71 }

 

Minimum sphere coverage in poj2069, simulated annealing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.