zoukankan      html  css  js  c++  java
  • 模拟退火 费马点求解

    A Star not a Tree?

    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
    

    模拟退火就是求点的坐标的

    增量数组移动 逐渐退火

    code:

    //
    #include<cmath>
    #include<iostream>
    #include<cstdio>
    #define maxnn 200
    using namespace std;
    struct node
    {
        double x,y;
    }res[maxnn];
    int n;
    double eps=1e-9;
    double t=100;
    double delta=0.98;
    #define INF 1e9
    int dx[5]={1,-1,0,0};
    int dy[5]={0,0,1,-1};
    double dist(node a,node b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    double check(node a,int n)
    {
        double ans=0;
        while(n--)
        {
            ans+=dist(a,res[n]);
        }
        return ans;
    }
    double solve(node res[])
    {
        node s=res[0];
        double ans=INF;
        node now;
        double t=100;
        int fla=0;
        while(t>eps)
        {
            fla=1;
            while(fla)
            {    
                fla=0;
                for(int i=0;i<4;i++)
                {
                    now.x=s.x+dx[i]*t;
                    now.y=s.y+dy[i]*t;
                        if(ans>check(now,n))
                        {
                            ans=check(now,n);//更新答案
                            s=now;
                            fla=1;
                        }
                }  //判断是否更新了解 若未更新就不继续递增
            }
            t=t*delta; //退火
        }
        return ans;
    }
    int main()
    {
        while(cin>>n)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%lf%lf",&res[i].x,&res[i].y);
            }
            printf("%.0f",solve(res));
        }
    }
    刀剑映出了战士的心。而我的心,漆黑且残破
  • 相关阅读:
    ARM汇编指令
    Linux系统里如何彻底的清空终端屏幕?
    Linux命令(16)压缩,解压文件
    Linux命令(18)查看当前用户who、whoami、who am i
    Linux命令(17)du 查看文件和目录磁盘使用情况
    Mongodb(3)插入文档,更新文档,删除文档
    Mongodb(2)创建数据库,删除数据库,创建集合,删除集合,显示文档内容
    Mongodb(1)如何存储以及简介
    Linux命令(15)查看系统版本信息
    Python 结巴分词(2)关键字提取
  • 原文地址:https://www.cnblogs.com/OIEREDSION/p/11290404.html
Copyright © 2011-2022 走看看