题目链接: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?(模拟退火)的更多相关文章

  1. poj-2420 A Star not a Tree?(模拟退火算法)

    题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepte ...

  2. POJ 2420 A Star not a Tree?(模拟退火)

    题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...

  3. poj2420A Star not a Tree?(模拟退火)

    链接 求某一点到其它点距离和最小,求这个和,这个点 为费马点. 做法:模拟退火 #include <iostream> #include<cstdio> #include< ...

  4. Poj2420 A Star not a Tree? 模拟退火算法

    题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...

  5. poj2420 A Star not a Tree? 模拟退火

    题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...

  6. POJA Star not a Tree?(模拟退火)

    题意 题目链接 给出$n$个点,求出一个点使得到各个点的距离之和最小,距离为欧几里得距离 Sol 模拟退火真是玄学,我退了一上午,最后把exp函数去了就A了. 后来改了改,发现是大小符号的问题.. 但 ...

  7. poj 2420 A Star not a Tree? —— 模拟退火

    题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #include<iostream> #include< ...

  8. poj 2420 A Star not a Tree?——模拟退火

    题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...

  9. poj2420 A Star not a Tree? 找费马点 模拟退火

    题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...

随机推荐

  1. 初步C++类模板学习笔记

    类模板 实现:在上课时间的定义给它的一个或多个参数,这些参数代表了不同的数据类型.                              -->抽象的类. 在调用类模板时, 指定參数, 由编 ...

  2. [C++] WinAES问题

    WinAES这是一个很好windows CAPI包. 假设C++项目需求和java程序aes加密和通信的解密,然后WinAES代码是有问题. java的aes默认代码未设置IV和用途ECB模式. 因此 ...

  3. mediator pattern

    20.4 中介者模式总结 中介者模式将一个网状的系统结构变成一个以中介者对象为中心的星形结构,在这个星型结构中,使用中介者对象与其他对象的一对多关系来取代原有对象之间的多对多关系.中介者模式在事件驱动 ...

  4. 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

    原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...

  5. C++ Primer 学习笔记_53_类和数据抽象 --友元、static员

    分类 --友元.static成员 一.友元 友元机制同意一个类将对其.友元关系:一个样例 如果一个窗体管理类Window_Mgr可能须要訪问由其管理的Screen对象的内部数据.Screen应该同意其 ...

  6. 高性能 Socket 组件 HP-Socket v3.2.1-RC2 公布

    HP-Socket 是一套通用的高性能 TCP/UDP Socket 组件,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C+ ...

  7. hive的非交互模式

    在linux的终端运行:$HIVE_HOME/bin/hive 会进入交互模式: $HIVE_HOME/bin/hive  -e或者-f 是非交互模式 1.非交互模式运行HQL语句 $HIVE_HOM ...

  8. C#使用SqlBulkCopy将DataTable写入数据库的表中(表不存在则创建新表,数据存在则更新,不存在则插入)

    原文:.net使用SqlBulkCopy导入数据(创建新表) .net2.0后ado.net提供了一个快速导入sqlserver的方法sqlbulkcopy.导入效率非常高.  包装了一个简单的sql ...

  9. WPF 绑定

    WPF里分三种Binding:Binding, PriorityBinding, MultiBinding,这三种Binding的基类都是BindingBase,而BindingBase又继承于Mar ...

  10. JAVA经BigDecimal圆角的解决方案及注意事项

    /** * @Description: 四舍五入住宿2小数位数 * @param valueName * @return */ public static Double formateDouble45 ...