HDU1162 最小生成树 kruskal

来源:互联网 发布:是 什么是大数据概念 编辑:程序博客网 时间:2024/05/18 18:18

Eddy's picture

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

31.0 1.02.0 2.02.0 4.0
# include<stdio.h># include<math.h># include<string.h># include<algorithm>using namespace std;typedef struct{  double x, y;} Point;       //定义一个点(x, y)typedef struct{    int x, y;double z;} Edge;       //定义关系Edge,表示第x个点与第y个点连成的边长度为zEdge a[10005];bool cmp(Edge x, Edge y){    return x.z<y.z;}int pre[10005]; //用来存放pre[i]个点的老大,老大相同说明它们相连着Point b[105]; //输入的点int Find(int t)       //第t个点{    if(t==pre[t]) return pre[t]; //如果它的老大是自己,直接返回自己    pre[t]=Find(pre[t]);  //否则就向上找老大,并把中间高级些的人直接指向最终的老大    return pre[t]; //找到最终老大,返回}     void Join(int x, int y) //第x个点 和第y个点{    x=Find(x); //x的老大    y=Find(y);//y的老大    if(x!=y)//x, y的老大不相等    pre[x]=y;//让y的老大成为x的老大, 也就是让第x个点和第y个点相连}int main(void){    int i, j, n, s;//用s来标记关系Edge数组a的下标double t;double ans;//存放输出结果    while(scanf("%d", &n)!=EOF)  //输入n    {s=1;        for(i=1; i<=n; i++)      //输入n个点 scanf("%lf%lf", &b[i].x, &b[i].y);for(i=1; i<n; i++) //第i个点和第j个点的Edge关系, 用s来标记第几个关系for(j=i+1; j<=n; j++){   a[s].x=i;//第i个点               a[s].y=j;//第j个点   t=sqrt((b[i].x-b[j].x)*(b[i].x-b[j].x)+(b[i].y-b[j].y)*(b[i].y-b[j].y));   a[s].z=t;//第i个点与第j个点的距离   s++;//s加1}        for (i=1;i<=n;i++)        {            pre[i]=i;      //初始化,一开始每个点都只和自己相连,它的老大就是自己        }        ans=0;//ans初始为0        sort(a, a+s, cmp);// 将a数组按照 距离由小到大排序 , 贪心的思想,由小到大        for(i=1; i<s; i++)        {            if(Find(a[i].x)==Find(a[i].y)) continue; //如果a[i]中的第x个点和第y个点老大相等,也就是它们相连了,continue            Join(a[i].x, a[i].y);//否则就把它们相连,(让它们的老大相等)            ans+=a[i].z;  //相连了就加上这条边        }        printf("%.2lf\n",ans);    }    return 0;}




原创粉丝点击