现在的位置: 首页 > 综合 > 正文

hdu 1162 Eddy’s picture

2018年05月02日 ⁄ 综合 ⁄ 共 2394字 ⁄ 字号 评论关闭

Eddy's picture

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

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
/*题意:
用并查集求最短路问题,题中给出了坐标,是求将个坐标上的点连通的最短距离。 
*/ 
 
// =====================================================================================
// 
//       Filename:  Eddy's picture.cpp
//    Description:  Eddy's picture
//      Algorithm:  Disjoint Set
//         Status: 	RunTime:0ms 	RunMemory:1936KB 
//        Version:  Dev-C++ 5.5.3 
//        Created:  2014/9/7 9:58 
//       Revision:  none
//       Compiler:  GUN C++
//         Author:  Tip of the finger melody, 1466989448@qq.com
//        Company:  none
//
// =====================================================================================
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int pre[100010];
int find(int x)//查 
{
	return x==pre[x]?x:pre[x]=find(pre[x]);
}
int join(int x,int y)//并
{ 
	int fx=find(x),fy=find(y);
	if(fx==fy) return 0;
	if(fx!=fy)
	{
		pre[fx]=fy;//将fx作为fy的根节点 	
	}
	return 1;
}
struct dege
{
	int a;
	int b;
	double l;
}e[100010];
int cmp(dege a,dege b)
{
	return a.l<b.l;
}
int main()
{
	int n,i,j,k;
	double a[100010],b[100010],dis,sum;
	while(scanf("%d",&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for(i=1; i<=n; i++)
			scanf("%lf %lf",&a[i],&b[i]);//注意这里是lf
		for(i=1,k=1; i<=n-1; i++)
		{
			for(j=i+1; j<=n; j++)
			{
				pre[k] = k;
				e[k].a = i;
				e[k].b = j;
				e[k++].l = sqrt((b[j]-b[i])*(b[j]-b[i])+(a[j]-a[i])*(a[j]-a[i]));
			}
		}
		k--;//注意这里一定要k-- 
		sort(e+1,e+k+1,cmp);
		for(i=1,sum=0.0; i<=k; i++)
		{
			if(join(e[i].a,e[i].b))
			{
				sum+=e[i].l;
			}
		}
		printf("%.2lf\n",sum);
	}
}

【上篇】
【下篇】

抱歉!评论已关闭.