zoukankan      html  css  js  c++  java
  • POJ 2420 A Star not a Tree? (模拟退火)

    职务地址:POJ 2420

    今天在比赛遇到了这题。。

    于是现场学了一下模拟退火。。。。

    这题是先初始化为一个点,然后不断趋近距离和最短的点。还是挺简单的。

    代码例如以下:

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    #include <queue>
    #include <map>
    #include <set>
    #include <algorithm>
    
    using namespace std;
    #define LL __int64
    const int INF=0x3f3f3f3f;
    int n;
    struct point
    {
        int x, y;
    }dian[200];
    double dist(point a, point b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
    }
    double juhe(point x)
    {
        int i;
        double z=0;
        for(i=0;i<n;i++)
        {
            z+=dist(x,dian[i]);
        }
        return z;
    }
    int main()
    {
        int i, j, flag, step;
        point st, ed;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&dian[i].x,&dian[i].y);
        }
        step=100;
        st.x=0;st.y=0;
        double d=juhe(st), s;
        while(step>0.2)
        {
            flag=1;
            while(flag)
            {
                flag=0;
                point now;
                now.x=st.x+step;
                now.y=st.y;
                s=juhe(now);
                if(d>s)
                {
                    flag=1;
                    d=s;
                    ed=now;
                }
                now.x=st.x-step;
                now.y=st.y;
                s=juhe(now);
                if(d>s)
                {
                    flag=1;
                    d=s;
                    ed=now;
                }
                now.x=st.x;
                now.y=st.y+step;
                s=juhe(now);
                if(d>s)
                {
                    flag=1;
                    d=s;
                    ed=now;
                }
                now.x=st.x;
                now.y=st.y-step;
                s=juhe(now);
                if(d>s)
                {
                    flag=1;
                    d=s;
                    ed=now;
                }
                if(flag)
                    st=ed;
            }
            step/=2.0;
        }
        printf("%.0f
    ",d);
        return 0;
    }
    


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    [JSOI2008]Blue Mary开公司[李超线段树]
    线段树分治
    满汉全席[2-SAT]
    「一本通 3.5 练习 5」和平委员会
    2-SAT问题
    2019/04/06 BJ省选模拟DAY1
    构造题【随时更
    文本编辑器vim/vi——命令模式
    指令——cat
    指令——history
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4810082.html
Copyright © 2011-2022 走看看