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个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
随机推荐
- delphi webbrowser 经常使用的演示样本
var Form : IHTMLFormElement ; D:IHTMLDocument2 ; begin with WebBrowser1 do begin D := Document as IH ...
- Javascript 进阶 作用域 作用域链
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/25076713 一直觉得Js很强大,由于长期不写js代码,最近刚好温故温故. 1. ...
- 自己实现的Boost库中的lexical_cast随意类型转换
知道了C++的I/O设施之后.这些就变的非常easy了. 假设你常常使用,时间长了就会有感觉.这个事情是多此一举吗?就当是练习吧,知道原理之后,你会认为用起来更舒畅,更喜欢C++了. #include ...
- Lucene40SkipListWriter
多级跳跃表是保存在tim文件里的. tip是term index,tim是term dictionary.记忆方法是,p是pointer因此是term index. 这个类会保存多个level的las ...
- REST API 基于ACCESS TOKEN
REST API 基于ACCESS TOKEN 的权限解决方案 REST 设计原则是statelessness的,而且但客户端是APP时,从APP发起的请求,不是基于bowers,无法带相同的se ...
- LinearLayout具体解释一:LinearLayout的简单介绍
LinearLayout,中文意思是线性布局.假设你是初学android的,肯定会非常困惑"啥叫布局",啥又叫"线性布局"呢. 有的时候,我尝试用官方的语言去解 ...
- Android访问设置
在需求 AndroidManifest.xml 中增加下面代码: (1)假设应用程序须要訪问网络权限 <uses-permission android:name="android.pe ...
- ListView 实现多选/无线电
ListView本身与无线电.多选模式.由listview.setChoiceMode设置: listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) ...
- SpringMVC Ajax返回的请求json
的方式来解决在中国字符串乱码问题
1.org.springframework.http.converter.StringHttpMessageConverter类是类处理请求或相应的字符串.和默认字符集ISO-8859-1,所以当返回 ...
- 《TCP/IP作品详细解释2:达到》注意事项--IP地址
1.接口和地址 如下面的图全部本文中讨论的接口和地址的结构看一个示例配置: 上图中显示了我们三个接口样例:以太网接口,SLIP接口和环回接口. 它们都有一个链路层地址作为地址列表中的第一个结点. 显示 ...