http://write.blog.csdn.net/postedit

A Star not a Tree?
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3751   Accepted: 1858

Description

Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you to connect any number of computers together in a linear arrangement. Luke is particulary proud that he solved a nasty NP-complete
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

The first line of input contains a positive integer N <= 100, the number of computers. N lines follow; each gives the (x,y) coordinates (in mm.) of a computer within the room. All coordinates are integers between 0 and 10,000.

Output

Output consists of one number, the total length of the cable segments, rounded to the nearest mm.

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)的更多相关文章

  1. [模拟退火][UVA10228] A Star not a Tree?

    好的,在h^ovny的安利下做了此题 模拟退火中的大水题,想当年联赛的时候都差点打了退火,正解貌似是三分套三分,我记得上一道三分套三分的题我就是退火水过去的... 貌似B班在讲退火这个大玄学... 这 ...

  2. poj-2420 A Star not a Tree?(模拟退火算法)

    题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepte ...

  3. poj2420 A Star not a Tree? 找费马点 模拟退火

    题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...

  4. uva 10228 - Star not a Tree?(模拟退火)

    题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...

  5. 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 ...

  6. 初探 模拟退火算法 POJ2420 HDU1109

    模拟退火算法来源于固体退火原理,更多的化学物理公式等等这里不再废话,我们直接这么来看 模拟退火算法简而言之就是一种暴力搜索算法,用来在一定概率下查找全局最优解 找的过程和固体退火原理有所联系,一般来讲 ...

  7. POJ 2420:A Star not a Tree?

    原文链接:https://www.dreamwings.cn/poj2420/2838.html A Star not a Tree? Time Limit: 1000MS   Memory Limi ...

  8. [POJ 2420] A Star not a Tree?

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4058   Accepted: 200 ...

  9. POJ 2420 A Star not a Tree? (计算几何-费马点)

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3435   Accepted: 172 ...

随机推荐

  1. [Cxf] spring-cxf 配置

    1.在web.xml中配置servlet <!-- spring监听的配置 --> <listener> <listener-class>org.springfra ...

  2. Xshell和SecureCRT等SSH下使用Tmux及Byobu(解决Byobu被statusline信息面板刷屏问题)

    Vim的vsplit用得爽吧!多命令行模式,同样让你爽得不蛋疼! 下面介绍一下两个终端多控制台软件:Tmux 和 Byobu!本文还是以Xshell为主进行介绍! --------------Tmux ...

  3. (转)MFC鼠标单击消息拦截双击消息

    如果LButtonDown和LButtonDblClk同时有实现的话 总会实现单击消息,在网上找解决方法,思想是在单击消息实现中取时间,计算两次单击事件的时间差 来回尝试修改,最后成这个样子,还算简单 ...

  4. php对gzip的使用(开启)

    gzip是一种压缩算法,在网络通信过程中,经常用到gzip压缩算法.比如一个文本文件,大小为100M,使用gzip压缩之后,大小可能会变成几M.在网络传输过程中,传10M和传100M,消耗的时间和带宽 ...

  5. 调用外部 DLL 中的函数(1. 早绑定)

    ,b,t,);end; end.

  6. Eclipse导入android项目包xml报错未生成R文件

    最近很是头痛的就是项目带回家做的时候.导入各种问题.自从升级23以后. 生成的带appcompat_v7包.copy时不论是新建还是导入这个包,项目都会报错. 网上的方法试了各种clean各种fix等 ...

  7. Fast-RCNN

    后面框架回归和分类都放到了神经网络里 测试速度提升100倍 训练10

  8. [spring] 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾

    org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 26 in XML document from ...

  9. opencv移植到ubuntu

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ OpenCV 2.2以后版本需要使用Cmake生成makefile文件,因此需要先安装cmake ...

  10. JavaScript jQuery 笔记

    资料来源:http://www.w3school.com.cn/jquery/index.asp http://files.cnblogs.com/files/defineconst/jQuery.r ...