http://acm.hdu.edu.cn/showproblem.php?pid=1162

最小生成树,水题。。。。。
#include<iostream>
#define N 105
#define M 9999999.0
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)
#include<string.h>
#include<cstdio>
#include<cmath>
using namespace std;
double map[N][N];
bool visit[N];
double  dist[N];
int n;
struct point{double x,y;
       }aa[N];

inline double distan(point& a,point& b )
{      return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); }



void prim()
{ FOR(i,1,n)
  {  dist[i]=M;
      visit[i]=true;
  }
  int now=1;
   dist[now]=0.0;
   visit[now]=false;
   double ans=0.0;
   FOR(i,1,n)
   {  ans+=dist[now];
     FOR(j,1,n)
     if(visit[j]&&dist[j]>map[now][j])
         dist[j]=map[now][j];
         double minx=M;
         FOR(j,1,n)
         if(visit[j]&&minx>dist[j])
          minx=dist[now=j];
          visit[now]=false;
         }
         printf("%.2lf\n",ans);
     }
     int main()
     {   while(cin>>n)
         {  memset(visit,true,sizeof(visit));
         
            for(int i=1;i<=n;++i)
              for(int j=1;j<=n;++j)
              {       if(i!=j) map[i][j]=M;
                       dist[i]=M;
              }
            for(int i=1;i<=n;++i)
              cin>>aa[i].x>>aa[i].y;
              for(int i=1;i<n;++i)
                for(int j=i+1;j<=n;++j)
                  {     double c=distan(aa[i],aa[j]) ;
                         if(map[i][j]>c)
                          map[i][j]=map[j][i]=c;
                  }
                       prim();
                       
                      }return 0;
         }


你可能感兴趣的:(http://acm.hdu.edu.cn/showproblem.php?pid=1162)