• Eddy's picture(最小生成树)


    Eddy's picture

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 228 Accepted Submission(s): 168
     
    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
     
    Author
    eddy
     
     
    Recommend
    JGShining
     
    #include<bits/stdc++.h>
    #define N 110
    using namespace std;
    struct node
    {
        int u,v;
        double val;
        node(){}
        node(int a,int b,double c)
        {
            u=a;
            v=b;
            val=c;
        }
        bool operator <(const node & a) const
        {
            return val<a.val;
        }
    };
    struct point
    {
        double x,y;
        point(){}
        point(double a,double b)
        {
            x=a;
            y=b;
        }
    };
    vector<node>edge;
    vector<point>p;
    int bin[N];
    double dis(point a,point b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    int findx(int x)
    {
        while(bin[x]!=x)
            x=bin[x];
        return x;
    }
    int n;
    double x,y;
    void init()
    {
        for(int i=0;i<=n;i++)
            bin[i]=i;
    }
    int main()
    {
        //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        while(scanf("%d",&n)!=EOF)
        {
            edge.clear();
            p.clear();
            init();
            for(int i=0;i<n;i++)
            {
                scanf("%lf%lf",&x,&y);
                p.push_back(point(x,y));
            }
            for(int i=0;i<p.size();i++)
            {
                for(int j=i+1;j<p.size();j++)
                {
                    edge.push_back(node(i,j,dis(p[i],p[j])));
                }
            }
            double cur=0;
            sort(edge.begin(),edge.end());
            for(int i=0;i<edge.size();i++)
            {
                int fx=findx(edge[i].u);
                int fy=findx(edge[i].v);
                if(fx!=fy)
                {
                    cur+=edge[i].val;
                    bin[fy]=fx;
                }
            }
            printf("%.2lf
    ",cur);
        }
        return 0;
    }
  • 相关阅读:
    react项目建立导入包问题总结
    React中创建组件的3种方式
    export default与export的区别
    vue中的过滤器
    git clone https://chromium.googlesource.com/失败
    typescript 实现函数重载
    Rename a local and remote branch in git
    使用typescript开发react应用
    自己实现一个Promise库
    【翻译】Webpack 4 从0配置到生产模式
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6143066.html
Copyright © 2020-2023  润新知