题意

题目链接

给出$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. 利用正则表达式类解析SQL语句,达到Worklist兼容各个RIS数据库的目的

    在做RIS的项目中遇到一个问题, 因为Worklist要兼容各个RIS数据库, 因此设计了目前这个架构. 1.xml文件来配置RIS数据库的数据源, 2.xml文件来存储关于查询/更新数据库的SQL语 ...

  2. SimpliciTI简介

    SimpliciTI简介 SimpliciTI是TI开发的一份专门针对其CCxxxx系列无线通信芯片的网络协议.按照其官方说法SimpliciTI是一个基于连接的点对点通讯协议.它支持两种网络拓扑结构 ...

  3. 04_数据库升级onUpgrade&ondowngrade

    如果想操作多个数据库就不要把数据库的名字写死了 public MyOpenHelper(Context context, String name){ //第一个参数上下文 //第二个参数 数据库的名字 ...

  4. pow()函数的精度问题

    妈蛋这个坑了我大半个小时都想不出个原因..后来看到pow的定义才想起,数据类型很重要啊.. 1.底数用常量,指数用整型 #include <stdio.h> #include <ma ...

  5. django根据不同app配置相应的log文件

    django根据不同app配置相应的log文件 settings.py # django logging LOG_PATH = "/var/log/blog/" LOGGING = ...

  6. win32 API中GetSystemMetrics函数

    1. SM_ARRANGE: 用于说明系统如何安排最小化窗口,根据显示器的不同系统数据可能有所不同.其包含一个起始位置和方向.关于在程序中怎么使用我还没有见个这样的代码. 起始位置可为下列值之一: A ...

  7. Oracle SQL调优之分区表

    目录 一.分区表简介 二.分区表优势 三.分区表分类 3.1 范围分区 3.2 列表分区 3.3 散列分区 3.4 组合分区 四.分区相关操作 五.分区相关查询 附录:分区表索引失效的操作 一.分区表 ...

  8. iOS 防止抓包

    原文链接:https://www.jianshu.com/p/1ab83d726d3f 现在 APP 开发,数据的保密性越来越受重视,保密性高才不会让数据轻易被泄露,同时保护到用户和公司的利益.如何做 ...

  9. Unity开发Android应用优化指南(下)

    http://forum.china.unity3d.com/thread-27044-1-1.html 在Unity开发Android应用优化指南(上)一文中,从游戏性能,脚本等方面进行了分析和总结 ...

  10. cf414B(dp)

    题目链接:http://codeforces.com/problemset/problem/414/B 题意:定义所有元素是其前一个元素的倍数的数列为good sequence,给出 n, 和 k,求 ...