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,以边作为对象,每次找最短的边加入#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct edge { int s,e; double l; }p[10005]; double x[105],y[105]; int n,fa[10005],t; bool cmp(edge a,edge b) { return a.l<b.l; } void init() { int i; for(i=1;i<=t;i++) fa[i]=i; } int find(int x) { while(fa[x]!=x) x=fa[x]; return fa[x]; } double kru() { double ans=0; int a,b,i; init(); for(i=1;i<=t;i++) { a=find(p[i].s),b=find(p[i].e); if(a!=b) { fa[b]=a; ans+=p[i].l; } } return ans; } int main() { int i,j; double l; while(~scanf("%d",&n)) { t=0; for(i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]); for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) { p[++t].s=i,p[t].e=j; p[t].l=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } sort(p+1,p+t+1,cmp); printf("%.2f\n",kru()); } return 0; }
prim算法版:#include<cstdio> #include<cstring> #include<cmath> #define inf 0x3f3f3f3f double map[105][105],dis[105]; int n; bool vis[105]; struct node { double x,y; }p[105]; double prim() { int i,j,k; double min,ans=0; memset(vis,0,sizeof vis); for(i=1;i<=n;i++) dis[i]=map[1][i]; vis[1]=1; for(i=1;i<n;i++) { min=inf; for(j=1;j<=n;j++) { if(!vis[j]&&min>dis[j]) { min=dis[j]; k=j; } } ans+=min; vis[k]=1; for(j=1;j<=n;j++) { if(dis[j]>map[k][j]) dis[j]=map[k][j]; } } return ans; } int main() { int i,j; double l; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { l=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)); map[i][j]=map[j][i]=l; } printf("%.2f\n",prim()); } return 0; }