题目
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<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int MAX=105;
struct point {
double x;
double y;
} po [MAX];
struct link {
int x;
int y;
double d;
};
int c[MAX];
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y*b.y)*(a.y*b.y));
}
int find(int a)
{
while(c[a]!=a)
a=c[a];
return a;
}
void merg(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
c[a]=b;
}
bool cmp(link a,link b)
{
return a.d<b.d;
}
int main()
{
//freopen("date.in","r",stdin);
//freopen("date.out","w",stdout);
int n,x,y,i,j,res;
link tem;
vector<link> dis;
while(scanf("%d",&n)) {
res=0;
dis.clear();
for(i=1; i<=n; i++) {
scanf("%lf%lf",&po[i].x,&po[i].y);
for(j=i-1; j>=1; j--) {
tem.x=i;
tem.y=j;
tem.d=dist(po[i],po[j]);
dis.push_back(tem);
}
}
vector<link>::iterator b=dis.begin();
vector<link>::iterator e=dis.end();
sort(b,e,cmp);
for(; b!=e; b++) {
if(find(b->x)!=find(b->y)) {
res+=(b->d);
merg(b->x,b->y);
}
}
printf("%.2lf\n",res);
}
}