• 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

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

  • 相关阅读:
    实现主从关系Form中汇总行金额/数量
    Custom.pll : 客制化菜单
    XML publisher 填充空白行数
    PLSQL提交带有模板的报表的方法
    使用Form个性化修改标准Form的LOV2
    在开发Form表单中的三种查询方法
    S3C2440 I2C实现
    NBOOT 基于VS2005的编程与编译(一)
    WINCE 6.0 调大image config.bib
    少用的defined,注意不是define
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2820340.html
Copyright © 2020-2023  润新知