首页 > 其他 > 详细

hdoj-1162-Eddy's picture

时间:2015-03-28 10:12:37      阅读:174      评论:0      收藏:0      [点我收藏+]

Eddy‘s picture

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


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
 

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n,uv,root[110];
double mini;
struct Point{
	double x,y;
}point[110];
struct G{
	int u,v;  //第i 个点 与 第 j 个点的距离  
	double dis; 
}graph[5050];
double hyp(Point a,Point b){
	return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
int find(int i){
	if(root[i]!=i)
	return find(root[i]);
	return i;
}
void unio(int i,int j){
	if(i<=j)
		root[i]=j;
	else
	  root[j]=i;
}
int cmp(G a,G b){
	return a.dis<b.dis;
}
double kruskal(){
	sort(graph,graph+uv,cmp);
	/*for(int i=0;i<uv;++i){
		printf("%.2lf,",graph[i].dis);
	}
	printf("***********\n");*/
	mini=0;
	for(int i=0;i<uv;++i){
		int t,k;
		t=find(graph[i].u);
		k=find(graph[i].v);
		if(t!=k){
			mini=mini+graph[i].dis;
			unio(t,k);
		}
	}
	return mini;
}
int main(){
	//int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;++i){
			scanf("%lf%lf",&point[i].x,&point[i].y);
		  root[i]=i;
		}
		uv=0;
		for(int i=0;i<n;++i){
			for(int j=i+1;j<n;++j){
			    graph[uv].u=i,graph[uv].v=j;
			    graph[uv].dis=hyp(point[i],point[j]);
			    uv++;
			}
		}
		//kruskal();
		printf("%.2lf\n",kruskal());
	}
	return 0;
}


hdoj-1162-Eddy's picture

原文:http://blog.csdn.net/qq_18062811/article/details/44698527

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!