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 ...
随机推荐
- Cow Contest(传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10450 Accepted: 5841 Desc ...
- elasticsearch从入门到出门-02-简单的CRUD
操作背景: 电商网站上面的一个商品的增删改查: es 能接受的都是JSON格式的数据 Es 提供了一套简单的集群信息健康监控的api GET /_cat/health?v epoch t ...
- 我的Android进阶之旅------>Android中AsyncTask源码分析
在我的<我的Android进阶之旅------>android异步加载图片显示,并且对图片进行缓存实例>文章中,先后使用了Handler和AsyncTask两种方式实现异步任务机制. ...
- 初步jmeter安装与使用
前言,最近公司做了面向全国用户的教育平台,由于测试人员以功能测试为主,于是接口代码压测就被开发揽了,这就开始倒腾jmeter了,其实我想对于java,我更愿意用Python的工具,毕竟我爬虫时用的Py ...
- python多版本管理
1.查看系统中的安装了那些python版本 2.查看系统中的alternatives命令是否安装 3.使用alternatives --install 接管python -install 选项使用了多 ...
- sql把字符数组转换成表
需求:把字符串1,2,3变成表里的行数据 方法:用自定义函数实现 /* 获取字符串数组的 Table */ from sysobjects where id = object_id('Get_StrA ...
- mssql-在一个特定的会话停止出发器
用SET CONTEXT_INFO来实现 --在某个会话里设置 SET CONTEXT_INFO 0x8888 --在触发器里判断 ) SELECT @Cinfo = Context_Info() 原 ...
- linux基础part2
linux基础 一.linux基础命令 1.pwd:用来显示当前目录位置 2.cd:用来切换目录位置.(eg:cd...cd../...cd-.cd~) 3.ls:用来查看目录或文件信息(eg:ls ...
- MyBatis:学习笔记(4)——动态SQL
MyBatis:学习笔记(4)——动态SQL
- c++学习笔记(网上资料)
C++笔记 2007-3-22 1. 程序 —— 可执行文件,人发送给计算机的一组指令. 硬件指令是二进制, ...