zoukankan      html  css  js  c++  java
  • poj2420 A Star not a Tree? 找费马点 模拟退火

    题目传送门

    题目大意:

      给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小。

    思路:

           模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码里设置的,那么T就和我设置的一样就可以了,但此时初始点如果稍微改动一下(比如横坐标加一),T就必须再增加一些才可以(我试了一下2*T才可以),所以初始点的选取也很重要。

    #include<cstdio>
    #include<cstring>
    #include<stdlib.h>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<map>
    #define CLR(a,b) memset(a,b,sizeof(a))
    #define PI acos(-1)
    using namespace std;
    typedef long long ll;
    const int inf=0x3f3f3f3f;
    const int maxn=1000010;
    int n;
    double ans;
    struct node{
        double x,y;
        node (){}
        node (double _x, double _y):x(_x),y(_y){}
        bool operator +=(const node t){
            x=x+t.x,y=y+t.y;
        }
    }p[110],now;
    inline double Rand(){
        return (rand()%1000+1)/1000.0;
    }
    inline double getdist(double x,double y){
        double ret = 0;
        for(int i=1; i<=n; ++i){
            ret += sqrt((p[i].x-x)*(p[i].x-x)*1.0 + (p[i].y-y)*(p[i].y-y)*1.0);
        }
        if(ret < ans) ans = ret;
        //printf("debug:%f
    ",ret);
        return ret;
    }
    
    
    inline void fire(){
        double T=100000,alpha,sub;
        double eps=1e-3;
        while(T>eps)
        {
            alpha =2.0*PI*Rand();
            node tmp(now.x+T*cos(alpha),now.y+T*sin(alpha));
            sub=getdist(now.x,now.y)-getdist(tmp.x,tmp.y);
            if(sub>=0||exp(sub/T)>=Rand())now=tmp;
            T*=0.99;
        }
    
    }
    int main(){
        cin>>n;
        srand(100233);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            now+=p[i];
        }
        now.x/=n,now.y/=n;
        ans=inf;
        fire();
        printf("%.f
    ",ans);
    }
    A Star not a Tree?
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9519   Accepted: 4089

    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
  • 相关阅读:
    k-means算法
    偏差-方差分解Bias-Variance Decomposition
    常见machine learning模型实现
    Bag-of-words模型、TF-IDF模型
    atomic原子操作
    oc 计算 带括号 式子
    oc 基本语法 类 静态变量 常量
    通过文件头标识判断图片格式
    十大经典排序算法最强总结(含JAVA代码实现)(转)
    JPEG格式 介绍
  • 原文地址:https://www.cnblogs.com/mountaink/p/9832690.html
Copyright © 2011-2022 走看看