标签:
#include<iostream> #include<stdio.h> #include<algorithm> #include<math.h> using namespace std; int par[103]; struct Island { int x,y; } isl[103]; struct Node { int isl1,isl2; double dis; } rode[100*99/2]; int find(int a) { if(a!=par[a]) { par[a]=find(par[a]); } return par[a]; } void build(int a,int b) { int fa=find(a); int fb=find(b); if(fa!=fb) { par[fb]=fa; } } bool cmp(Node a,Node b) { return a.dis<b.dis; } int main() { int t; scanf("%d",&t); while(t--) { int c; scanf("%d",&c); if(c == 1 || c == 0) {printf("oh!\n"); continue;} for(int i=0; i<c; i++) par[i]=i; int num=c*(c-1)/2; for(int i=0; i<c; i++) { scanf("%d%d",&isl[i].x,&isl[i].y); } int t=0; for(int i=0; i<c; i++) { for(int j=i+1; j<c; j++) { double mid=double((isl[i].x-isl[j].x)*(isl[i].x-isl[j].x)+(isl[i].y-isl[j].y)*(isl[i].y-isl[j].y)); if(mid<100||mid>1000000) continue; else { rode[t].isl1=i; rode[t].isl2=j; rode[t].dis=sqrt(mid); t++; } } } //cout<<t<<" num " <<num; double ans=0.0; int cnt=0; sort(rode,rode+t,cmp); for(int i=0;i<t;i++) { if(find(rode[i].isl1)!=find(rode[i].isl2)) { ans+=rode[i].dis; build(rode[i].isl1,rode[i].isl2); cnt++; } } ans*=100; if(cnt!=c-1) printf("oh!\n"); else printf("%.1lf\n",ans); } return 0; }
Description
Input
Output
Sample Input
Sample Output
标签:
原文地址:http://www.cnblogs.com/superxuezhazha/p/5334110.html