poj2420A Star not a Tree?(模拟退火)
生活随笔
收集整理的這篇文章主要介紹了
poj2420A Star not a Tree?(模拟退火)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接
求某一點到其它點距離和最小,求這個和,這個點 為費馬點。
做法:模擬退火
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 100000 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 struct point 18 { 19 double x,y; 20 point(double x=0,double y =0):x(x),y(y){} 21 }p[N]; 22 int n; 23 int dir[4][2] = {0,1,0,-1,1,0,-1,0}; 24 typedef point pointt; 25 pointt operator -(point a,point b) 26 { 27 return point(a.x-b.x,a.y-b.y); 28 } 29 double dis(point a) 30 { 31 return sqrt(a.x*a.x+a.y*a.y); 32 } 33 double cal(point pp) 34 { 35 int i; 36 double s = 0; 37 for(i = 1; i <= n; i++) 38 s+=dis(pp-p[i]); 39 return s; 40 } 41 int main() 42 { 43 int i; 44 while(scanf("%d",&n)!=EOF) 45 { 46 double t = 10000; 47 point pp = point(0,0); 48 for(i =1 ; i <= n;i ++) 49 { 50 scanf("%lf%lf",&p[i].x,&p[i].y); 51 pp.x+=p[i].x; 52 pp.y+=p[i].y; 53 } 54 pp.x /=n; 55 pp.y /=n; 56 double sum = cal(pp),tmp; 57 point tp,ans = pp; 58 while(t>0.02) 59 { 60 int flag = 1; 61 while(flag) 62 { 63 flag = 0; 64 for(i =0 ; i< 4 ; i++) 65 { 66 tp = point(pp.x+dir[i][0]*t,pp.y+dir[i][1]*t); 67 tmp = cal(tp); 68 if(tmp<sum) 69 { 70 sum = tmp; 71 flag = 1; 72 ans = tp; 73 } 74 } 75 pp = ans; 76 } 77 t/=2; 78 } 79 printf("%.0f\n",sum); 80 } 81 return 0; 82 }View Code
?
轉載于:https://www.cnblogs.com/shangyu/p/3890279.html
總結
以上是生活随笔為你收集整理的poj2420A Star not a Tree?(模拟退火)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 明日方舟陈是谁画的呢?
- 下一篇: 机器学习问题的十个实例【转】