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

Eddy‘s picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8107    Accepted Submission(s): 4106

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

又一道模板题,最小生成树的克鲁斯卡尔算法,只是普利姆算法还不是很会用,就一直用克鲁斯卡尔了,不过普利姆算法可以处理大数据,这一点想必是极好的:

附ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node
{
	int start;
	int end;
	double dis;
}t[10010];
int cmp(node a,node b)
{
	return a.dis <b.dis ;
}
int per[110];
int find(int x)
{
	int r=x;
	while(r!=per[r])
	r=per[r];
	return r;
}
int join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		return 1;
	}
	return 0;
}
int main()
{
	int m,n,i,j,p;
	double x[110],y[110];
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<110;i++)
		per[i]=i;
		for(i=0;i<n;i++)
		scanf("%lf%lf",&x[i],&y[i]);
		int k=0;
		for(i=0;i<n;i++)
		for(j=i+1;j<n;j++)
		{
			t[k].start=i;
			t[k].end =j;
			t[k].dis =sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
			k++;
		}
		sort(t,t+k,cmp);
		double sum=0.00;
		for(i=0;i<k;i++)
		if(join(t[i].start,t[i].end))
		sum=sum+t[i].dis;
		printf("%.2lf\n",sum);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

时间: 2024-10-10 22:46:35

杭电1162Eddy's picture(最小生成树)的相关文章

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b

杭电1162Eddy&#39;s picture

Eddy's picture Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 28   Accepted Submission(s) : 17 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Eddy begins to like  p

杭电3371--Connect the Cities(最小生成树)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 RT: 一直超时, 网上也找不到好方法, 不能忍的是同样的写法他们能过!! Prim() 998ms卡过去了, 严重怀疑杭电判题有问题: ac: #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int INF = 0x3f3f3f3f; int map[

杭电ACM1301——Jungle Roads~~最小生成树

这题,简单的最小生成树问题. 只是输入的时候比较麻烦,一开始的N,是村庄的个数,下面N - 1 条信息,一开始的大写字母S和数K,是S村庄有K条路连接,后面是K个村庄以及权值. 处理好了输入的数据,就很简单了. 下面的是AC的代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; class data { pu

杭电1301--Jungle Roads(最小生成树)

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5379    Accepted Submission(s): 3884 Problem Description  The Head Elder of the tropical island of Lagrishan has a problem. A burst o

杭电ACM1162——Eddy&#39;s picture~~最小生成树

这一题,就是简单的最小生成树的应用.开始,没有想到用最小生成树做,想到的是贪心,当知道了用最小生成树做的时候,还犯了一个很严重的错误,就是时间复杂度的估计错了,导致开始不敢写,在想其他的办法.当作一次教训吧. 下面是AC的代码,有详细的注释,用的是并查集来判环,时间复杂度为nlogn,主要时间在排序上. #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> us

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

杭电 1030 Delta-wave

Delta-wave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5447    Accepted Submission(s): 2063 Problem Description A triangle field is numbered with successive integers in the way shown on the