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 ...
随机推荐
- c++的检测的确比C++更严格
见下面代码 #include <stdio.h> #include <stdlib.h> #include <time.h> enum guess { paper, ...
- 《Java线程池》:任务拒绝策略
在没有分析线程池原理之前先来分析下为什么有任务拒绝的情况发生. 这里先假设一个前提:线程池有一个任务队列,用于缓存所有待处理的任务,正在处理的任务将从任务队列中移除.因此在任务队列长度有限的情况下就会 ...
- 1.Python学习---helloworld
1.首先访问http://www.python.org/download/去下载最新的python版本. 2.安装下载包,一路next. 3.为计算机添加安装目录搭到环境变量,如图把python的安装 ...
- Flex中容器的完全隐藏
在html中,但我们设置某个dom元素的display属性为none时,dom元素不可见且其占用空间从dom树上隐藏,而在Flex中,但我们把某个组件的visible属性visible设为false的 ...
- python3.7.1 内置函数
python3.7.1 内置函数列表 内置函数 abs() delattr() hash() memoryview() set() all() dict() help() min() setattr( ...
- Jmeter使用文档(windows)
1. 安装jdk并配置环境变量 以1.8为例: (1)安装jdk1.8; (2)在系统变量里点击新建,变量名填写JAVA_HOME,变量值填写JDK的安装路径“C:\Program Files\Jav ...
- java入门了解12
1.SequenceInputStream序列流:能将其他输入流的串联 用处:读完第一个再去读第二个输入流 用法:构造方法:SequenceInputStream(InputStream s1,Inp ...
- 算法(Algorithms)第4版 练习 1.5.24
package com.qiusongde; import edu.princeton.cs.algs4.StdOut; public class Exercise1524 { public stat ...
- JSP&EL 内置对象
JSP&EL 内置对象 转载▼ 具体的JSP和El中的内置对象见下表,由于我写在了excel中,也不知道怎么把excel发出来,就截了图. 相关问题: Q1: JSP:EL中 pageCo ...
- Spark- Transformation实战
RDD的算子分为两类,是 Trans formation(Lazy),一类是 Action(触发任务执行RDD不存在真正要计算的数据,而是记录了RDD的转换关系(调用了什么方法,传入什么函数) RDD ...