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. jQuery实现布局高宽自适应

    在页面布局(layout)时经常是上左右的框架布局并且需要宽.高度的自适应,div+css是无法实现(*hegz:div+css其实是可以实现的,利用jQuery比较容易实现浏览器的兼容性),所以需要 ...

  2. 创建ajax对象并兼容多个浏览器方法简单记录

    这篇文章主要介绍了如何创建ajax对象并兼容多个浏览器,需要的朋友可以参考下<script> function createAjax(){ var request=false; //win ...

  3. php插入htm htm插入php的变量

    <?php $file = "ueditor\php\upload\image\*\*.png"; foreach (glob("$file") as $ ...

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

  5. Unity3D - 详解Quaternion类(二)

    OK,不做引子了,接上篇Unity3D - 详解Quaternion类(一)走起! 四.Quaternion类静态方法 Quaternion中的静态方法有9个即:Angle方法.Dot方法.Euler ...

  6. 【Java NIO的深入研究1】缓冲区

    缓冲区 传统的流和通道的对比 流 通道 慢 快 处理简单 处理复杂 单字节的传输 一块数据的传输 - Java.io.*已经重新写过 - 是对流的模拟 单向的 双向的 可直接访问 必须通过Buffer ...

  7. openal 基础知识2

    三枚举扩展包(enumeration extension,“ALC_ENUMERATION_EXT”) 开发者可以通过这个extension得到一个字符串列表,区分不同的渲染/捕捉设备.OpenALr ...

  8. js中的var

    vars变量预解析 JavaScript中,你可以在函数的任何位置声明多个var语句,并且它们就好像是在函数顶部声明一样发挥作用,这种行为称为 hoisting(悬置/置顶解析/预解析).当你使用了一 ...

  9. Unity3d导出安卓版本

    1. 要想导出安卓版,就必须要安装安卓 SDK,这个可以去这里下载. http://developer.android.com/sdk/index.html.  当我们打开后就是看见这个了. 2.当我 ...

  10. 落实制度靠流程<摘自平安50万人的执行力>

     落实制度靠流程<摘自平安50万人的执行力> 讲在嘴上的制度是给人听的,写在纸上的制度是给人看的,落实在流程上的制度才是可靠的.制度的执行不能都依赖个人的自觉性. 很多企业都在强调和推行制 ...