zoukankan      html  css  js  c++  java
  • HDUOJ 1162 Eddy's picture(最小生成树)

    Eddy's picture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3511    Accepted Submission(s): 1722


    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
     

    注意快排对double的实现,由于cmp必须返回int,所以不能return c->len - d->len。

    AC code:

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    
    const int N=100;
    typedef struct coodinate
    {
        int v,u;//两个点
        double len;//v、u间的距离
    }Point;
    Point p[N*(N-1)];
    
    int cmp(const void *a,const void *b)
    {
        Point *c=(Point *)a;
        Point *d=(Point *)b;
        return c->len > d->len ? 1:-1;
    }
    
    int main()
    {
        int i,j,k,t,n,vis[N],m,M;
        double x[N],y[N],min;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=0;i<n;i++)
            {
                scanf("%lf%lf",&x[i],&y[i]);
            }
            min=0.0;
            for(i=0;i<n;i++) vis[i]=i;
            for(i=0,k=0;i<n;i++)
            {
                for(j=i+1;j<n;j++,k++)
                {
                    p[k].v=i;
                    p[k].u=j;
                    p[k].len=pow((pow((x[i]-x[j]),2.0)+pow((y[i]-y[j]),2.0)),0.5);
                }
            }
            qsort(p,k,sizeof(p[0]),cmp);
            for(i=0,j=0;j<n-1;i++)
            {
                if(vis[p[i].v] != vis[p[i].u])
                {
                    min+=p[i].len;
                    j++;
                    //合并集合
                    if(vis[p[i].v]>vis[p[i].u])
                    {
                        M=vis[p[i].v];
                        m=vis[p[i].u];
                    }
                    else
                    {
                        M=vis[p[i].u];
                        m=vis[p[i].v];
                    }
                    for(k=0;k<n;k++)
                    {
                        if(vis[k]==M) vis[k]=m;
                    }
                }
            }
            printf("%.2lf\n",min);
        }
        return 0;
    }
    


  • 相关阅读:
    html5 桌面提醒:Notifycations
    windows 下 apache 的虚拟主机配置
    javascript 跨域
    javascript 类型数组读取二进制数据
    javascript parseInt() 函数的进制转换陷阱
    javascript 中几个与正则表达式相关的应用
    javascript 中的二进制运算
    一段小代码,发布网页时为js 、css 文件加上版本号
    base64 编码及解码
    PHP 的比较运算与逻辑运算
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910515.html
Copyright © 2011-2022 走看看