题意

题目链接

给出$n$个点,求出一个点使得到各个点的距离之和最小,距离为欧几里得距离

Sol

模拟退火真是玄学,我退了一上午,最后把exp函数去了就A了。

后来改了改,发现是大小符号的问题。。

但是

这样是对的。

然后把RAND_MAX除过去就错了。。

需要改大小号才行。真是玄学。。。

/*
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int MAXN = 1e5 + ;
const double eps = 1e-, Dlt = 0.97;
int N;
double xx[MAXN], yy[MAXN];
double Ans;
double rand(double T, int opt) {
return opt * T ;
}
double sqr(double x) {
return x * x;
}
double calc(double x, double y) {
double ans = ;
for(int i = ;i <= N; i++)
ans += sqrt(sqr(x - xx[i]) + sqr(y - yy[i]));
return ans;
}
void solve(double x, double y) {
double now = calc(x, y);
Ans = min(Ans, now);
for(double T = ; T > eps; T *= Dlt) {
for(int i = -; i <= ; i++) {
for(int j = -; j <= ; j++) {
double wx = x + rand(T, i), wy = y + rand(T, j);
// if(wx < 0 || wy < 0 || wx > 10000 || wy > 10000) continue;
double wv = calc(wx, wy);
// printf("%lf %lf %lf\n", wx, wy, calc(wx, wy));
if(wv < Ans) x = wx, y = wy, Ans= wv;
if(wv < now || ( exp((now - wv) / T) < (rand() / RAND_MAX) )) x = wx, y = wy, now = wv;
// if(wv < now) x = wx, y = wy, now = wv;
}
}
}
}
int main() {
srand();
// freopen("a.in", "r", stdin);
Ans = 1e20;
scanf("%d", &N);
for(int i = ; i <= N; i++)
scanf("%lf %lf", &xx[i], &yy[i]);
//printf("%lf", calc(5000, 5000));
//for(int i = 1; i <= N; i++) {
// double x = rand() % 10000, y = rand() % 10000;
solve(xx[], yy[]);
//}
printf("%d", (int)(Ans + 0.5));
return ;
}
/*
4
0 0
0 5000
2354 10000
8787 0
*/

POJA Star not a Tree?(模拟退火)的更多相关文章

  1. pojA Star not a Tree?

    题目链接 pojA Star not a Tree? 题解 啊,模拟退火是个好东西 模拟退火即可 代码 #include<cmath> #include<cstdio> #in ...

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

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

  3. poj-2420 A Star not a Tree?(模拟退火算法)

    题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepte ...

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

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

  5. poj2420A Star not a Tree?(模拟退火)

    链接 求某一点到其它点距离和最小,求这个和,这个点 为费马点. 做法:模拟退火 #include <iostream> #include<cstdio> #include< ...

  6. Poj2420 A Star not a Tree? 模拟退火算法

    题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...

  7. poj2420 A Star not a Tree? 模拟退火

    题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...

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

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

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

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

随机推荐

  1. 'xxx' declared `static' but never defined

    'xxx' declared `static' but never defined [问题描述] uart.c文件中有函数read_sample的实现: [plain] view plain copy ...

  2. Apache日志解读

    想要知道什么人在什么时候浏览了网站的哪些内容吗?查看Apache的访问日志就可以知道.访问日志是Apache的标准日志,本文详细解释了访问日志的内容以及相关选项的配置. 一.访问日志的格式  Apac ...

  3. Linux运行Tomcat下的war文件

    1.查看Tomcat进程: ps -ef |grep tomcat 2.关闭Tomcat进程: kill -9 pid 3.关闭Tomcat运行: bin目录下执行 ./shutdown.sh 4. ...

  4. EasyUI把datagrid的值赋给表单

    $('#infoForm${INDEX}').form('load', rowToDto(pageConfig${INDEX}.infoName, row)); function rowToDto(i ...

  5. Windows server 2003+IIS6+PHP5.3 以上的安装配置

    一.安装好IIS 具体安装方法可查看:http://down.chinaz.com/server/201102/11_1.htm. 二.下载并安装IIS FastCGI 下载地址:http://www ...

  6. 创建Maven项目步骤

    (转自:http://blog.csdn.net/chuyuqing/article/details/28879477) 使用eclipse插件创建一个web project 首先创建一个Maven的 ...

  7. [转]C/C++获取当前系统时间

    原文转自:http://www.cnblogs.com/mfryf/archive/2012/02/13/2349360.html 个人觉得第二种还是比较实用的,而且也是最常用的~ 不过当计算算法耗时 ...

  8. iframe父页面和子页面相互调用的方法

    随着W3C一声令下,几年前使用非常频繁的frameset + frame已完成使命,光荣退伍.作为frameset的替代方案(姑且这么称吧),iframe的使用也多了起来.较frameset方案,if ...

  9. Java异常控制机制和异常处理原则【转】

    原文:https://www.jianshu.com/p/15872cba211d Java异常控制机制又被称为“违例控制机制”. 捕获程序错误最理想的时机是在编译阶段,这样可以彻底避免错误的代码运行 ...

  10. 11. 几点基于Web日志的Webshell检测思路

    摘要: Web日志记录了网站被访问的情况,在Web安全的应用中,Web日志常被用来进行攻击事件的回溯和取证.Webshell大多由网页脚本语言编写,常被入侵者用作对网站服务器操作的后门程序,网站被植入 ...