Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7097 Accepted Submission(s): 3588
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?
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.
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
题目大意:求用最短的连线把几个点全部连接起来。
介绍下Kruskal最小生成树的算法,其核心是每次取最短的边,看该边相连的两点是否在同一个集合内,若在则跳过,若不在,就把两个点合并,判断与合并都用并查集实现,至于什么是并查集,请大家看我博客中并查集的解析。代码如下:
介绍下Kruskal最小生成树的算法,其核心是每次取最短的边,看该边相连的两点是否在同一个集合内,若在则跳过,若不在,就把两个点合并,判断与合并都用并查集实现,至于什么是并查集,请大家看我博客中并查集的解析。代码如下:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int maxn = 105;
int father[maxn];
struct point
{
double x,y;
}p[maxn];
struct Map
{
int v,e;
double dis;
bool operator <(const struct Map &L) const //运算符重载,由小到大
{
return dis <L.dis;
}
}l[10010];
double cal(point a,point b) //计算权值
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
void made(int n) //并查集的初始化
{
int i;
for(i=1;i<=n;i++)
father[i] = i;
}
int find(int x) //查找父结点
{
if(x != father[x])
{
father[x] = find(father[x]);
}
return father[x];
}
void Union(int x,int y) //两个元素合并成一个集合
{
x = find(x);
y = find(y);
if(x != y)
{
father[x] = y;
}
}
int main()
{
int m,i;
double ans;
while(cin>>m)
{
ans = 0;
for(i=1;i<=m;i++)
{
cin>>p[i].x>>p[i].y;
}
int cnt = 0;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
{
l[cnt].v = i; //初始化边和点
l[cnt].e = j;
l[cnt].dis = cal(p[i],p[j]);
cnt ++;
}
}
sort(l,l+cnt);
made(m);
for(int i=0;i<cnt;i++)
{
if(find(l[i].v) != find(l[i].e)) //如果不在一个集合就合并
{
Union(l[i].v,l[i].e);
ans += l[i].dis; //然后加上权值
}
}
printf("%.2lf\n",ans);
}
}