杭电--1162--Eddy's picture--并查集
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5178 Accepted Submission(s): 2566
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 易做图, 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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int father[111111];
double s;
struct ssss
{
double a,b;
}ss[111];
struct dddd
{
int a,b;
double x;
}dd[5000];
int Find(int a)
{
return a==father[a]?a:father[a]=Find(father[a]);
}
void Union(int i)
{
int a=Find(dd[i].a),b=Find(dd[i].b);
if(a!=b)father[a]=b,s+=dd[i].x; //只有没有连通的才能进行这一步,所以每次都是需要连通的最短距离
}
bool cmp(const dddd &a,const dddd &b)
{
return a.x<b.x;
}
int main (void)
{
int n,i,j,k,l;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>ss[i].a>>ss[i].b;
for(i=0;i<111111;i++)father[i]=i; //初始化
for(i=l=0;i<n;i++)
for(j=i+1;j<n;j++)
{
dd[l].a=i,dd[l].b=j;
double x=ss[i].a-ss[j].a,y=ss[i].b-ss[j].b;
dd[l++].x=sqrt(x*x+y*y); //老规矩,把距离存两点一起
}
sort(dd,dd+l,cmp);
for(i=0,s=0;i<l;i++)
Union(i); //并起来
printf("%.2f\n",s);
}
return 0;
}
补充:软件开发 , C++ ,