hdu 1162 Eddy's picture(最小生成树,基础)
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <malloc.h> #include<stdlib.h> #include<algorithm> #include<iostream> #include<math.h> using namespace std; #define M 110 #define inf 999999999 double mat[M][M]; struct tt { double x,y; }dian[M]; double prim(int n,int sta) { double minn,sum=0,dis[M]; int mark[M],i,j,flag; for(i=0;i<n;i++) { dis[i]=mat[sta][i]; mark[i]=0; } mark[sta]=1; for(i=1;i<n;i++) { minn=inf; for(j=0;j<n;j++) { if(mark[j]==0&&dis[j]<minn) { minn=dis[j]; flag=j; } } mark[flag]=1; sum+=dis[flag]; for(j=0;j<n;j++) { if(dis[j]>mat[flag][j]) dis[j]=mat[flag][j]; } } return sum; } int main() { int n,m,i,j; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) { scanf("%lf%lf",&dian[i].x,&dian[i].y); for(j=0;j<i;j++) { mat[i][j]=mat[j][i]=sqrt((dian[i].x-dian[j].x)*(dian[i].x-dian[j].x)+(dian[i].y-dian[j].y)*(dian[i].y-dian[j].y)); } } printf("%.2lf\n",prim(n,0)); } return 0; }
一道又一道,好高兴!