模拟退火算法A Star not a Tree?(poj2420)

时间:2023-03-09 05:46:46
模拟退火算法A Star not a Tree?(poj2420)

http://write.blog.****.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;
}