1. 程式人生 > >POJ 2420 模擬退火 解題報告

POJ 2420 模擬退火 解題報告

A Star not a Tree?

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

【解題報告】
模擬退火求費馬點是非常經典的問題了,如果覺得模擬退火這個名字比較高階難以理解的話們可以把它叫做“隨機化變步長貪心”。

據說某位大神說過:爬山演算法就是一隻兔子看到一座山峰,然後跳來跳去最後跳上山頂,模擬退火就是一隻喝醉的兔子,一開始亂跳,過一會酒醒了,然後再跳上山頂——黃學長

程式碼如下:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
using namespace std;

int n;
double xx,yy,ans,t;
struct point
{
    double x,y;
}p[105];

double dis(double x,double y,point p)
{
    return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
double getsum(double x,double y)
{
    double tmp=0;
    for(int i=1;i<=n;++i)
        tmp+=dis(x,y,p[i]);
    return tmp;
}
int main()
{
//  srand(time(0));
    while(scanf("%d",&n)!=EOF)
    {
        xx=yy=0,ans=1e20,t=100000;
        for(int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            xx+=p[i].x,yy+=p[i].y;
        }
        xx/=n;yy/=n;
        ans=getsum(xx,yy);
        double tmp,x,y;
        while(t>0.02)
        {
            x=y=0;
            for(int i=1;i<=n;++i)
            {
                x+=(p[i].x-xx)/dis(xx,yy,p[i]);
                y+=(p[i].y-yy)/dis(xx,yy,p[i]);
            }
            tmp=getsum(xx+x*t,yy+y*t);
            if(tmp<ans)
            {
                ans=tmp;
                xx+=x*t,yy+=y*t;
            }
            else if(log((tmp-ans)/t)<(rand()%10000)/10000.0)
            {
                ans=tmp;
                xx+=x*t,yy+=y*t;
            }       
            t*=0.9; 
        }
        printf("%.0lf\n",ans);
    }
    return 0;
}

優化過隨機函式後,0ms。。。

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
using namespace std;

int n;
double xx,yy,ans,t;
struct point
{
    double x,y;
}p[105];

inline int q_rand()
{
    static int seed=10007; 
    return seed=int(seed*48271LL%2147483647);
}
inline double dis(double x,double y,point p)
{
    return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
inline double getsum(double x,double y)
{
    double tmp=0;
    for(int i=1;i<=n;++i)
        tmp+=dis(x,y,p[i]);
    return tmp;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        xx=yy=0,ans=1e20,t=100000;
        for(int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            xx+=p[i].x,yy+=p[i].y;
        }
        xx/=n;yy/=n;
        ans=getsum(xx,yy);
        double tmp,x,y;
        while(t>0.02)
        {
            x=y=0;
            for(int i=1;i<=n;++i)
            {
                x+=(p[i].x-xx)/dis(xx,yy,p[i]);
                y+=(p[i].y-yy)/dis(xx,yy,p[i]);
            }
            tmp=getsum(xx+x*t,yy+y*t);
            if(tmp<ans)
            {
                ans=tmp;
                xx+=x*t,yy+=y*t;
            }
            else if(log((tmp-ans)/t)<(q_rand()%10000)/10000.0)
            {
                ans=tmp;
                xx+=x*t,yy+=y*t;
            }       
            t*=0.9; 
        }
        printf("%.0lf\n",ans);
    }
    return 0;
}