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

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

  • 相关阅读:
    OAuth2.0 基础概述
    Ubuntu安装Gogs服务
    ASP.NET WebAPI 生成帮助文档与使用Swagger服务测试
    ASP.NET MVC 中的路由
    升级Ghost
    搭建Golang开发环境
    TDD并不是看上去的那么美
    .NET Framework 源码查看与调试
    在 ASP.NET MVC 中使用异步控制器
    SpringMVC+FreeMarker+Mybatis 整合
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2820340.html
Copyright © 2011-2022 走看看