zoukankan      html  css  js  c++  java
  • HDU 1162 Prim

    Eddy's picture
    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7287 Accepted Submission(s): 3689


    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


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int inf=0x3fffffff;
    double cost[107][107];
    double ans;
    int n;
    struct node
    {
        double x,y;
    };
    node e[10007];
    bool used[107];
    double dis[107];
    void prim()
    {
        for(int i=1;i<=n;i++)
        {
            dis[i]=inf;
            used[i]=0;
        }
        dis[1]=0;
        while(1){
            int v=-1;
            for(int i=1;i<=n;i++)
            {
                if(!used[i]&&(v==-1||dis[i]<dis[v])) v=i;
            }
            if(v==-1) break;
            used[v]=1;
            for(int i=1;i<=n;i++)
            {
                if(!used[i]) dis[i]=min(dis[i],cost[v][i]);
            }
        }
       // for(int i=1;i<=n;i++)
          //  printf("%.2lf ",dis[i]);
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d",&n)){
               memset(e,0,sizeof(e));
               for(int i=0;i<=n;i++)
                 for(int j=0;j<=n;j++)
                   if(j==i) cost[i][j]=0;
                   else cost[i][j]=inf;
                for(int i=1;i<=n;i++) scanf("%lf%lf",&e[i].x,&e[i].y);
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=n;j++)
                {
                    cost[i][j]=cost[j][i]=sqrt((e[i].x-e[j].x)*(e[i].x-e[j].x)+(e[i].y-e[j].y)*(e[i].y-e[j].y));
                }
               prim();
                ans=inf;
                double sum=0;
                for(int i=1;i<=n;i++)
                {
                    sum+=dis[i];
                }
                printf("%.2lf
    ",sum);
        }
        return 0;
    }
    
    



  • 相关阅读:
    php单元测试标注(注解)
    python数字图像处理(1):环境安装与配置
    python实现身份证识别
    python tensorflow 学习
    python tensorflow 安装
    python 微信跳一跳进阶
    python 微信跳一跳和源码解读
    C#保存登录用户名供其他页面调用
    C#解析复杂的Json成Dictionary<key,value>并保存到数据库(多方法解析Json 四)
    C# JavaScriptSerializer 解析Json数据(多方法解析Json 三)
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254379.html
Copyright © 2011-2022 走看看