• 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;
    }
  • 相关阅读:
    python——函数
    python——文件操作
    python——字符编码
    【转】基于jquery的无刷新表格分页
    js console.log 打印 对像 数组 详解
    百度地图自己添加 标识地点 代码
    在线提取PDF中图片和文字
    安全cookie登录状态设计方案
    iScroll 下拉刷新
    WebSQL实例记录
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3377434.html
Copyright © 2020-2023  润新知