标签:des style blog http color java os io
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14835 Accepted Submission(s): 4591
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=10000+5000; 8 struct point 9 { 10 int x; 11 int y; 12 }po[MAXN]; 13 14 struct node 15 { 16 int star; 17 int en; 18 double dis; 19 bool operator<(const node&B)const 20 { 21 return dis<B.dis; 22 } 23 }a[MAXN]; 24 int p[MAXN]; 25 int Find(int x) 26 { 27 return p[x]==x?x:p[x]=Find(p[x]); 28 } 29 30 double distans(int x1,int y1,int x2,int y2) 31 { 32 return sqrt((double)(x1-x2)*(x1-x2)+(double)(y1-y2)*(y1-y2)); 33 } 34 35 int main() 36 { 37 //freopen("in.txt","r",stdin); 38 int kase; 39 scanf("%d",&kase); 40 while(kase--) 41 { 42 int n,cnt=1; 43 double ans=0; 44 scanf("%d",&n); 45 for(int i=1;i<=n;i++) 46 scanf("%d %d",&po[i].x,&po[i].y); 47 for(int i=1;i<n;i++) 48 { 49 for(int j=i+1;j<=n;j++) 50 { 51 double temp=distans(po[i].x,po[i].y,po[j].x,po[j].y); 52 if(10.0<=temp&&temp<=1000.0) 53 { 54 a[cnt].star=i; 55 a[cnt].en=j; 56 a[cnt].dis=temp; 57 cnt++; 58 } 59 } 60 } 61 sort(a+1,a+cnt+1); 62 for(int i=1;i<=cnt;i++) 63 p[i]=i; 64 int sum=0; 65 for(int i=1;i<=cnt;i++) 66 { 67 int x=Find(a[i].star); 68 int y=Find(a[i].en); 69 //printf("x=%d,y=%d",x,y); 70 if(x!=y) 71 { 72 p[x]=y; 73 ans+=a[i].dis; 74 sum++; 75 if(sum==n-1) 76 break; 77 } 78 } 79 //printf("%d",sum); 80 if(sum==n-1) 81 printf("%.1lf\n",ans*100); 82 else 83 printf("oh!\n"); 84 } 85 return 0; 86 }
HDU 1875 畅通工程再续 (最小生成树),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/clliff/p/3903160.html