zoukankan      html  css  js  c++  java
  • Problem B

    Problem Description
    Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
    Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?

    Input
    The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

    Input contains multiple test cases. Process to the end of file.

    Output
    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.

    Sample Input
    3 1.0 1.0 2.0 2.0 2.0 4.0

    Sample Output
    3.41
    题意:在一个平面上给你几个点,让你求用线段将这几个点连起来,需要的最小长度;
    解题思路:这里要想用最短路的话,父亲数组里必须做一个替换,第n个点的父亲数组值对应n,这样才能进行kruskal;
    感悟:在电阅一气呵成,0ms过的感觉真是爽啊;
    代码:
    #include
    #define N 110
    using namespace std;
    struct node
    {
        double f,e,w;//点的坐标,距离
    };
    bool comp(node a,node b)
    {
        return a.w
    }
    node fr[N*N];
    int bin[N];

    double dis(double a,double b,double c,double d)
    {
        return sqrt((a-c)*(a-c)+(b-d)*(b-d));
    }
    int find1(int x)
    {
        int r=x;
        while(bin[r]!=r)
            r=bin[r];
        return r;
    }
    double kruskal(int n,int m)
    {
        sort(fr,fr+m,comp);
        for(int i=0;i<=n;i++)
            bin[i]=i;
        double s=0;
        for(int i=0;i
        {
            int fx=find1(fr[i].f);
            int fy=find1(fr[i].e);
            if(fx!=fy)
            {
                bin[fx]=fy;
                s+=fr[i].w;
            }
        }
        return s;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n;
        double m[N][N];
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;i++)
                scanf("%lf%lf",&m[i][0],&m[i][1]);
            int t=0;
            for(int i=1;i<=n;i++)
                for(int j=i+1;j<=n;j++)
                {
                    fr[t].f=i;
                    fr[t].e=j;
                    fr[t++].w=dis(m[i][0],m[i][1],m[j][0],m[j][1]);
                }
            double ans=kruskal(n,t);
            printf("%.2f ",ans);
        }
        return 0;
    }

  • 相关阅读:
    JVM调优方法笔记
    JVM调优方法笔记
    JavaScript实现选择排序
    自动安装带nginx_upstream_check_module模块的Nginx脚本
    自动安装带nginx_upstream_check_module模块的Nginx脚本
    自动安装带nginx_upstream_check_module模块的Nginx脚本
    简单的文件上传html+ashx
    【转】建构修正版 Casper 协议
    【转】为什么去中心化兑换协议很重要
    【转】当我们说“区块链是无需信任的”,我们的意思是
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5781528.html
Copyright © 2011-2022 走看看