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
1 2 4 0 100 0 300 0 600 150 750
Sample Output
212.13
大致题意:有n个卫星和m个哨所,现在要把m个哨所并入通信网络,在n个哨所中安放卫星,安放有卫星的哨所互相通信可以无视距离,剩下的哨所需要配置信号接收器,信号接收器接收信号的半径越大价格越高。每个哨所安放的信号接收器的型号都一样,即信号接收半径都一样,求最低要购买接收信号半径为多少的信号接收器.
解题思路:用最小生成树解,用最小生成树求出联通所有哨所的所有路径长度,然后在里面最长的路径所联通的哨所
里放卫星。
1.普里姆算法:
#include <iostream> // 普里姆算法
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define sc(a) scanf("%d",&a)
#define nsc(a,b) scanf("%d %d",&a,&b)
#define pr(a) printf("%d\n",a)
#define mem(a,x) memset(a,x,sizeof(a))
#define lf(i,l,r) for(int i=l;i<r;i++)
#define inf 0x3f3f3f3f
#define N 510
int t,n,m,cnt,x[N],y[N],vis[N];
double spot[N][N],num[N],dis[N];
void solve()
{
int tol;
cnt=0;
lf(i,1,m+1)
dis[i]=spot[1][i];
vis[1]=1;
lf(i,1,m)
{
tol=-1;
num[cnt]=inf; //用num数组来存储最短路径中的所有路径。
lf(j,1,m+1)
{
if(!vis[j]&&num[cnt]>dis[j])
{
num[cnt]=dis[j];
tol=j;
}
}
cnt++;
if(tol==-1)
return ;
vis[tol]=1;
lf(j,1,m+1)
if(!vis[j])
dis[j]=min(dis[j],spot[tol][j]);
}
return ;
}
int main()
{
sc(t);
while(t--)
{
nsc(n,m);
lf(i,1,m+1)
{
vis[i]=0;
nsc(x[i],y[i]);
spot[i][i]=inf;
lf(j,1,i)
{
spot[i][j]=spot[j][i]=sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
}
solve();
sort(num,num+cnt); //排序,因为卫星要放在里面最长路径连接的哨所
printf("%.2lf\n",num[cnt-n]); // 一共cnt-1条路径,n-1条路径可以通过放卫星替换
}
return 0;
}
2.克鲁斯卡尔算法:
#include <iostream> // 克鲁斯卡尔算法
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define sc(a) scanf("%d",&a)
#define nsc(a,b) scanf("%d %d",&a,&b)
#define pr(a) printf("%d\n",a)
#define mem(a,x) memset(a,x,sizeof(a))
#define lf(i,l,r) for(int i=l;i<r;i++)
#define inf 0x3f3f3f3f
#define N 510
int t,n,m,cnt,x[N],y[N],p[N];
double num;
struct node
{
int s;
int e;
double v;
} spot[N*N>>1];
int Find(int x)
{
if(x!=p[x])
{
p[x]=Find(p[x]);
}
return p[x];
}
int cmp(node a,node b)
{
return a.v<b.v;
}
void solve()
{
int tol=0;
num=0;
lf(i,1,cnt)
{
int xx=Find(spot[i].s);
int yy=Find(spot[i].e);
if(xx!=yy)
{
p[xx]=yy;
tol++; //tol代表找到的是第几条小的边
if(tol==m-n) //第m-n条边就是没有替换的最后的一条边。
{
num=spot[i].v;
return ;
}
}
}
return ;
}
int main()
{
sc(t);
while(t--)
{
cnt=1;
nsc(n,m);
lf(i,1,m+1)
{
p[i]=i;
nsc(x[i],y[i]);
lf(j,1,i)
{
spot[cnt].s=i;
spot[cnt].e=j;
spot[cnt++].v=sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
}
sort(spot+1,spot+cnt,cmp); //先排序。
solve();
printf("%.2lf\n",num);
}
return 0;
}