zoukankan      html  css  js  c++  java
  • HDOJ 1162 Eddy's Picture (最小生成树)

    Eddy's picture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8206    Accepted Submission(s): 4164


    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
     
    给出n,然后n个点的坐标,求出将这些点用线全部连接起来最短的路径。先保存下所有点的坐标,然后对于任意不相同的亮点计算下距离加入到图中,因为边的数目比较多,就用prim算法了。
    #include<iostream>
    #include<vector>
    #include<cstdio>
    #define MAXN 105
    #include<cmath>
    using namespace std;
    int m,n;
    const int inf=1<<31-1;
    double point[MAXN][2];
    vector<int>mmap[MAXN];
    double val[MAXN][MAXN];
    int vis[MAXN];
    double  prim()
    {
        double  re=0;
        int k=0;
        double d[MAXN];
        fill(d,d+n+1,inf);
        fill(vis,vis+n+1,0);
        d[1]=0;
        while(true)
        {
            int v=-1;
            for(int i=1;i<=n;i++){
                if(!vis[i]&&(v==-1||d[i]<d[v]))
                    v=i;
            }
           /* if(v==-1||d[v]==inf)
                    return -1;*/
            re+=d[v];
            k++;
            if(k==n)return re;
            vis[v]=1;
            for(int i=0;i<mmap[v].size();i++)
            {
                int x=mmap[v][i];
                if(!vis[x]&&val[v][x]<d[x])
                    d[x]=val[v][x];
            }
    
        }
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            m=0;
            for(int i=1;i<=n;i++)
             {
                 mmap[i].clear();
                 scanf("%lf%lf",&point[i][0],&point[i][1]);
             }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<i;j++)
                {
                    m++;
                    double x1=point[i][0],y1=point[i][1];
                    double x2=point[j][0],y2=point[j][1];
                    mmap[i].push_back(j);
                    mmap[j].push_back(i);
                    val[i][j]=val[j][i]=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    
                }
            }
           printf("%.2lf
    ",prim());
        }
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    浅析TCP /UDP/ IP协议
    大小端模式
    小技巧—计算内存
    浅谈启发式合并
    浅谈换根DP
    POJ 3585 Accumulation Degree
    OSGi类加载问题
    Redis缓存集群方案
    Tair分布式缓存
    Tedis:淘宝的Redis的Java客户端开发包
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768440.html
Copyright © 2011-2022 走看看