POJ 2420 模拟退火
来源:互联网 发布:儿童手机保护软件 编辑:程序博客网 时间:2024/09/21 09:25
最近再刷模拟退火,发现这是一个nb的算法。
刚开始只能最一些简单的题。
题目:求到所有点距离和的最小值,随机化步长。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define NUM 30
#define R 10000
#define pi acos(-1.0)
const int N = 100+10;
int n;
struct point
{
double x;double y;
double dis;
point() {}
point(double xx,double yy):x(xx),y(yy){}
}p[N],pp[N];
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double getDis(point a)
{
double ans = 0;
for(int i=1;i<=n;i++)
ans+=dist(a,p[i]);
return ans;
}
void init()
{
for(int i=1;i<=NUM;i++)
{
int tx = rand()%R;
int ty = rand()%R;
pp[i].x = tx;
pp[i].y = ty;
pp[i].dis = getDis(pp[i]);
}
}
int isIn(double x,double y)
{
if(x<0 || x>R ||y<0 ||y>R)
return 0;
return 1;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
init();
double T = 1e5;
double Tmin = 1e-3;
double r = 0.9;
while(T > Tmin)
{
for(int i=1;i<=NUM;i++)
{
double x = pp[i].x , y = pp[i].y;
for(int j=1;j<=20;j++)
{
double angle = (rand()%1000)*1.0/1000*2.0*pi;
double tx = x + T*cos(angle);
double ty = y + T*sin(angle);
if(!isIn(tx,ty)) continue;
point tmp(tx,ty);
double dis = getDis(tmp);
tmp.dis = dis;
if(dis < pp[i].dis)
pp[i] = tmp;
}
}
T*=r;
}
double ans = 1e10;
for(int i=1;i<=NUM;i++)
ans = min(ans,pp[i].dis);
printf("%d\n",(int)(ans+0.5));
return 0;
}
- POJ 2420 模拟退火
- poj 2420 模拟退火
- POJ 2420 模拟退火求费马点
- poj 2420 & poj1379 模拟退火
- 模拟退火算法 POJ 2420
- 模拟退火 (poj 2420, poj 2069)
- 【模拟退火】POJ 2420/ZOJ 1901 费马点
- POJ 2420 二分法 模拟退火算法
- POJ 2420 模拟退火 解题报告
- 模拟退火 poj 1379
- poj 1379 模拟退火
- poj 3285 模拟退火,
- POJ 1379 模拟退火
- POJ 1379模拟退火
- POJ 1379 模拟退火算法
- POJ 2420 A Star not a Tree? 费马点,模拟退火
- poj 2420 A Star not a Tree? 模拟退火
- poj 2420 A Star not a Tree?(模拟退火求费马点)
- CocoaPods安装和使用教程
- 交通换乘GIS地图应用解决方案
- 对二维数据进行边界拓展
- 产品经理做市场调研和数据分析的方法
- 委托模式
- POJ 2420 模拟退火
- mysql 连接字符串与SQL不同
- Java学习笔记(八)
- python 文件大小显示kb、mb或gb等
- 怎么把多张图片转换成为一个PDF文件
- QTableView内容对齐实现方法
- typedef用法总结
- ORACLE 中 WITH AS 的用法
- ..js获取refer搜索关键词