传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1875
简单的最小生成树
#include <iostream> #include <cstdio> #include <cmath> using namespace std; const int maxn=10000+5; const double INF=1.0e20; struct Node{ double x,y; } isl[maxn]; bool book[maxn]; double dis[maxn]; double dist(double x1,double y1,double x2,double y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } int main(){ int T; scanf("%d",&T); while(T--){ int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lf%lf",&isl[i].x,&isl[i].y); } for(int i=1;i<=n;i++){ dis[i]=INF; book[i]=false; } dis[1]=0; for(int k=1;k<=n;k++){ int m=-1; double mx=INF; for(int i=1;i<=n;i++){ if(!book[i]&&dis[i]<mx){ m=i; mx=dis[i]; } } if(m==-1) continue; book[m]=true; for(int i=1;i<=n;i++){ if(!book[i]){ double d=dist(isl[m].x,isl[m].y,isl[i].x,isl[i].y); if(d>=10.000000&&d<=1000.000001){ if(d<dis[i]){ dis[i]=d; } } } } } double ans=0; for(int i=1;i<=n;i++){ ans+=dis[i]; if(ans>=INF) break; } int flag=0; for(int i=1;i<=n;i++){ if(book[i]==false){ flag=1; break; } } if(ans>=INF||flag==1){ printf("oh! "); } else{ printf("%.1lf ",ans*100); } } return 0; }