您的位置:首页 > 其它

最小生成树 prim POJ 2349解题报告

2015-09-20 19:40 344 查看
Arctic Network

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 13809 Accepted: 4443

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.

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

1

2 4

0 100

0 300

0 600

150 750

Sample Output

212.13

用c++提交,不用G++

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define MAXN 505
#define INF 0xffffff
using namespace std;
int satelliteNum,outpostsNum;
int X[MAXN];//X[i]=1表示点i在最小生成树上
double DisX[MAXN];//DisX[i]表示点i(非生成树点)到生成树所有点的最短距离
double SideInTree[MAXN];//记录生成树上的线段长度
double G[MAXN][MAXN];
struct point{
double x,y;
point(){
x=0;y=0;
}
}Point[MAXN];
double GetDis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void Pri(){
memset(X,0,sizeof(X));
X[1]=1;
for(int i=2;i<=outpostsNum;i++) DisX[i]=G[i][1];
int SideNum=outpostsNum-1;
int nextPoint;//下一个将要被加进生成树的点
while(SideNum--){
double minPath=INF;
for(int i=2;i<=outpostsNum;i++){
if(!X[i]&&DisX[i]<minPath){//在当前距离中找到最小值
nextPoint=i;
minPath=DisX[i];
}
}
X[nextPoint]=1;//将最短距离对应的不在生成树上的点加进生成树
SideInTree[SideNum]=minPath;
for(int i=1;i<=outpostsNum;i++){//更新不在生成树的所有点的DisX
if(!X[i]&&DisX[i]>G[nextPoint][i])
DisX[i]=G[nextPoint][i];
}
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int tests;
scanf("%d",&tests);
while(tests--){
scanf("%d%d",&satelliteNum,&outpostsNum);
for(int i=1;i<=outpostsNum;i++) scanf("%lf%lf",&Point[i].x,&Point[i].y);
for(int i=1;i<=outpostsNum;i++){
for(int j=1;j<=outpostsNum;j++){
if(j>i)  G[i][j]=GetDis(Point[i],Point[j]);
else  G[i][j]=G[j][i];//减少计算量
}
G[i][i]=0;
}
Pri();
sort(SideInTree,SideInTree+outpostsNum-1);
printf("%.2lf\n",SideInTree[outpostsNum-1-satelliteNum]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 最小生成树 prim