题目:http://poj.org/problem?id=2420

给出 n 个点的坐标,求费马点;

上模拟退火。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<cmath>
#define eps 1e-17
#define dt 0.99
using namespace std;
typedef double db;
int const xn=;
int n,xx[xn],yy[xn];
db ansx,ansy,ans;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
db dist(db x,db y,db a,db b){return sqrt((x-a)*(x-a)+(y-b)*(y-b));}
db cal(db x,db y)
{
db ret=;
for(int i=;i<=n;i++)ret+=dist(x,y,xx[i],yy[i]);
return ret;
}
void SA()
{
db T=,x=ansx,y=ansy,lst=ans,tx,ty,tmp;
while(T>eps)
{
tx=x+(rand()*-RAND_MAX)*T;
ty=y+(rand()*-RAND_MAX)*T;
tmp=cal(tx,ty); db delt=tmp-lst;
if(delt<||exp(delt/T)*RAND_MAX<rand())x=tx,y=ty,lst=tmp;
if(tmp<ans)ansx=tx,ansy=ty,ans=tmp;
T*=dt;
}
}
int main()
{
n=rd(); int sx=,sy=;
for(int i=;i<=n;i++)xx[i]=rd(),yy[i]=rd(),sx+=xx[i],sy+=yy[i];
ansx=1.0*sx/n; ansy=1.0*sy/n; ans=cal(ansx,ansy);
SA(); SA(); SA();
printf("%.0lf\n",ans);
return ;
}

poj 2420 A Star not a Tree? —— 模拟退火的更多相关文章

  1. poj 2420 A Star not a Tree?——模拟退火

    题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...

  2. POJ 2420 A Star not a Tree?(模拟退火)

    题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...

  3. 三分 POJ 2420 A Star not a Tree?

    题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...

  4. [POJ 2420] A Star not a Tree?

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4058   Accepted: 200 ...

  5. POJ 2420 A Star not a Tree? (计算几何-费马点)

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3435   Accepted: 172 ...

  6. POJ 2420 A Star not a Tree? 爬山算法

    B - A Star not a Tree? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...

  7. POJ 2420 A Star not a Tree?【爬山法】

    题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...

  8. 【POJ】2420 A Star not a Tree?(模拟退火)

    题目 传送门:QWQ 分析 军训完状态不好QwQ,做不动难题,于是就学了下模拟退火. 之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂? 骗分利器,但据说由于调参困 ...

  9. uva 10228 - Star not a Tree?(模拟退火)

    题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...

随机推荐

  1. AngularJS的ng-repeat显示属性名和属性值

    代码下载:https://files.cnblogs.com/files/xiandedanteng/AngularJSAuthorRepeat.rar 代码: <!DOCTYPE HTML P ...

  2. 【Unity3D自学记录】Unity3D之自制小钟表

    今天来写一个小钟表,事实上非常easy,就运用到了欧拉角. 首先创建时钟.分钟.秒钟以及4个点(12点.3点.6点.9点)偷懒了~~没弄那么多点. 时钟.分钟.秒钟这三个父级的中心一定要注意,我们旋转 ...

  3. PNG24图片兼容IE6解决的方法

    非常多人都遇到一个问题:那就是PNG不能正常显示,比方: 网上试过的非常多办法都非常难实现.要嘛就是效果不好,那如今最好的办法就是直接调用JS插件,解决! 点击下载 如今说一下怎么用这个文件吧! 首先 ...

  4. c#高级编程笔记----委托

    因为定义委托基本上是定义一个新类,所以可以在定义类的任何相同地方定义委托,也就是说,可以在另一个类的内部定义,也可以在任何类的外部定义,还可以在名称空间中把委托定义为顶层对象.根据定义的可见性,和委托 ...

  5. javascript变量初始化位置

    变量在之前<script type="text/javascript"></script>(或引用的js文件)中初始化,可以正常访问. 运行程序:弹出123 ...

  6. Selenium系列之--07 操作远程浏览器

    Selenium远程控制浏览,可以通过如下两种方式实现,本质上都是Selenium Grid a.  客户机启Selenium Standalone Server 作为远程服务,服务端通过调用Remo ...

  7. 04 http协议模拟登陆发帖

    <?php require('./http.class.php'); $http = new Http('http://home.verycd.com/cp.php?ac=pm&op=s ...

  8. 【BZOJ3197】[Sdoi2013]assassin 树同构+动态规划+KM

    [BZOJ3197][Sdoi2013]assassin Description Input Output Sample Input 4 1 2 2 3 3 4 0 0 1 1 1 0 0 0 Sam ...

  9. Appnium安装-Mac平台

    Appium的安装-MAC平台   其实Appium的安装方式主要有两种: 1)自己安装配置nodejs的环境,然后通过npm进行appium的安装 2)直接下载官网提供的dmg进行安装,dmg里面已 ...

  10. git项目.gitignore文件不生效解决办法

    配置好.gitignore文件如下: HELP.md /target/ !.mvn/wrapper/maven-wrapper.jar ### STS ### .apt_generated .clas ...