标签:des style blog color os io java strong for
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15734 Accepted Submission(s): 4872
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <map> 8 #include <cmath> 9 using namespace std; 10 11 struct node{ 12 int x, y; 13 double w; 14 }a[105*105/2]; 15 16 struct mem{ 17 double x, y; 18 }b[105]; 19 20 int father[105]; 21 int f[105]; 22 int n, m; 23 int ma[105][105]; 24 int visited[105]; 25 int dis[105]; 26 int c[105]; 27 28 int findroot1(int p){ 29 int x=p; 30 while(x!=father[x]) x=father[x]; 31 return x; 32 } 33 34 int findroot2(int p){ 35 int x=p; 36 while(x!=f[x]) x=f[x]; 37 return x; 38 } 39 40 bool cmp(node a,node b){ 41 return a.w<b.w; 42 } 43 44 main() 45 { 46 int i, j, k; 47 int t; 48 double d; 49 cin>>t; 50 while(t--){ 51 scanf("%d",&n); 52 k=0; 53 for(i=0;i<=n;i++) {father[i]=i;f[i]=i;c[i]=1;} 54 for(i=0;i<n;i++) scanf("%lf %lf",&b[i].x,&b[i].y); 55 for(i=0;i<n;i++){ 56 for(j=i+1;j<n;j++){ 57 d=sqrt((double)(b[i].x-b[j].x)*(b[i].x-b[j].x)+(double)(b[i].y-b[j].y)*(b[i].y-b[j].y)); 58 if(d>=10&&d<=1000){ 59 a[k].x=i;a[k].y=j; 60 a[k++].w=d*100.0; 61 father[findroot1(j)]=findroot1(i); 62 } 63 } 64 } 65 int num=0; 66 for(i=0;i<n;i++){ 67 if(i==findroot1(i)) 68 num++; 69 } 70 if(num>1) { 71 printf("oh!\n");continue; 72 } 73 sort(a,a+k,cmp); 74 double ans=0;j=1; 75 for(i=0;i<k;i++){ 76 int fx=findroot2(a[i].x); 77 int fy=findroot2(a[i].y); 78 if(fx!=fy){ 79 ans+=a[i].w; 80 f[fy]=fx; 81 c[fx]+=c[fy]; 82 if(c[fx]>=n) break; 83 } 84 } 85 printf("%.1lf\n",ans); 86 } 87 }
标签:des style blog color os io java strong for
原文地址:http://www.cnblogs.com/qq1012662902/p/3961350.html