题目链接:http://poj.org/problem?id=2420

求费马点,即到所有其他点总和距离最小的点。

一开始想枚举一个坐标,另一个坐标二分的,但是check的时候还是O(n)的,复杂度相当于O(n^2lgn),没意义。

学习一种神贪心,模拟退火。感觉和启发式搜索有点像啊,又有点像牛顿迭代。

  思路就是,固定一个点和一个步长,从这个点开始向四个方向扩展,扩展的步长就是当前步长。如果扩展到的点可以更新答案,那么记住这个点,也就是说这个贪心方向(梯度?)是正确的。就可以拿着这个点继续沿着这个方向走了(剩余的方向可以不考虑了)。

  如果四个方向都不能走,说明步长过长,这个时候模拟退火,将步长按比率缩小。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std; typedef pair<double, double> pdd;
#define x first
#define y second
const double eps = 1e-;
const double delta = 0.98;
const int maxn = ;
const int dx[] = {, -, , };
const int dy[] = {, , , -};
int n;
double ret;
pdd cur;
pdd p[maxn]; double dist(pdd a, pdd b) {
double p = a.x - b.x;
double q = a.y - b.y;
return sqrt(p * p + q * q);
} int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
for(int i = ; i < n; i++) {
scanf("%lf %lf", &p[i].x, &p[i].y);
}
int t = ;
ret = 1e100;
cur = pdd(., .);
while(t > eps) {
bool flag = ;
while(flag) {
flag = ;
for(int i = ; i < ; i++) {
pdd pos = pdd(cur.x+dx[i]*t, cur.y+dy[i]*t);
double tmp = .;
for(int j = ; j < n; j++) {
tmp += dist(pos, p[j]);
}
if(ret - tmp > eps) {
ret = tmp;
cur = pos;
flag = ;
break;
}
}
}
t *= delta;
}
printf("%.0f\n", ret);
}
return ;
}

[POJ2420]A 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. Poj2420 A Star not a Tree? 模拟退火算法

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

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

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

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

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

  5. uva 10228 - Star not a Tree?(模拟退火)

    题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...

  6. 【模拟退火】poj2420 A Star not a Tree?

    题意:求平面上一个点,使其到给定的n个点的距离和最小,即费马点. 模拟退火的思想是随机移动,然后100%接受更优解,以一定概率接受更劣解.移动的过程中温度缓慢降低,接受更劣解的概率降低. 在网上看到的 ...

  7. POJ-2420 A Star not a Tree? 梯度下降 | 模拟退火

    题目链接:https://cn.vjudge.net/problem/POJ-2420 题意 给出n个点,找一个点,使得这个点到其余所有点距离之和最小. 思路 一开始就在抖机灵考虑梯度下降,猜测是个凸 ...

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

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

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

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

随机推荐

  1. 使用linq的好处

    1.linq非常方便,把复杂的业务逻辑从数据库分离,起到了很好的优化作用 2.linq非常灵活,可以用基本统一的访问方式,访问各种数据源,对项目的管理和维护,起到了十分便捷的作用 3.用linq可以不 ...

  2. jQuery工具函数(转)

    原文地址:http://www.cnblogs.com/kissdodog/archive/2012/12/27/2835561.html 作者:逆心 ------------------------ ...

  3. TensorFlow安装(Ubuntu 16.04)

    原文链接 github not support on this platform pip安装: # Ubuntu/Linux 64-bit $ sudo apt-get install python- ...

  4. js 实现类似php函数number_format的功能

    今天同事在做一个功能的时候需要使用js来实现类似php函数number_format的功能,最后就有了下面的方法,可以实现了: /** * number_format * @param number ...

  5. Leetcode: Ternary Expression Parser

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

  6. C++之路进阶——codevs1789(最大获利)

    1789 最大获利 2006年NOI全国竞赛  时间限制: 2 s  空间限制: 128000 KB  题目等级 : 大师 Master       题目描述 Description 新的技术正冲击着 ...

  7. input中空格后的数据不显示

    bug,input中空格后的数据不显示     昨天在修bug,有一个bug是用户修改的个人信息的地址栏,输入有空格的话,空格后面的内容存不上,而且没有报错,奇怪了,只好跟踪下. 页面上的输入框就是个 ...

  8. java插入排序

    /** * 插入排序 * @param a * @date 2016-10-8 * @author shaobn */ public static void insertSort(int[] a){ ...

  9. OO Design

    什么是设计原则? 设计原则是基本的工具,应用这些规则可以使你的代码更加灵活.更容易维护.更容易扩展.基本原则:封装变化Encapsulate what varies.面向接口变成而不是实现 Code ...

  10. [原] VS新添加WebApplication项目,无法运行,请求帮助,问题如何解决

    最近在WIN10 Pro上安装运行VS2012(安装顺利),新建WebApplication项目,无法运行,编译都无法通过,但都是警告. 症状: 1.新建项目无法编译: 2.新建后,默认引用全部感叹号 ...