您的位置:首页 > 其它

POJ 2349 Arctic Network+Kruskal最小生成树

2017-03-17 19:38 260 查看
Arctic Network

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18714 Accepted: 5921
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

Source
Waterloo local 2002.09.28

题意:

首先是样例数T

输入卫星个数S,输入村庄个数P,然后输入P个村庄的坐标。

卫星的作用就是让两个村庄可以无视距离的通讯,*(x个村庄通信需要x个卫星)

让你求为了让所有村庄通信,除了卫星的最小的两个村庄距离。

解题思路:

直接500个村庄,完全图的边数在合理范围内,所以求出每两个村庄的距离

跑一遍最小生成树,注意这个最小生成数加边加到n-s就够了,输出那个边就可以了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 1000005 ;
int s,p,n,m;
int u[maxn],v[maxn];
double w[maxn] ;
int xx[maxn],yy[maxn];
int pp[maxn];
int r[maxn];
int fid(int x){return pp[x]==x?x:pp[x]=fid(pp[x]);}
int cmp(const int i,const int j){return w[i]<w[j] ;}
double kruskal(){
double ret =0;
for(int i=0;i<n;i++)pp[i]=i;
for(int i=0;i<m;i++)r[i]=i;
sort(r,r+m,cmp) ;
int cnt=0;
for(int i=0;i<m;i++){
if(cnt==n-s){
b
4000
reak ;
}
int e = r[i] ;
int x = fid(u[e]) ;
int y = fid(v[e]) ;
if(x!=y){
pp[x]=y ;
cnt++ ;
ret = w[e] ;
}
}
return ret ;

}
int main(){
int t ;
scanf("%d",&t);
while(t--){
scanf("%d%d",&s,&p);
n=p;
for(int i=0;i<p;i++){
scanf("%d%d",&xx[i],&yy[i]);
}
int c=0;
for(int i=0;i<p;i++){
for(int j=i+1;j<p;j++){
u[c]=i;
v[c]=j;
w[c++]=sqrt(pow((double)(xx[i]-xx[j]),2)+pow((double)(yy[i]-yy[j]),2));
}
}
m=c;
double ans = kruskal() ;
printf("%.2f\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: