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;
    }
  • 相关阅读:
    java 反编译和文档生成器
    java 导入自定义类
    hust 1010 最短循环节点
    KMP算法
    hiho 第119周 最大权闭合子图
    2007 Asia
    hiho 第1周 最长回文子串
    hiho 第118周 网络流四·最小路径覆盖
    hiho 第117周 二分图多重匹配,网络流解决
    成为一名JAVA高级工程师你需要学什么
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6143066.html
Copyright © 2011-2022 走看看