Each set of data is first an integer C (C <= 100), representing the number of islands, followed by C coordinates, representing the coordinates of each island, which are all integers of 0 <= x, y <= 1000.
Output Each group of input data output a row, representing the minimum cost of building the bridge, the result retains a decimal. If the project can not be achieved to achieve full unimpeded, output "oh!". Sample Input
2 2 10 10 20 20 3 1 1 2 2 1000 1000Sample Output
1414.2 oh!
#include<stdio.h> #include<math.h> #Include < stdlib.h >//qsort function header file int p[10000]; typedef struct { int x; int y; double date; }node; node a[10000],b[10000];//a is used to store the coordinates of each point, b is used to store the distance between no two points and the ordinal number of those two points. int find(int x) { return p[x]!=x?p[x]=find(p[x]):x; } int he(int a,int b) { if(find(a)==find(b)) return 0; else { int x; int y; x=find(a); y=find(b); p[x]=find(y); return 1; } } int cmp( const void *a ,const void *b)//Part of qsort function { node *aa = (node *)a; node *bb = (node *)b; return aa->date > bb->date ? 1 : -1; } int main() { int n; scanf("%d",&n); while(n--) { node t; double sum=0; int count=0; int k=0; int i,j; int m; scanf("%d",&m); for(i=0;i<m;i++) p[i]=i; for(i=0;i<m;i++) scanf("%d %d",&a[i].x,&a[i].y); for(i=0;i<m;i++) for(j=0;j<m;j++) { b[k].x=i; b[k].y=j; b[k].date=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y)); k++; } qsort(b,k-1,sizeof(b[0]),cmp); for(i=0;i<k;i++) { if(b[i].date>=10&&b[i].date<=1000&&he(b[i].x,b[i].y)) { count++; sum+=b[i].date; } if(count==m-1)//The number after the establishment is equal to the number of points - 1 break; } if(count==m-1) printf("%.1lf\n",sum*100); else printf("oh!\n"); } return 0; }