POJ2420:A Star not A Tree?
我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html
我对爬山的理解:https://www.cnblogs.com/AKMer/p/9555215.html
题目传送门:http://poj.org/problem?id=2420
这题就是要我们求平面图费马点……
然后我似乎先写了广义费马点……顺序错了……至于对费马点的解释去这里看吧……
BZOJ3680吊打XXX:https://www.cnblogs.com/AKMer/p/9588724.html
时间复杂度:\(O(能A)\)
空间复杂度:\(O(能A)\)
爬山算法代码如下:
#include <ctime>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define sqr(x) ((x)*(x))
const int maxn=105;
int n;
double ans=1e18,ansx,ansy;
struct computer {
double x,y;
}p[maxn];
double len() {
double x=rand()%200000-100000;
return x/100000;
}
double dis(double x1,double y1,double x2,double y2) {
return sqrt(sqr(x1-x2)+sqr(y1-y2));
}
double calc(double x,double y) {
double tmp=0;
for(int i=1;i<=n;i++)
tmp+=dis(x,y,p[i].x,p[i].y);//除了这里没有乘权值就跟吊打XXX没有任何区别了
if(tmp<ans) {ans=tmp;ansx=x;ansy=y;}
return tmp;
}
int main() {
srand(time(0));
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
ansx+=p[i].x,ansy+=p[i].y;
}ansx/=n;ansy/=n;double now_x=ansx,now_y=ansy;
for(int T=1e7;T>=1e-7;T*=0.98) {
double nxt_x=now_x+len()*T;
double nxt_y=now_y+len()*T;
if(calc(nxt_x,nxt_y)<calc(now_x,now_y))
now_x=nxt_x,now_y=nxt_y;
}
printf("%.0lf\n",ans);
return 0;
}
模拟退火代码如下:
#include <ctime>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define sqr(x) ((x)*(x))
const int maxn=101;
const double T_0=1e-7;
const double del_T=0.98;
int n;
double ans=1e18,ansx,ansy;
struct computer {
double x,y;
}p[maxn];
double len() {
double x=rand()%200000-100000;
return x/100000;
}
double dis(double x1,double y1,double x2,double y2) {
return sqrt(sqr(x1-x2)+sqr(y1-y2));
}
double calc(double x,double y) {
double tmp=0;
for(int i=1;i<=n;i++)
tmp+=dis(x,y,p[i].x,p[i].y);
if(tmp<ans) {ans=tmp;ansx=x;ansy=y;}
return tmp;
}
void Anneal() {
double T=10000000,now_x=ansx,now_y=ansy;
while(T>=T_0) {
double nxt_x=now_x+len()*T;
double nxt_y=now_y+len()*T;
double tmp1=calc(now_x,now_y);
double tmp2=calc(nxt_x,nxt_y);
if(tmp2<tmp1||exp((tmp1-tmp2)/T)*RAND_MAX>rand())
now_x=nxt_x,now_y=nxt_y;
T*=del_T;
}
}
int main() {
srand(time(0));
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
ansx+=p[i].x,ansy+=p[i].y;
}ansx/=n;ansy/=n;Anneal();
printf("%.0lf\n",ans);
return 0;
}
因为这题要我们求的答案是总距离(并且还是保存整数位的),所以相对吊打XXX简单多了。两种算法都可以\(A\)我自己造的数据
POJ2420:A Star not A Tree?的更多相关文章
- POJ 2420:A Star not a Tree?
原文链接:https://www.dreamwings.cn/poj2420/2838.html A Star not a Tree? Time Limit: 1000MS Memory Limi ...
- POJ 2420 A Star not a Tree?【爬山法】
题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...
- 【POJ2420】A star not a tree?
蒟蒻开始学模拟退火…… 起初一直不肯学,因为毕竟玄学算法…… 哎呀玄学怎么就没用呢?对不对? #include<iostream> #include<cstdio> #incl ...
- 【POJ】【2420】A Star not a Tree?
模拟退火 Orz HZWER 这题的题意是在二维平面内找一点,使得这点到给定的n个点的距离和最小……0.0 模拟退火算法请戳这里 //POJ 2420 #include<ctime> #i ...
- poj-2420 A Star not a Tree?(模拟退火算法)
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
- POJ2069:Super Star
我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...
- 模拟退火算法A Star not a Tree?(poj2420)
http://write.blog.csdn.net/postedit A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Tot ...
- poj2420 A Star not a Tree? 找费马点 模拟退火
题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
- POJ 2420 A Star not a Tree? 爬山算法
B - A Star not a Tree? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...
随机推荐
- 软RAID5制作流程
说明:本实验没有用到多个磁盘,而是利用单个磁盘划分出的多个分区来仿真的,如果在实际项目中,请依情况而定. 1. 分区 我这里划分6个分区,用4个分区组成RAID 5,用1个分区作为spare disk ...
- 洛谷 P1558 色板游戏
洛谷 题解里面好像都是压位什么的, 身为蒟蒻的我真的不会, 所以就来谈谈我的30颗线段树蠢方法吧! 这题初看没有头绪. 然后发现颜色范围好像只有30: 所以,我就想到一种\(sao\)操作,搞30颗线 ...
- Nvidia NVENC 硬编码预研总结
本篇博客记录NVENC硬编码的预研过程 github: https://github.com/MarkRepo/NvencEncoder 步骤如下: (1)环境搭建 (2)demo编译,测试,ARG ...
- Data Structure Linked List: Write a function to reverse a linked list
iterative太简单不写了 http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/ ...
- [原创]Scala学习:流程控制,异常处理
1.流程控制 1)do..while def doWhile(){ var line="" do{ line = readLine() println("readline ...
- Python 3 面向对象进阶
Python 3 面向对象进阶 一. isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的 ...
- 常见http返回状态码
200:表示从客户端发来的请求在服务器端被正常处理了. 302:临时重定向,该状态码表示请求的资源已经被分配了新的URI,希望用户本次能够通过新的UIRI访问. 304:未修改,服务端资源未改变,可直 ...
- location记录<18.7.21>
// var index = location.href; // console.log(index) // // indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. // v ...
- 适用grunt的注意点
0.使用grunt可以为前端开发省去很多工作量,与git版本控制器配合起来不要太完美,一般也都是这么用的: 1.先安装node.js,下载软件安装就行了,一般自带npm管理器; 2.通过npm安装gr ...
- 如何在unigui中用代码展开一棵树?
procedure expandtree(tree:tunitreeview);begin UniSession.AddJS('setTimeout("' + Tree1.JSName ...