zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Fiddler 的几个用法
    jQuery中ajax的使用和缓存问题解决 $getjson 与$get都会被IE缓存
    http与https的区别以及https的加密原理
    asp.net mvc通过预处理实现数据过滤和数据篡改。
    C# winform的WebBrowser非常规编程(强烈推荐)
    DES加密 java与.net可以相互加密解密的方法
    日常开发中常见的HTTP协议的状态码
    前端性能监控
    在手机的浏览器上通过连接打开App
    工作五年总结一二三流公司的常见特性
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6143066.html
Copyright © 2011-2022 走看看