zoukankan      html  css  js  c++  java
  • HDU ACM 1162 Eddy's picture(MST)

    Eddy's picture

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


    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<stdio.h>
    #include<string.h>
    #include<math.h>
    
    double path[102][102];
    int flag[102];
    double closedge[102];
    double max;
    double cnt;
    
    typedef struct{
        double x, y;
    }input;
    
    input temp[102];
    
    double calculate(double x1, double y1, double x2, double y2)
    {// 两点之间的距离 
        
        return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    }
    
    double CreatMST(int n)
    {
        int i, j, x;
        double k;
        flag[0] = 1;
        for(i=1; i<n; ++i)
        closedge[i] = path[0][i];
        for(i=1; i<n; ++i)
        {
            k = max, x = 1;
            for(j=1; j<n; ++j)
            if(!flag[j] && closedge[j] < k)
                x = j, k = closedge[j];
            flag[x] = 1;
            cnt += k;
            for(j=1; j<n; ++j)
            if(!flag[j] && closedge[j] > path[x][j])
            closedge[j] = path[x][j];
        }
        return cnt;
    }
    
    int main()
    {
        int i, j, k, t, x, y, n, m;
        while(scanf("%d", &n) != EOF)
        {
            max = 0;
            cnt = 0;
            memset(temp, 0, sizeof(input)*102);
            memset(flag, 0, sizeof(flag));
            memset(closedge, 0, sizeof(closedge));
            memset(path, 0, sizeof(double)*102*102);
            for(i=0; i<n; ++i)
            scanf("%lf%lf", &temp[i].x, &temp[i].y);
            
            // 计算N*(N+1)条路径的权重 
            for(i=0; i<n; ++i)
            for(j=0; j<n; ++j)
            {
                path[i][j] = calculate(temp[i].x, temp[i].y, temp[j].x, temp[j].y);
                if(max < path[i][j]) max = path[i][j];
            }
            printf("%.2lf\n", CreatMST(n));
        }
        return 0;
    }

    解题报告:1y

    最小生成树不会叫你就写一个函数就行了。

  • 相关阅读:
    MongoDB驱动之Linq操作
    连接Access数据库
    ExecutorCompletionService原理具体解释
    Java 构造时成员初始化的陷阱
    activeMQ公布订阅模式中中经常使用工具类
    计算机视觉、图像处理一些先进研究机构
    php循环,die/exit脚本执行控制,文件载入及错误控制
    VCenter中嵌套openstack VM不能ping通外部网络问题解决的方法
    代码保存、配色、公布-总体方案----一段代码的公布
    【iOS开发系列】NSObject方法介绍
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2820340.html
Copyright © 2011-2022 走看看