Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19483 Accepted Submission(s): 6103
2 2 10 10 20 20 3 1 1 2 2 1000 1000
1414.2 oh!
<span style="font-size:12px;">#include<cstdio> #include<cmath> #include<algorithm> using namespace std; int set[110]; double x[110],y[110];//记录岛屿的坐标 struct node { int s,e; double w; }row[10010];//结构体储存两岛屿间的信息,岛屿数最大100,结构体数组应不小于10000 double cmp(node a,node b) { return a.w<b.w; } int find(int x) { int r=x,t; while(r!=set[r]) r=set[r]; while(r!=x) { t=set[x]; set[x]=r; x=t; } return r; } int merge(int a,int b) { int fa,fb; fa=find(a); fb=find(b); if(fa!=fb) { set[fa]=fb; return 1; } return 0; } int main() { int t,n,i,j,k; double sum,d; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) { set[i]=i; scanf("%lf%lf",&x[i],&y[i]);//输入坐标就对岛屿进行了编号 } k=0; for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { d=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));//计算岛屿间距离 if(d>=10&&d<=1000)//将满足建桥条件的两个岛屿记录入结构体数组 { row[k].s=i; row[k].e=j; row[k].w=d; k++; } } } sort(row,row+k,cmp); sum=0; for(i=0;i<k;i++) { if(merge(row[i].s,row[i].e)) sum+=row[i].w; } int count=0; for(i=1;i<=n;i++) { if(set[i]==i) count++;//count大于1表示还有岛屿没畅通 } if(count>1) printf("oh!\n"); else printf("%.1lf\n",sum*100); } return 0; } </span>