题目:
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
分析与解答
题意:就是给你几个点的坐标,让你求一下每个点都经过的最短距离。我们知道点坐标,就可以把两点之间的距离求出来,此时我们可以看作给了两点和一个边,求最短路,这就转化成连通工程那一道题。我写这个突然发现我把那个kruskal算法给忘了
先上一个kruskal模板
int pre[505];
struct data
{int x,y;double len;
} a[25005];
void init()
{for(int i=0; i<505; i++)pre[i]=i;
}
int Find(int x)
{if(pre[x]!=x)return pre[x]=Find(pre[x]);return x;
}
void Merge(int x,int y)
{int X=Find(x);int Y=Find(y);if(X!=Y)pre[X]=Y;
}
int cmp(data a,data b)
{return a.len<b.len;
}
//main里面把数据先输入到a里面,输入完之后,
//如果u,v不在一个连通分量里,就把uv连通并且最短路加上他们间的距离
//注意初始化init();sort(a,a+cnt,cmp);double res=0;for(int i=0;i<cnt;i++){int X=Find(a[i].x);int Y=Find(a[i].y);if(X!=Y){pre[X]=Y;res+=a[i].len;}}
代码参考:https://blog.csdn.net/qq_30591245/article/details/77094961
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int pre[505];
struct data
{int x,y;double len;
} a[25005];
struct Point
{double x,y;
}b[25005];
void init()
{for(int i=0; i<505; i++)pre[i]=i;
}
int Find(int x)
{if(pre[x]!=x)return pre[x]=Find(pre[x]);return x;
}
//void Merge(int x,int y)
//{
// int X=Find(x);
// int Y=Find(y);
// if(X!=Y)
// pre[X]=Y;
//}
int cmp(data a,data b)
{return a.len<b.len;
}
double Distance(Point a,Point b)
{return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{int n;while(~scanf("%d",&n)){init();for(int i=1;i<=n;i++){scanf("%lf%lf",&b[i].x,&b[i].y);}int cnt=0;for(int i=1;i<n;i++){for(int j=i+1;j<=n;j++){a[cnt].x=i;a[cnt].y=j;a[cnt].len=Distance(b[i],b[j]);//printf("%f\n",a[cnt].len);++cnt;}}//printf("%d\n",cnt);sort(a,a+cnt,cmp);double res=0;for(int i=0;i<cnt;i++){int X=Find(a[i].x);int Y=Find(a[i].y);if(X!=Y){pre[X]=Y;// printf("%f\n",a[i].len);res+=a[i].len;}}printf("%.2f\n",res);}
}