hdu 1162 Eddy's picture (最小生成树)
2014-05-05 21:02
483 查看
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6339 Accepted Submission(s): 3179
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
[align=left]Sample Input[/align]
3
1.0 1.0
2.0 2.0
2.0 4.0
[align=left]Sample Output[/align]
3.41
[align=left]Author[/align]
eddy
[align=left]Recommend[/align]
JGShining | We have carefully selected several similar problems for you: 1301 1213 1116 1269 1198
最小生成树入门题:
//0MS 384K 1215B G++ #include<stdio.h> #include<stdlib.h> #include<math.h> struct point{ double x,y; }p[105]; struct node{ int u,v; double d; }t[10005]; int set[105],n; int cmp(const void*a,const void*b) { return (*(node*)a).d>(*(node*)b).d?1:-1; } int find(int x) { if(set[x]!=x) set[x]=find(set[x]); return set[x]; } int merge(int a,int b) { int x=find(a); int y=find(b); if(x!=y){ set[x]=y; return 1; } return 0; } double kruskal() { double ans=0; for(int i=0;i<n*n;i++) if(merge(t[i].u,t[i].v)) ans+=t[i].d; return ans; } 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 main(void) { while(scanf("%d",&n)!=EOF) { for(int i=0;i<=n;i++) set[i]=i; for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); int k=0; for(int i=0;i<n;i++) for(int j=0;j<n;j++){ t[k].u=i; t[k].v=j; t[k].d=dis(p[i],p[j]); k++; } qsort(t,n*n,sizeof(t[0]),cmp); printf("%.2lf\n",kruskal()); } return 0; }
相关文章推荐
- HDU1162-Eddy's picture(最小生成树)
- HDU 1162 Eddy's picture (最小生成树)(java版)
- HDU 1162 Eddy's picture(最小生成树)
- hdu 1162 Eddy's picture(最小生成树算法)
- hdu 1162 Eddy's picture (Kruskal算法,prim算法,最小生成树)
- hdu 1162 Eddy's picture (Kruskal算法,prim算法,最小生成树)
- hdu 1162 Eddy's picture(最小生成树Prim算法)
- hdu 1162 Eddy's picture 基础最小生成树★
- hdu 1162 Eddy's picture(最小生成树)
- HDUOJ-----(1162)Eddy's picture(最小生成树)
- HDU 1162 Eddy's picture (Kruscal最小生成树)
- hdu 1162 Eddy's picture(最小生成树,prim)
- HDU 1162 Eddy's picture(最小生成树)
- hdu 1162 Eddy's picture(最小生成树Kruskal)
- hdu 1162 Eddy's picture (Kruskal算法,prim算法,最小生成树)
- hdu 1162 Eddy's picture(最小生成树算法)
- ACM HDU 1162 Eddy's picture (简单最小生成树,开始练习使用模板了)
- hdu 1162 Eddy's picture(基础最小生成树)
- HDU-1875 畅通工程再续-1162 - Eddy's picture(最小生成树,Kruskal 算法实现 )
- hdu1162 Eddy's picture 最小生成树 prim