zoukankan      html  css  js  c++  java
  • 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/contest/view.action?cid=88808#problem/B

    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

    HINT

    题意

     平面上有100个点,让你找到一个点,使得这些点到这个点的距离和最小

    就是让你找广义费马点

    题解

    爬山算法

    有一个蠢萌兔子,喝醉了,四处走,然后清醒了,就滚回去了,就是这样……

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <bitset>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200051
    #define mod 10007
    #define eps 1e-9
    int Num;
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    
    int n;
    struct node
    {
        double x,y;
    };
    double sqr(double x)
    {
        return x*x;
    }
    double dis(double x,double y,node p)
    {
        return sqrt(sqr(x-p.x)+sqr(y-p.y));
    }
    node p[105];
    double get_sum(double x,double y)
    {
        double ans=0;
        for(int i=0;i<n;i++)
        {
            ans+=dis(x,y,p[i]);
        }
        return ans;
    }
    
    int main()
    {
        n=read();
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        node ans;
        ans.x=0,ans.y=0;
        for(int i=0;i<n;i++)
            ans.x+=p[i].x,ans.y+=p[i].y;
        ans.x=(ans.x/(n*1.0)),ans.y=(ans.y/(n*1.0));
        double Ans = get_sum(ans.x,ans.y);
        double t  = 10000;
        double x,y,tmp;
        while(t>0.02)
        {
            x=0,y=0;
            for(int i=0;i<n;i++)
            {
                x+=(p[i].x-ans.x)/dis(ans.x,ans.y,p[i]);
                y+=(p[i].y-ans.y)/dis(ans.x,ans.y,p[i]);
            }
            tmp = get_sum(ans.x+x*t,ans.y+y*t);
            if(tmp<Ans)
            {
                Ans = tmp;
                ans.x+=x*t;
                ans.y+=y*t;
            }
            t*=0.9;
        }
        printf("%.0lf
    ",Ans);
    }
  • 相关阅读:
    【xsy2506】 bipartite 并查集+线段树
    Linux K8s容器集群技术
    Linux 运维工作中的经典应用ansible(批量管理)Docker容器技术(环境的快速搭建)
    Linux Django项目部署
    Linux Django项目测试
    Linux 首先基本包安装(vim啊什么的),源,源优化,项目架构介绍, (LNMuWsgi)Django项目相关软件mysql,redies,python(相关模块)安装配置测试
    Linux centos系统安装后的基本配置,Linux命令
    Linux 虚拟机上安装linux系统 (ip:子网掩码,网关,dns,交换机,路由知识回顾)
    $ Django 调API的几种方式,django自定义错误响应
    $Django 路飞之显示视频,Redis存购物车数据,优惠卷生成表,优惠卷的一个领取表。(知识小回顾)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4761340.html
Copyright © 2011-2022 走看看