题意

题目链接

给出$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. 非常好的LINUX学习者博客

    http://blog.csdn.net/qq_21794823/article/category/6496200

  2. 杂项-QRCode:ZXing

    ylbtech-杂项-QRCode:ZXing 1.返回顶部 1. ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.Zxing可以实现使用 ...

  3. day1 java基础回顾-泛型

    2.泛型(Generic) 当集合中存储的对象类型不同时,那么会导致程序在运行的时候的转型异常 1 import java.util.ArrayList; 2 import java.util.Ite ...

  4. 打包python文件为exe文件(PyInstaller工具使用方法)

    最近做的新浪微博爬虫程序,打算打包成.exe软件以方便使用,网上找到一个很好的打包工具pyinstaller,这里记录一下打包的方法. 一.下载pyinstaller 我使用的版本为PyInstall ...

  5. ZOJ 3512 Financial Fraud (左偏树)

    题意:给定一个序列,求另一个不递减序列,使得Abs(bi - ai) 和最小. 析:首先是在每个相同的区间中,中位数是最优的,然后由于要合并,和维护中位数,所以我们选用左偏树来维护,当然也可以用划分树 ...

  6. HDU - 5094 Maze(状压+bfs)

    Maze This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enter ...

  7. backgroundWorker取消后,重新开始就报错:此 BackgroundWorker 当前正忙,无法同时运行多个任务。

    使用BackgroundWorker控件,有2个按钮buttonBegin和buttonCancel.其他都正常,只是在用buttonBegin开始运行,然后点击buttonCancel取消后,到这里 ...

  8. 我们团队是如何落地DDD的(1)

    最近发现文章老是被窃取,有些平台举报了还没有用.请识别我的id方丈的寺院. 摘要 DDD领域驱动设计,起源于2004年著名建模专家Eric Evans发表的他最具影响力的著名书籍:Domain-Dri ...

  9. 3DMAX可编辑多边形常用命令-桥

    桥命令可以连接连个面 下图两个cube(可编辑多边形)已经被附加到一起,选中相对的两个面,点击桥 结果

  10. 30个php操作redis常用方法代码例子【转】

    背景:redis这个新产品在sns时很火,而memcache早就存在, 但redis提供出来的功能,好多网站均把它当memcache使用,这是大才小用,这儿有30个方法来使用redis,值得了解. 这 ...