【POJ2420】A star not a tree?
蒟蒻开始学模拟退火……
起初一直不肯学,因为毕竟玄学算法……
哎呀玄学怎么就没用呢?对不对?
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
using namespace std;
int n;
double xx,yy,ans,T;
struct Point{double x,y;}p[];
inline double sqr(double x){return x*x;}
inline double dis(double x,double y,Point p){
return sqrt(sqr(x-p.x)+sqr(y-p.y));
}
inline double getsum(double x,double y){
double ret=;
for(int i=;i<=n;i++)ret+=dis(x,y,p[i]);
return ret;
}
int main(){
srand(time(NULL));
while(~scanf("%d",&n)){
xx=yy=;ans=1e20;T=;
for(int i=;i<=n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
xx+=p[i].x;yy+=p[i].y;
}
xx/=n;yy/=n;ans=getsum(xx,yy);
double tmp,x,y;
while(T>=0.02){
x=y=;
for(int i=;i<=n;i++){
x+=(p[i].x-xx)/dis(xx,yy,p[i]);
y+=(p[i].y-yy)/dis(xx,yy,p[i]);
}
tmp=getsum(xx+x*T,yy+y*T);
if(tmp<ans){ans=tmp;xx+=x*T;yy+=y*T;}
else if(log((tmp-ans)/T)<(rand()%)/10000.0)
{ans=tmp;xx+=x*T;yy+=y*T;}
T*=0.9;
}
printf("%.0lf\n",ans);
}
}
【POJ2420】A star not a tree?的更多相关文章
- 【POJ】【2420】A Star not a Tree?
模拟退火 Orz HZWER 这题的题意是在二维平面内找一点,使得这点到给定的n个点的距离和最小……0.0 模拟退火算法请戳这里 //POJ 2420 #include<ctime> #i ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【BZOJ2843】极地旅行社(Link-Cut Tree)
[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...
- 【BZOJ4530】大融合(Link-Cut Tree)
[BZOJ4530]大融合(Link-Cut Tree) 题面 讨厌权限题!!! Loj链接 题目描述 小强要在 N个孤立的星球上建立起一套通信系统.这套通信系统就是连接 N个点的一个树.这个树的边是 ...
- 【BZOJ1969】航线规划(Link-Cut Tree)
[BZOJ1969]航线规划(Link-Cut Tree) 题面 BZOJ 题解 删边操作 套路呀 离线读入倒过来做 变成加边操作 现在考虑怎么确定两点直接的关键路径条数 如果是一棵树,那么每条边都是 ...
- 【BZOJ4825】【HNOI2017】单旋(Link-Cut Tree)
[BZOJ4825][HNOI2017]单旋(Link-Cut Tree) 题面 题面太长,懒得粘过来 题解 既然题目让你写Spaly 那就肯定不是正解 这道题目,让你求的是最大/最小值的深度 如果有 ...
- 【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)
[BZOJ3669][Noi2014]魔法森林(Link-Cut Tree) 题面 题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n ...
- 【BZOJ2049】洞穴勘测(Link-Cut Tree)
[BZOJ2049]洞穴勘测(Link-Cut Tree) 题面 题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别 ...
- 【BZOJ3999】[TJOI2015]旅游(Link-Cut Tree)
[BZOJ3999][TJOI2015]旅游(Link-Cut Tree) 题面 BZOJ 洛谷 题解 一道不难的\(LCT\)题(用树链剖分不是为难自己吗,这种有方向的东西用\(LCT\)不是方便那 ...
随机推荐
- 用select模拟一个socket server
1, 必须在非阻塞模式下,才能实现IO的多路复用,否则一个卡住就都卡住了.(单线程下的多路复用) 先检测自己,现在没有客户端连进来,所以会卡住. # 用select去模拟socket,实现单线程下的多 ...
- usaco中遇到的问题
numbers are integers with unique digits 意思是数字中的每一个数字都是不一样的& 让一个图成为强连通图只需添加max(出度为0,入度为0)的点,然后如果图 ...
- POJ2079:Triangle——题解
http://poj.org/problem?id=2079 题目大意:求最大面积的三角形. —————————————————— 可以知道,最大面积的三角形的顶点一定是最大凸包的顶点. 接下来就是O ...
- BZOJ4196:[NOI2015]软件包管理器——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=4196 https://www.luogu.org/problemnew/show/P2146 你决定 ...
- 51nod 1554:欧姆诺姆和项链——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1554 题目: 有一天,欧姆诺姆发现了一串长度为n的宝石串,上面有五颜六色 ...
- BZOJ2823 [AHOI2012]信号塔 【最小圆覆盖】
题目链接 BZOJ2823 题解 最小圆覆盖模板 都懒得再写一次 #include<iostream> #include<cstdio> #include<cmath&g ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- 51nod 1275 连续子段的差异(twopointer+单调队列)
对于每一个i找到最近的j满足最大值-最小值>K,对答案的贡献为j-i,用单调队列维护最值即可 #include<iostream> #include<cstdlib> # ...
- 【BZOJ 1998】[Hnoi2010]Fsk物品调度 置换群+并查集
置换群的部分水得一比,据说是经典的置换群理论(然而我并不知道这理论是啥).重点就在于怎么求pos!!!容易发现这个东西是这样的:每次寻找pos,先在本环里找,找不到再往下一个环里找,直到找到为止……一 ...
- HDU2841 (队列容斥)
Visible TreesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...