题目

传送门:QWQ

分析

军训完状态不好QwQ,做不动难题,于是就学了下模拟退火。

之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂?

骗分利器,但据说由于调参困难,很多情况比不上多点爬山?

总体来说模拟退火的精髓就是:

可以看到T越大,转移的概率越高。 exp是在cmath里面有的,直接用就ok。

本题就是找出n个点的费马点,即找出一个点到这n个点的距离和最小。

代码

 // #include <bits/stdc++.h>
// POJ 2420
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn=;
const double pace=0.95;
struct Point { double x,y; }; int n;
Point p[maxn]; double sqr(double x){ return x*x; }
double dis(double x,double y,Point a){ return sqrt(sqr(x-a.x)+sqr(y-a.y)); }
double getnum(double x,double y){
double ans=;
for(int i=;i<=n;i++) ans+=dis(x,y,p[i]);
return ans;
}
int main(){
// srand(time(NULL));
srand();
Point ans;
scanf("%d",&n);
for(int i=;i<=n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
ans.x+=p[i].x; ans.y+=p[i].y;
}
ans.x/=n; ans.y/=n;
double t=1e5, end=0.02, pre=getnum(ans.x,ans.y);
while(t>end){ double xx=, yy=;
t*=pace;
for(int i=;i<=n;i++){
xx+=(p[i].x-ans.x)/dis(ans.x,ans.y,p[i]);
yy+=(p[i].y-ans.y)/dis(ans.x,ans.y,p[i]);
}
double tmp=getnum(ans.x+xx*t,ans.y+yy*t);
if(tmp<pre){
pre=tmp; ans.x+=xx*t; ans.y+=yy*t;
}
else if(exp((pre-tmp)/t)>(rand()%)/10000.0){
ans.x+=xx*t; ans.y+=yy*t;
}
t*=pace;
}
printf("%.0f",pre);
return ;
}

【POJ】2420 A Star not a Tree?(模拟退火)的更多相关文章

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

    题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #include<iostream> #include< ...

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

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

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

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

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

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

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

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

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

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

  7. 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 ...

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

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

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

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

随机推荐

  1. Java中线程同步的方法

    同步方法 即有synchronized关键字修饰的方法. 由于java的每个对象都有一个内置锁,当用此关键字修饰方法时, 内置锁会保护整个方法.在调用该方法前,需要获得内置锁,否则就处于阻塞状态. 注 ...

  2. 解析XML异常

    包含库 #include <QtXml/QDomDocument> 再修改.pro文件 将 QT += core gui 后面添加 xml —> QT += core gui xml

  3. shell 脚本实战笔记(10)--spark集群脚本片段念念碎

    前言: 通过对spark集群脚本的研读, 对一些重要的shell脚本技巧, 做下笔记. *). 取当前脚本的目录 sbin=`dirname "$0"` sbin=`cd &quo ...

  4. 【opencv基础】Rect类的神奇用法

    前言 最近看github上源码发现对两个cv::Rect使用相与(&)操作,猛地感觉自己蒙啦,Rect类还有这种神奇用法?!翻看opencv官网Rect类,果然如此! opencv中Rect类 ...

  5. MyEclipse快捷键及经验总结 绝对的有用 太给力了

    Ctrl+Shift+L    显示所有快捷键  Ctrl+K      参照选中的词(Word)快速定位到下一个  Ctrl+Shift+K    参照选中的词(Word)快速定位到上一个    C ...

  6. django中的FBV和CBV

    django中请求处理方式有2种:FBV 和 CBV 一.FBV FBV(function base views) 就是在视图里使用函数处理请求. 看代码: urls.py from django.c ...

  7. 使用Spring Boot操作Hive JDBC时,启动时报出错误:NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDef

    使用Spring Boot操作Hive JDBC时,启动时报出错误:NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDef ...

  8. Oracle 跨库 查询 复制表数据

    在目前绝大部分数据库有分布式查询的需要.下面简单的介绍如何在oracle中配置实现跨库访问. 比如现在有2个数据库服务器,安装了2个数据库.数据库server A和B.现在来实现在A库中访问B的数据库 ...

  9. es6知识点

    扩展运算符(三个点): 将值转换为参数序列. 解构赋值:比如:var [a,b,c]=[1,2,3];

  10. mysql explicit_defaults_for_timestamp参数

    在mysql中:- timestamp列如果没有显式定义为null,默认会被设置为not null属性.(其它的数据类型如果没有显式定义为not null,默认是可以为null的).设置timesta ...