标签:eve des exce bre int return desc dia enter
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18447 | Accepted: 5829 |
Description
Input
Output
Sample Input
1
2 4
0 100
0 300
0 600
150 750
Sample Output
212.13
Source
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 int T,n,tot,fa[510],x,y; 8 const int INF=0x7fffffff; 9 double rt[250005]; 10 int mark=0; 11 struct dian{ 12 double x,y; 13 }node[510]; 14 struct Edge{ 15 int u,v;double w; 16 }e[250005]; 17 void add_edge(int u,int v,double w){ 18 e[tot].u=u;e[tot].v=v;e[tot].w=w;tot++; 19 } 20 bool cmp(Edge a,Edge b){ 21 return a.w<b.w; 22 } 23 void init(){ 24 memset(fa,-1,sizeof(fa)); 25 tot=0; 26 } 27 int find(int x){ 28 if(fa[x]==-1) return x; 29 else return fa[x]=find(fa[x]); 30 } 31 void kruskal(int n){ 32 double ans=0; 33 sort(e,e+tot,cmp); 34 int cnt=0; 35 for(int k=0;k<tot;k++){ 36 int u=e[k].u,v=e[k].v; 37 int rx=find(u),ry=find(v); 38 if(rx!=ry){ 39 rt[mark]=e[k].w;ans+=e[k].w; 40 fa[rx]=ry;mark++;cnt++; 41 } 42 if(cnt == n-1) break; 43 } 44 //if(cnt<n-1) return -1; 45 //else return ans; 46 } 47 int main() 48 { 49 scanf("%d",&T);int s; 50 while(T--){ 51 init(); 52 scanf("%d%d",&s,&n); 53 for(int i=1;i<=n;i++) 54 scanf("%lf %lf",&node[i].x,&node[i].y); 55 for(int i=1;i<=n;i++){ 56 double d; 57 for(int j=1;j<=i;j++){ 58 if(i==j){ d=INF; } 59 else{ 60 d=sqrt(1.0*(node[i].x-node[j].x)* 61 (node[i].x-node[j].x) 62 +1.0*(node[i].y-node[j].y) 63 *(node[i].y-node[j].y)); 64 } 65 add_edge(i,j,d); 66 } 67 } 68 kruskal(n); 69 printf("%.2f\n",rt[mark-s]); 70 } 71 return 0; 72 }
搞了一晚上的破题终于AC了
标签:eve des exce bre int return desc dia enter
原文地址:http://www.cnblogs.com/suishiguang/p/6347949.html