hdu 1162 Eddy's picture

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

 最小生成树的题目,还是keuskal

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAX=1000000;
struct node
{
    int left,right;
    double cost;
}road[MAX];

bool cmp(node x,node y){return x.cost<y.cost;}

int p[MAX],m,n;

int find(int x){return x==p[x]? x:p[x]=find(p[x]);}

double kruskal()
{
    double ans=0.0;
    int k=0;
    for (int i=0;i<=n;i++) p[i]=i;
    for (int i=0;i<m;i++)
    {
        int x=find(road[i].left);
        int y=find(road[i].right);
        if(x!=y)
        {
            ans+=road[i].cost;
            p[x]=y;
            k++;
        }
        if (k==n-1) break;
    }
    if (k==n-1)return ans;
    else return -1;
}

int main()
{
    //int n;
    double x[101],y[101];
    while (scanf("%d",&n)!=EOF)
    {
        for (int i=1;i<=n;i++)scanf("%lf%lf",&x[i],&y[i]);
        m=0;
        for (int i=1;i<=n;i++)
        for (int j=1;j<i;j++)
        {
                road[m].left=i;
                road[m].right=j;
                road[m].cost=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
                m++;
        }
        sort(road,road+m,cmp);
        printf("%.2lf\n",kruskal());
    }
    return 0;
}

 

posted @ 2014-05-10 07:56  Hust_BaoJia  阅读(107)  评论(0编辑  收藏  举报
努力