poj 2349(Prime + Kruskal 最小生成树)
来源:互联网 发布:淘宝手机比京东便宜 编辑:程序博客网 时间:2024/06/07 07:53
Arctic Network
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 16591 Accepted: 5284
Description
The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.
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.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.
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
The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).
Output
For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.
Sample Input
12 40 1000 3000 600150 750
Sample Output
212.13
Source
题意:
国防部(DND)要用无线网络连接北部几个哨所。两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道。
任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置。同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率。功率越大,D也越大,但成本更高。出于采购和维修的方便,所有哨所的收发器必须是相同的;那就是说,D值对每一个哨所相同。
你的任务是确定收发器的D的最小值。每对哨所间至少要有一条通信线路(直接或间接)。
Input
输入的第一行是测试数据的数量N。
每组测试数据的第一行包含卫星的数量S(1 < = S < = 100)和哨所的数量P(S < P < = 500)。接下来的P行,给出以公里为单位的每个哨所的坐标(x,y)( 坐标为0到10000之间的整数)。
Output
对于每组测试数据,输出一行,输出收发器的D的最小值。精确到小数点后两位。
分析:
有S颗卫星和P个哨所,有卫星的两个哨所之间可以任意通信;否则,一个哨所只能和距离它小于等于D的哨所通信。给出卫星的数量和P个哨所的坐标,求D的最小值。这是一个最小生成树问题。P个哨所最多用P-1条边即可连起来,而S颗卫星可以代替S-1条边,基于贪心思想,代替的边越长,求得的D就越小。所以可以用一个数组保存加入最小生成树的边的长度,共有P-1条边,如果从大到小排序,把前S-1条较长的边代替掉,剩下的边中最长的即为所求,即D=lu【s】,从小到大排序的话就把后面的S-1条边用卫星同道代替掉D=lu【top-S】。
Prime:
<pre name="code" class="cpp">#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <math.h>typedef long long ll;using namespace std;#define INF 1LL<<60#define N 550int T,S,P,top;int vis[N];double maps[N][N],dis[N];double lu[N]; ///用来存放生成最小生成树的所有边struct node{ int x,y;}p[N];void init(){ int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++) maps[i][j]=maps[j][i]=(i==j)?0:INF;}double Dist(node a,node b) ///求两点间的距离{ return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));}void Prime(){ int i,j,index; double Min; top=0; for(i=1;i<=P;i++) { dis[i]=maps[1][i]; vis[i]=0; } vis[1]=1; for(i=1;i<=P;i++) { Min=INF; index=-1; for(j=1;j<=P;j++) { if(!vis[j] && Min>dis[j]) { Min=dis[j]; index=j; } } if(index==-1) break; vis[index]=1; lu[top++]=Min; ///用来建树的边存在lu[]中.。。 for(j=1;j<=P;j++) { if(!vis[j] && dis[j]>maps[index][j]) dis[j]=maps[index][j]; } }}int main(){ int i,j; scanf("%d",&T); while(T--) { scanf("%d%d",&S ,&P); init(); for(i=1;i<=P;i++) { scanf("%d%d",&p[i].x,&p[i].y); for(j=1;j<i;j++) { maps[i][j]=maps[j][i]=Dist(p[i],p[j]); } } Prime(); sort(lu,lu+top); ///sort将lu[]中的所有边从小到达排序 double ans=lu[top-S]; ///直接输出删除S-1条边后的最大值 printf("%.2lf\n",ans); } return 0;}
Kruskal:
<pre name="code" class="cpp">#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <math.h>typedef long long ll;using namespace std;#define INF 1LL<<60#define N 550int T,S,P,top,k;int father[N];double lu[N];struct node ///用来存放各个点坐标{ int x,y;}p[N];struct edge ///存每条边的两个结点和距离{ int u,v; double w;}e[N*N];int cmp(edge a,edge b){ return a.w<b.w;}double Dist(node a,node b){ return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));}int Find(int x){ while(x!=father[x]) x=father[x]; return x;}int main(){ int i,j,k; scanf("%d",&T); while(T--) { scanf("%d%d",&S,&P); for(i=1;i<=P;i++) father[i]=i; for(i=1;i<=P;i++) scanf("%d%d",&p[i].x,&p[i].y); top=0; for(i=1;i<=P;i++) { for(j=i+1;j<=P;j++) { e[top].u=i; e[top].v=j; e[top].w=Dist(p[i],p[j]); top++; e[top].u=j; e[top].v=i; e[top].w=Dist(p[i],p[j]); top++; } } sort(e,e+top,cmp); k=0; for(i=0;i<top;i++) { int x,y; x=Find(e[i].u); y=Find(e[i].v); if(x==y) continue; else { if(x<y) father[y]=x; else father[x]=y; lu[k++]=e[i].w; if(k==P-1) /// 当找到P-1条边时 已经形成最小生成树,跳出 break; } } printf("%.2f\n",lu[k-S]); } return 0;}
0 0
- poj 2349(Prime + Kruskal 最小生成树)
- poj 1287 (最小生成树 Prime + kruskal )
- poj 2421 (最小生成树 Prime+kruskal)
- poj 1751 (最小生成树 Prime+kruskal 路径输出)
- poj2485-最小生成树(prime+kruskal)
- 最小生成树---Kruskal/Prime算法
- 最小生成树(Prime/kruskal)
- 最小生成树算法[Prime/(Kruskal)]
- 【图论】【最小生成树】【kruskal+prime】
- 最小生成树(Prime and Kruskal)
- HDU 1301 &POJ 1215 Jungle Roads【最小生成树,Prime算法+Kruskal算法】
- Agri-Net(POJ 1258)(裸最小生成树)(Prime算法+Kruskal算法)
- 最小生成树的prime和kruskal算法
- 最小生成树(kruskal算法)和 (Prime算法)
- 图论-最小生成树-prime算法&Kruskal算法 c++
- EOJ2067 最小生成树 prime算法+kruskal算法
- hdu1863 畅通工程 Kruskal 和 Prime求最小生成树
- Arctic Network(POJ 2349)(最小生成树Kruskal)
- C++11 异步方法
- 剑指offer第12题 最优解
- Android学习笔记035之HTTP协议
- webView的使用及注意事项
- webapp开发使用Cordova-Cordova教程
- poj 2349(Prime + Kruskal 最小生成树)
- C++学习之路(3) 常用数据结构与STL
- 使用SpanableString实现textview部分字体点击事件(不同颜色)
- Eclipse开发C/C++环境配置
- 01背包
- JS构造Map结构
- 完数-week6-C语言习题集
- revit二次开发基础
- Linux命令--grep