模拟退火算法A Star not a Tree?(poj2420)
http://write.blog.csdn.net/postedit
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3751 | Accepted: 1858 | 
Description
problem in order to minimize the total cable length.
Unfortunately, Luke cannot use his existing cabling. The 100mbs system uses 100baseT (twisted pair) cables. Each 100baseT cable connects only two devices: either two network cards or a network card and a hub. (A hub is an electronic device that interconnects
several cables.) Luke has a choice: He can buy 2N-2 network cards and connect his N computers together by inserting one or more cards into each computer and connecting them all together. Or he can buy N network cards and a hub and connect each of his N computers
to the hub. The first approach would require that Luke configure his operating system to forward network traffic. However, with the installation of Winux 2007.2, Luke discovered that network forwarding no longer worked. He couldn't figure out how to re-enable
forwarding, and he had never heard of Prim or Kruskal, so he settled on the second approach: N network cards and a hub.
Luke lives in a loft and so is prepared to run the cables and place the hub anywhere. But he won't move his computers. He wants to minimize the total length of cable he must buy.
Input
Output
Sample Input
4
0 0
0 10000
10000 10000
10000 0
Sample Output
28284
题意:给出n个电脑的坐标,然后找出一个hub的位置,使hub到每个电脑的距离之和最小,输出最小值;
程序:
方法一:
#include"string.h"
#include"stdio.h"
#include"queue"
#include"stack"
#include"vector"
#include"algorithm"
#include"iostream"
#include"math.h"
#include"stdlib.h"
#define M 222
#define inf 100000000000
#define eps 1e-10
#define PI acos(-1.0)
using namespace std;
struct node
{
double x,y,dis;
}p[M],q[M];
int n;
double X1,X2,Y1,Y2;
double min(double a,double b)
{
return a<b?a:b;
}
double max(double a,double b)
{
return a>b?a:b;
}
double pow(double x)
{
return x*x;
}
double len(double x1,double y1,double x,double y)
{
return sqrt(pow(x1-x)+pow(y1-y));
}
double fun(double x,double y)
{
double ans=0;
for(int i=1;i<=n;i++)
ans+=len(x,y,p[i].x,p[i].y);
return ans;
}
void solve()
{
int i,j,po=20,est=25;
for(i=1;i<=po;i++)
{
q[i].x=(rand()%1000+10)/1000.0*(X2-X1)+X1;
q[i].y=(rand()%1000+10)/1000.0*(Y2-Y1)+Y1;
q[i].dis=fun(q[i].x,q[i].y);
}
double temp=len(X1,Y1,X2,Y2);
while(temp>eps)
{
for(i=1;i<=po;i++)
{
for(j=1;j<=est;j++)
{
double rad=(rand()%1000+10)/1000.0*PI*10;
node now;
now.x=q[i].x+temp*cos(rad);
now.y=q[i].y+temp*sin(rad);
if(now.x<0||now.y<0||now.x>10000||now.y>10000)continue;
now.dis=fun(now.x,now.y);
if(now.dis<q[i].dis)
q[i]=now;
}
}
temp*=0.9;
}
int id=1;
for(i=1;i<=po;i++)
{
if(q[i].dis<q[id].dis)
id=i;
}
printf("%.0lf\n",q[id].dis); }
int main()
{
int i;
while(scanf("%d",&n)!=-1)
{
X1=Y1=10000;
X2=Y2=0;
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
X1=min(X1,p[i].x);
X2=max(X2,p[i].x);
Y1=min(Y1,p[i].y);
Y2=max(Y2,p[i].y);
}
solve();
}
}
方法二:
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#include"vector"
#include"queue"
#include"map"
#include"string"
#define M 10009
#define Maxm 10000
#define INF 10000000000000000LL
#define inf 100000000
#define eps 1e-5
#define pps 1e-8
#define PI acos(-1.0)
#define LL __int64
using namespace std;
struct node
{
double x,y;
node(){}
node(double xx,double yy){x=xx;y=yy;}
node operator-(node a)
{
return node(x-a.x,y-a.y);
}
node operator+(node a)
{
return node(x+a.x,y+a.y);
}
double operator ^(node a)
{
return x*a.y-y*a.x;
}
double operator *(node a)
{
return x*a.x+y*a.y;
}
}p[M];
int n;
double maxi;
node ret;
double len(node a)
{
return sqrt(a*a);
}
double dis(node a,node b)
{
return len(b-a);
}
double cross(node a,node b,node c)
{
return (b-a)^(c-a);
}
double fun(node q)
{
double sum=0;
for(int i=1;i<=n;i++)
sum+=dis(q,p[i]);
if(maxi>sum)
{
maxi=sum;
ret=q;
}
return sum;
}
void SA()
{
double temp=10000.0;
node now=ret;
maxi=INF;
while(temp>0.0001)
{
double rad=(rand()%1000)/1000.0*PI*10;
node cur;
cur.x=now.x+temp*cos(rad);
cur.y=now.y+temp*sin(rad);
double pe=fun(now)-fun(cur);
if(pe>0)
now=cur;
temp*=0.98;
}
for(int i=1;i<=1000;i++)
{
double rad=(rand()%1000)/1000.0*PI*10;
node cur;
cur.x=now.x+temp*cos(rad);
cur.y=now.y+temp*sin(rad);
fun(cur);
}
printf("%.0lf\n",fun(ret));
}
int main()
{
int i;
while(scanf("%d",&n)!=-1)
{
ret=node(0,0);
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
ret.x+=p[i].x;
ret.y+=p[i].y;
}
ret.x/=n;
ret.y/=n;
SA();
}
return 0;
}
模拟退火算法A Star not a Tree?(poj2420)的更多相关文章
- [模拟退火][UVA10228] A Star not a Tree?
		
好的,在h^ovny的安利下做了此题 模拟退火中的大水题,想当年联赛的时候都差点打了退火,正解貌似是三分套三分,我记得上一道三分套三分的题我就是退火水过去的... 貌似B班在讲退火这个大玄学... 这 ...
 - poj-2420 A Star not a Tree?(模拟退火算法)
		
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
 - poj2420  A Star not a Tree?    找费马点          模拟退火
		
题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
 - uva 10228 - Star not a Tree?(模拟退火)
		
题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...
 - 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 ...
 - 初探 模拟退火算法 POJ2420 HDU1109
		
模拟退火算法来源于固体退火原理,更多的化学物理公式等等这里不再废话,我们直接这么来看 模拟退火算法简而言之就是一种暴力搜索算法,用来在一定概率下查找全局最优解 找的过程和固体退火原理有所联系,一般来讲 ...
 - 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?
		
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4058 Accepted: 200 ...
 - POJ 2420 A Star not a Tree? (计算几何-费马点)
		
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3435 Accepted: 172 ...
 
随机推荐
- webstorm软件使用记录
			
右边的那条线的去除:setting-editor-appearance-show right margin 勾选去掉
 - C# 窗体显示避免抢夺焦点
			
通过调用API进行显示可以避免抢夺焦点的问题 以下是API调用 using System.Runtime.InteropServices; [DllImport("user32.dll&qu ...
 - resize2fs: Bad magic number in super-block while trying to open
			
I am trying to resize a logical volume on CentOS7 but am running into the following error: resize2fs ...
 - while(scanf("%d",&n)!=EOF)与while(cin>>n)
			
我们知道scanf函数是C语言里面的,其返回值是,被输入函数成功赋值的变量个数.针对于int counts = scanf("%d",&n);来说如果赋值成功那么其返回值 ...
 - c#后台常用知识
			
生成如:2015-10-25T12:12:12格式的时间 DateTime.Now.ToString("s") 非asp.net mvc环境下对url编码 (HttpUtility ...
 - LazyValue<T>
			
public void ExtendFuncT() { //():匿名无参方法.() =>方法名,指派匿名无参方法去执行另外一个方法. LazyValue<int> lazyOne ...
 - C# GetType和typeof的区别
			
typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...
 - 机器学习技法之Aggregation方法总结:Blending、Learning(Bagging、AdaBoost、Decision Tree)及其aggregation of aggregation
			
本文主要基于台大林轩田老师的机器学习技法课程中关于使用融合(aggregation)方法获得更好性能的g的一个总结.包含从静态的融合方法blending(已经有了一堆的g,通过uniform:voti ...
 - Spring----Spring Boot Rest的使用方法
			
1.下载Google浏览器并安装插件 转载: http://chromecj.com/web-development/2015-03/401/download.html 打开Google浏览器-> ...
 - Extjs学习笔记--(二)
			
1.配置实用Extjs <link href="Extjs/resources/css/ext-all.css" rel="stylesheet" /&g ...