标签:
Input
Output
Sample Input
Sample Output
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int pre[10000],x[10000],y[10000]; struct inf{ int u,v; double x; }a[10000]; int cmp(inf a,inf b){ return a.x<b.x; } double f(int x1,int x2, int y1, int y2){ return sqrt((double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))); } int find(int i){ return pre[i] == i?pre[i] : find(pre[i]); } int main(){ int T; scanf("%d", &T); while(T--){ int k,i,j,num = 1,sum = 0; double ans = 0; scanf("%d", &k); for(int i =1; i <= k; i++){ scanf("%d%d", &x[i],&y[i]); } for(int i =1; i < k; i++){ for(j = i+1;j <=k;j++){ double temp; temp = f(x[i],x[j],y[i],y[j]); if(temp>= 10.0&&temp<=1000.0){ a[num].u = i; a[num].v = j; a[num].x = temp; num++; } } } for(int i =1; i <=num;i++) pre[i] = i; sort(a+1,a+num,cmp); for(int i = 1;i <num;i++){ int x = find(a[i].u); int y = find(a[i].v); if(x!=y){ sum++; pre[x] = y; ans +=a[i].x; } } if(sum == k -1) printf("%.1lf\n",ans*100); else printf("oh!\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4322111.html