标签:des style blog http color os io java strong
先上题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 888 Accepted Submission(s): 345
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #define MAX 100 6 using namespace std; 7 8 const double eps=1e-3; 9 10 int p[MAX]; 11 int n; 12 int dcmp(double x){ 13 if(fabs(x)<eps) return 0; 14 return x>0 ? 1 : -1; 15 } 16 17 typedef struct Point{ 18 double x,y; 19 Point(double x=0,double y=0):x(x),y(y){} 20 }Point; 21 22 Point s[MAX]; 23 int id[MAX]; 24 25 typedef struct edge{ 26 int a,b; 27 double l; 28 29 bool operator < (const edge& o)const{ 30 return dcmp(l-o.l)<0; 31 } 32 33 }edge; 34 35 edge e[MAX*MAX]; 36 int tot; 37 38 double dist(Point a,Point b){ 39 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 40 } 41 42 int findset(int x){ return p[x]==x ? p[x] : p[x]=findset(p[x]);} 43 44 45 46 int main() 47 { 48 int t,co; 49 double ans,minn; 50 //freopen("data.txt","r",stdin); 51 scanf("%d",&t); 52 while(t--){ 53 scanf("%d",&n); 54 for(int i=0;i<n;i++){ 55 scanf("%lf %lf",&s[i].x,&s[i].y); 56 } 57 tot=0; 58 for(int i=0;i<n;i++){ 59 for(int j=i+1;j<n;j++){ 60 double l=dist(s[i],s[j]); 61 e[tot].a=i; e[tot].b=j; e[tot].l=l; 62 tot++; 63 } 64 } 65 sort(e,e+tot); 66 minn=1e12; 67 for(int i=0;i<n;i++){ 68 ans=0; 69 for(int j=0;j<n;j++){ 70 p[j]=j; 71 } 72 co=0; 73 for(int j=0;j<tot && co!=n-2;j++){ 74 if(e[j].a==i || e[j].b==i) continue; 75 int a=findset(e[j].a),b=findset(e[j].b); 76 if(p[a]!=p[b]){ 77 p[b]=p[a]; 78 ans+=e[j].l; 79 co++; 80 } 81 } 82 minn=min(ans,minn); 83 } 84 if(n==1) minn=0; 85 printf("%.2lf\n",minn); 86 } 87 return 0; 88 }
标签:des style blog http color os io java strong
原文地址:http://www.cnblogs.com/sineatos/p/3946727.html