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



  • 相关阅读:
    Codeforces Round #325D (Div. 2) (DP)
    Codeforces Round #382 (Div. 2) (模拟|数学)
    HDU5950-Recursive sequence(矩阵快速幂)
    9. javacript高级程序设计-客户端检测
    8. javacript高级程序设计-BOM
    7. javacript高级程序设计- 函数表达式
    6. javacript高级程序设计-面向对象设计
    Js注释
    5. javacript高级程序设计-引用类型
    4. javacript高级程序设计-变量、作用域和内存问题
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254379.html
Copyright © 2011-2022 走看看