uva 10228 - Star not a Tree?(模拟退火)
题目链接:uva 10228 - Star not a Tree?
题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点)
解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处,则转移。每次操作之后降低步长后做相同的操作,直到步长小于指定精度。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
const int maxn = 105;
const int MOD = 1e4+1;
const double eps = 1e-9;
const double INF = 0x3f3f3f3f3f3f3f3f;
const int dir[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };
struct point {
double x, y;
point (double x = 0, double y = 0) {
this->x = x;
this->y = y;
}
}p[maxn];
int N;
double distance (point u) {
double ret = 0;
for (int i = 0; i < N; i++) {
double dx = u.x - p[i].x;
double dy = u.y - p[i].y;
ret += sqrt(dx * dx + dy * dy);
}
return ret;
}
double solve () {
int ti = 10;
double ret = INF, r = 0.9;
srand(time(NULL));
while (ti--) {
point u(rand() % MOD, rand() % MOD);
double step = 1e4;
double dis = distance(u);
while (step > eps) {
point v = u;
for (int i = 0; i < 8; i++) {
point tr(u.x + dir[i][0] * step, u.y + dir[i][1] * step);
double tmpd = distance(tr);
if (tmpd < dis) {
dis = tmpd;
v = tr;
}
}
u = v;
step *= r;
ret = min(ret, dis);
}
}
return ret;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
printf("%.0lf\n", solve());
if (cas)
printf("\n");
}
return 0;
}
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 ...
- POJA Star not a Tree?(模拟退火)
题意 题目链接 给出$n$个点,求出一个点使得到各个点的距离之和最小,距离为欧几里得距离 Sol 模拟退火真是玄学,我退了一上午,最后把exp函数去了就A了. 后来改了改,发现是大小符号的问题.. 但 ...
- 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 ...
- poj2420 A Star not a Tree? 找费马点 模拟退火
题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
随机推荐
- Ubuntu 下一个 vim 建立python 周围环境 构造
于Windows通过使用各种现成的工具使用,去Linux下一个,没有一个关于线索--总之google有些人的经验,折腾来折腾,开发环境也算是一个好工作. 1. 安装完成vim # apt-get in ...
- kNN(K-Nearest Neighbor)最近的分类规则
KNN最近的规则,主要的应用领域是未知的鉴定,这一推断未知的哪一类,这样做是为了推断.基于欧几里得定理,已知推断未知什么样的特点和最亲密的事情特性: K最近的邻居(k-Nearest Neighbor ...
- 给EasyUI的DateBox控件添加清除button
EasyUI中间DateBox控制甚至没有被清除button.例如下面的附图: 真是不可思议,对于要求日期格式必须选择的情况下,不能清空日期,很不方便. 尽管能够通过手工改动EasyU ...
- FTP文件操作之创建目录
前面几篇博客讲的都是对文件的操作,今天跟大家说一说对目录的操作,先让我们从创建目录开始说起吧. 创建目录很简单,首先创建一个ftp对象,然后将参数传进去,接着告诉ftp对象需要执行什么操作即可. 下面 ...
- Ejb in action(两)——示例入门
在前面的文章中,,我们推出Ejb相关概念.在此之前展开的阐述,我给大家Ejb示例.加深印象. 开发环境:eclipse 应用server:jboss 1.服务端程序 在Eclipse中创建一个Ejb项 ...
- TypeScript 5 Angular 2
TypeScript 5 分钟快速入门 翻译:Angular 2 - TypeScript 5 分钟快速入门 原文地址:https://angular.io/docs/ts/latest/quicks ...
- T-SQL中default值的使用
今天介绍一下通过T-SQL语句来创建表时使用default的关键字来自动使用默认值,这个关键字和其它的如:identity,primary key ,not null ,unique等不是相同,这里简 ...
- 【rman,1】经典案例增量备份
一.备份策略: 1.星期天晚上 -level 0 backup performed(全备份) 2.星期一晚上 -level 2 backup performed 3.星期二晚上 ...
- [LeetCode257]Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- PHP之运用CI用钩子实现URL权限控制————————【Badboy】
<span style="background-color: rgb(247, 252, 255); font-family: Verdana, Arial, Helvetica, s ...