POJA Star not a Tree?(模拟退火)
题意
给出$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?(模拟退火)的更多相关文章
- pojA Star not a Tree?
题目链接 pojA Star not a Tree? 题解 啊,模拟退火是个好东西 模拟退火即可 代码 #include<cmath> #include<cstdio> #in ...
- uva 10228 - Star not a Tree?(模拟退火)
题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...
- poj-2420 A Star not a Tree?(模拟退火算法)
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
- POJ 2420 A Star not a Tree?(模拟退火)
题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...
- poj2420A Star not a Tree?(模拟退火)
链接 求某一点到其它点距离和最小,求这个和,这个点 为费马点. 做法:模拟退火 #include <iostream> #include<cstdio> #include< ...
- Poj2420 A Star not a Tree? 模拟退火算法
题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...
- poj2420 A Star not a Tree? 模拟退火
题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...
- poj 2420 A Star not a Tree? —— 模拟退火
题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #include<iostream> #include< ...
- poj 2420 A Star not a Tree?——模拟退火
题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...
随机推荐
- HDU 4625. JZPTREE
题目简述:给定$n \leq 50000$个节点的数,每条边的长度为$1$,对每个节点$u$,求 $$ E_u = \sum_{v=1}^n (d(u, v))^k, $$ 其中$d(u, v)$是节 ...
- 《精通Spring4.X企业应用开发实战》读后感第四章(BeanFactory和ApllicationContext)
- 创建Maven项目步骤
(转自:http://blog.csdn.net/chuyuqing/article/details/28879477) 使用eclipse插件创建一个web project 首先创建一个Maven的 ...
- HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)
Sort it You want to processe a sequence of n distinct integers by swapping two adjacent sequence ele ...
- IIS 上传大文件 30MB 设置限制了上传大小
用uploadify在IIS6下上传大文件没有问题,但是迁移到IIS7下面,上传大文件时,出现HTTP 404错误. 查了半天,原来是IIS7下的默认设置限制了上传大小.这个时候Web.Config中 ...
- c/c++ 获取mysql数据库以blob类型储存的图片
简单的code如下: #include <iostream> #include <fstream> #include <sstream> #include < ...
- GitHub上最火爆!码代码不得不知的所有定律法则
目录 介绍 定律 阿姆达尔定律 (Amdahl's Law) 布鲁克斯法则 (Brooks's Law) 康威定律 (Conway's Law) 侯世达定律 (Hofstadter's Law) 技术 ...
- 洛谷P1831 杠杆数
P1831 杠杆数 题目描述 如果把一个数的某一位当成支点,且左边的数字到这个点的力矩和等于右边的数字到这个点的力矩和,那么这个数就可以被叫成杠杆数. 比如4139就是杠杆数,把3当成支点,我们有这样 ...
- if-else判断语句中经常犯的一个错误
假设题目为:随便给定一个数,三种情况:(1)若小于0,输出为“小于0”:(2)若在0-50之间,则输出“在0-50之间”.(3)若大于50,则输出“大于50”. 解法:如果我这么写,运行一下看看. i ...
- SpringBoot2.0 基础案例(07):集成Druid连接池,配置监控界面
一.Druid连接池 1.druid简介 Druid连接池是阿里巴巴开源的数据库连接池项目.Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能.功能强大,能防SQL注入,内置Login ...