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;
    }
    
    



  • 相关阅读:
    银联测试
    mysql 往表中某个字段的字符串后追加字符串
    jsp通过js往后端传文字时乱码问题的解决
    artTemplate 如何遍历数据
    Error:java: Annotation processing is not supported for module cycles. Please ensure that all modules from cycle [kstore_goods_platform,kstore_goods,kstore_custom] are excluded from annotation processi
    递归方式实现二分查找
    递归与二分查找
    python内置函数
    函数的四种传参方式
    python基础(四)
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254379.html
Copyright © 2011-2022 走看看