zoukankan      html  css  js  c++  java
  • hdu 1162 Eddy's picture (Kruskal 算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1162

    Eddy's picture

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


    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
     
    题目大意: 给出一些点的坐标,然后连线使得所有顶点都能够连通,并且线的长度最短。
     
    解题思路: 根据题意,可以选用以边为主导的Kruskal算法计算。
     
    AC代码:
    19975116 2017-03-03 08:53:42 Accepted 1162 0MS 1524K 1304 B G++ 
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <algorithm>
    
    using namespace std;
    
    struct point
    {
        int x,y;
        double l;
    }p[11000];
    int parent[110],n;
    double x[110],y[110];
    bool cmp(point a, point b)
    {
        return a.l < b.l;
    }
    int find(int x)
    {
        int s,tmp;
        for (s = x; parent[s] >= 0; s = parent[s]);
        while (s != x)
        {
            tmp = parent[x];
            parent[x] = s;
            x = tmp;
        }
        return s;
    }
    void Union(int A, int B)
    {
        int a = find(A), b = find(B);
        int tmp = parent[a]+parent[b];
        if (parent[a] < parent[b])
        {
            parent[b] = a;
            parent[a] = tmp;
        }
        else
        {
            parent[a] = b;
            parent[b] = tmp;
        }
    } 
    void kruskal(int k)
    {
        double sum = 0;
        int u,v,i,j = 0;
        memset(parent,-1,sizeof(parent));
        for (i = 0; i <= k; i ++)
        {
            u = p[i].x;    v = p[i].y;
            if (find(u) != find(v))
            {
                sum += p[i].l;
                Union(u,v);
                j ++;
            }
            if (j == n-1) break;
        }
        printf("%.2lf
    ",sum);
    }
    int main ()
    {
        int i,j,k;
        while (scanf("%d",&n)!=EOF)
        {
            k = -1;
            for (i = 1; i <= n; i ++)
                scanf("%lf%lf",x+i,y+i);
            for (i = 1; i < n; i ++)
            for (j = 1+i; j <= n; j ++)
            {
                p[++ k].x = i;
                p[k].y = j;
                p[k].l = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
            }
            sort(p,p+k+1,cmp);
            kruskal(k);
        }
        return 0;
    }

    算法理解: http://www.cnblogs.com/yoke/p/6506492.html

  • 相关阅读:
    hdu 4396(spfs/二维最短路)
    hdu 4360(最短路变形)
    hdu 3501(欧拉函数的应用)
    bnu 4359(数位dp)
    用Hermite插值方法估计积分值
    虞旦盛老师 的《数学分析续》课件
    数值积分中的梯形法则
    数值积分中的梯形法则
    辛普森三八律
    利用自然数的标准分解证明可数集合的所有有限子集形成的集合是可数集
  • 原文地址:https://www.cnblogs.com/yoke/p/6506455.html
Copyright © 2011-2022 走看看