标签:
题目链接:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5219 | Accepted: 2491 |
Description
Input
Output
Sample Input
4 0 0 0 10000 10000 10000 10000 0
Sample Output
28284
题目:求一个多边形的费马点到所有点的距离和,费马点是多边形内到顶点距离和最短的点;
思路:更poj1379一个人思路,用模拟退火算法找费马点求距离;
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <ctime> #include <algorithm> using namespace std; const int num=100; const int cnt=20; const double PI=acos(-1.0); const double inf=1e12; struct node { double px,py,dis; }; node ans[100]; int n; double x[105],y[105]; double get_dis(double a,double b,double c,double d) { return sqrt((c-a)*(c-a)+(d-b)*(d-b)); } void Iint() { for(int i=0;i<num;i++) { ans[i].px=1.0*(rand()%32767)/32767*10000; ans[i].py=1.0*(rand()%32767)/32767*10000; ans[i].dis=0; for(int j=0;j<n;j++) { ans[i].dis+=get_dis(ans[i].px,ans[i].py,x[j],y[j]); } } } int main() { srand((unsigned)time(NULL)); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf%lf",&x[i],&y[i]); } Iint(); double temp=get_dis(0,0,10000,10000); while(temp>0.2) { for(int i=0;i<num;i++) { double fx,fy; for(int j=0;j<cnt;j++) { double cur=2.0*PI*(rand()%32767)/32767; fx=ans[i].px+cos(cur)*temp; fy=ans[i].py+sin(cur)*temp; if(fx<0||fx>10000||fy<0||fy>10000)continue; double dist=0; for(int k=0;k<n;k++) { dist+=get_dis(fx,fy,x[k],y[k]); } if(dist<ans[i].dis) { ans[i].px=fx; ans[i].py=fy; ans[i].dis=dist; } } } temp*=0.8; } double ans_dis=inf; int ans_x,ans_y; for(int i=0;i<num;i++) { if(ans[i].dis<ans_dis) { ans_dis=ans[i].dis; } } printf("%.0lf\n",ans_dis); return 0; }
poj-2420 A Star not a Tree?(模拟退火算法)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5289860.html