poj 2349 Arctic Network
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11187 | Accepted: 3660 |
Description
Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.
Input
Output
Sample Input
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
Source
分析:
Kruskal算法,由短到长排序,生成最小生成树,倒数的s-1条边用卫星通讯代替,由前向后第p-1-(s-1)条边的长度就是所求
1 #include<iostream> 2 #include<queue> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 struct point{ 9 int x,y; 10 }; 11 struct edge{ 12 int u,v; 13 double w; 14 }; 15 point po[505]; 16 int f[505]; 17 edge e[250000]; 18 bool cmp(edge a,edge b){ 19 return a.w<b.w; 20 } 21 int find(int i){//并查集 22 if(i!=f[i]){ 23 f[i]=find(f[i]); 24 } 25 return f[i]; 26 } 27 double get_dis(int i,int j){ 28 int x=po[i].x-po[j].x; 29 int y=po[i].y-po[j].y; 30 return sqrt(double(x*x+y*y)); 31 } 32 double kruskal(int m,int ss){ 33 int i=0; 34 for(;i<m;i++){ 35 int p=find(e[i].u); 36 int q=find(e[i].v); 37 if(p!=q){ 38 if(p>q){ 39 f[q]=p; 40 } 41 else{ 42 f[p]=q; 43 } 44 ss--; 45 } 46 if(ss==0){ 47 return e[i].w; 48 } 49 } 50 } 51 int main(){ 52 int t,s,p,m; 53 scanf("%d",&t); 54 while(t--){ 55 m=0; 56 scanf("%d %d",&s,&p); 57 int i,j,k; 58 for(i=0;i<p;i++){ 59 scanf("%d %d",&po[i].x,&po[i].y); 60 f[i]=i; 61 } 62 for(i=0;i<p;i++){ 63 for(j=i+1;j<p;j++){ 64 e[m].u=i; 65 e[m].v=j; 66 double dis=get_dis(i,j); 67 e[m++].w=dis; 68 e[m].u=j; 69 e[m].v=i; 70 e[m++].w=dis; 71 } 72 } 73 sort(e,e+m,cmp); 74 /*for(i=0;i<m;i++) 75 cout<<"i: "<<e[i].w<<endl;*/ 76 printf("%.2f\n",kruskal(m,p-s)); 77 } 78 return 0; 79 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。