Eddy's picture(最小生成树)

Posted 勿忘初心0924

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eddy's picture(最小生成树)相关的知识,希望对你有一定的参考价值。

Eddy‘s picture

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 228 Accepted Submission(s): 168
 
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<bits/stdc++.h>
#define N 110
using namespace std;
struct node
{
    int u,v;
    double val;
    node(){}
    node(int a,int b,double c)
    {
        u=a;
        v=b;
        val=c;
    }
    bool operator <(const node & a) const
    {
        return val<a.val;
    }
};
struct point
{
    double x,y;
    point(){}
    point(double a,double b)
    {
        x=a;
        y=b;
    }
};
vector<node>edge;
vector<point>p;
int bin[N];
double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int findx(int x)
{
    while(bin[x]!=x)
        x=bin[x];
    return x;
}
int n;
double x,y;
void init()
{
    for(int i=0;i<=n;i++)
        bin[i]=i;
}
int main()
{
    //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF)
    {
        edge.clear();
        p.clear();
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&x,&y);
            p.push_back(point(x,y));
        }
        for(int i=0;i<p.size();i++)
        {
            for(int j=i+1;j<p.size();j++)
            {
                edge.push_back(node(i,j,dis(p[i],p[j])));
            }
        }
        double cur=0;
        sort(edge.begin(),edge.end());
        for(int i=0;i<edge.size();i++)
        {
            int fx=findx(edge[i].u);
            int fy=findx(edge[i].v);
            if(fx!=fy)
            {
                cur+=edge[i].val;
                bin[fy]=fx;
            }
        }
        printf("%.2lf\n",cur);
    }
    return 0;
}

 

以上是关于Eddy's picture(最小生成树)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1162 Eddy's picture (最小生成树 prim)

HDU 1162 Eddy's picture (最小生成树)(java版)

hdu1162 Eddy's picture 基础最小生成树

hdu 1162 Eddy&#39;s picture (Kruskal算法,prim算法,最小生成树)

Eddy's picture

hdu 1162 Eddy's picture (prim)