zoukankan      html  css  js  c++  java
  • HDU 1162 Eddy's picture

    Eddy's picture

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


    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
     
    思路:简单Kruskal
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    int n;
    int the_last_flag;
    double the_last_sum;
    int map[110];
    double pppp[110][2];
    struct Node
    {
        int first,end;
        double value;
        bool select;
    }Edge[10010];
    int find(int x)
    {
        while(x != map[x])
        {
            x = map[x];
        }
        return x;
    }
    void Merge(int x,int y)
    {
        int p = find(x);
        int q = find(y);
        if(p > q)
           map[q] = p;
        else
           map[p] = q;
    }
    double position(double x1,double y1,double x2,double y2)
    {
        return sqrt(pow(x1 - x2,2) + pow(y1 - y2,2));
    }
    int cmp(Node a,Node b)
    {
        if(a.value != b.value)
            return a.value < b.value;
        if(a.first != b.first)
            return a.first < b.first;
        return a.end < b.end;
    }
    void Kruskal(Node *Edge,int n,int m)
    {
        the_last_flag = 0;
        sort(Edge + 1,Edge + m + 1,cmp);
        for(int i = 1;i <= m;i ++)
        {
            if(the_last_flag == n - 1)
                 break ;
            int x = find(Edge[i].first);
            int y = find(Edge[i].end);
            if(x != y)
            {
                Merge(x,y);
                the_last_flag ++;
                Edge[i].select = true;
            }
        }
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            for(int i = 1;i <= n;i ++)
               map[i] = i;
            int pos = 0;
            memset(pppp,0,sizeof(pppp));
            for(int i = 1;i <= n;i ++)
               scanf("%lf%lf",&pppp[i][0],&pppp[i][1]);
            for(int i = 1;i <= n;i ++)
              for(int j = i + 1;j <= n;j ++)
              {
                  Edge[++ pos].first = i;
                  Edge[pos].end = j;
                  Edge[pos].select = false;
                  Edge[pos].value = position(pppp[i][0],pppp[i][1],pppp[j][0],pppp[j][1]);
              }
            ///for(int i = 1;i <= pos;i ++)
            //{
                //printf("%d %d %lf
    ",Edge[i].first,Edge[i].end,Edge[i].value);
            //}
            Kruskal(Edge,n,pos);
            the_last_sum = 0;
            for(int i = 1;i <= pos;i ++)
            {
                if(Edge[i].select == 1)
                   the_last_sum += Edge[i].value;
            }
            printf("%.2lf
    ",the_last_sum);
        }
        return 0;
    }
  • 相关阅读:
    LCA --算法竞赛专题解析(29)
    倍增与ST算法 --算法竞赛专题解析(28)
    可持久化线段树(主席树) --算法竞赛专题解析(27)
    莫队算法 --算法竞赛专题解析(26)
    分块 --算法竞赛专题解析(25)
    表格标题或内容平铺样式
    SpringMVC传参
    按字节截取字符串
    Redis常用命令及知识
    修改数据库字段类型或名字
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3377434.html
Copyright © 2011-2022 走看看