杭电1162 Eddy's picture 最小生成树

来源:互联网 发布:重庆时时彩倍投软件 编辑:程序博客网 时间:2024/06/06 08:31

Eddy's picture

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


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
 

Sample Output
3.41
自己正儿八经的写了一次最小生成树,感觉收获挺大,有时候心里明白当时写不出来,只有自己多写才是王道啊,看的刘汝佳的书上的代码,用的并查集优化了下。贴下自己过的代码。不足敬请指正,本人新手·····
思路:把点转成线段,线段包含长度和起始位置终止位置,对线段长度进行排序,然后从最小的长度开始添加,每次添加线段判断是否成环,如果不成环修改祖先.
#include <iostream>  #include <cstdio>  #include <cmath>  #include <vector>  #include <cstring>  #include <algorithm>  #include <string>  #include <set>  #include <functional>  #include <numeric>  #include <sstream>  #include <stack>  #include <map>  #include <queue>    #define CL(arr, val)    memset(arr, val, sizeof(arr))    #define lc l,m,rt<<1  #define rc m + 1,r,rt<<1|1  #define pi acos(-1.0)  #define ll __int64  #define L(x)    (x) << 1  #define R(x)    (x) << 1 | 1  #define MID(l, r)   (l + r) >> 1  #define Min(x, y)   (x) < (y) ? (x) : (y)  #define Max(x, y)   (x) < (y) ? (y) : (x)  #define E(x)        (1 << (x))  #define iabs(x)     (x) < 0 ? -(x) : (x)  #define OUT(x)  printf("%I64d\n", x)  #define lowbit(x)   (x)&(-x)  #define Read()  freopen("input.txt", "r", stdin)  #define Write() freopen("output.txt", "w", stdout);      #define M 10007  #define N 2007    using namespace std;struct lnode{double x,y;};struct point{double st,ed,len;};lnode node[10001];point pot[10001];int p[10001];double dis(lnode a,lnode b){return sqrt(abs(a.x-b.x)*abs(a.x-b.x)+abs(a.y-b.y)*abs(a.y-b.y));}
//用并查集实现环的判断int find(int x){return x==p[x]?x:p[x]=find(p[x]);}bool cmp(point a,point b){if(a.len<b.len)return true;return false;}int main(){int t;while(cin>>t){int count=0,total=0;double ans=0.0;for(int i=0;i<t;i++)cin>>node[i].x>>node[i].y;for(int i=0;i<t;i++)for(int j=i+1;j<t;j++){int n=count++;pot[n].len=dis(node[i],node[j]);pot[n].st=i;pot[n].ed=j;}for(int i=0;i<count;i++)p[i]=i;sort(pot,pot+count,cmp);for(int i=0;i<count;i++){int x=find(pot[i].st);int y=find(pot[i].ed);if(x!=y){ans+=pot[i].len;total++;p[x]=y;}}printf("%.2f\n",ans);}//system("pause");}
原创粉丝点击