Arctic Network
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10570 | Accepted: 3486 |
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大边的权值。可以用kruskal直接在加到第n-m条边时,输出答案并结束循环。不过这题我是为了练习prim算法,而prim算法必须在构造完生成树后,对权值数组进行一遍排序,千万不可以在中间加了n-m+1的顶点时,输出答案,因为prim算法加边的过程并不是按照边权从小到大的顺序,它取决于已加入点所派生出来的边权的最小值,这在全局的最小生成树中并不是按照权从小到大的顺序构造的。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define Maxn 510
using namespace std;
struct point{
int x,y;
}p[Maxn];
double adj[Maxn][Maxn],lowcost[Maxn];
int vis[Maxn];
const double inf=100000000;
double dis(int i,int j){
return sqrt(double(p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
}
void prim(int u,int n,int m){
memset(vis,0,sizeof vis);
vis[u]=1;
for(int i=0;i<n;i++)
lowcost[i]=adj[u][i];
for(int i=1;i<n;i++){
double minn=inf;
int v;
for(int j=0;j<n;j++)
if(!vis[j]&&lowcost[j]<minn)
minn=lowcost[j],v=j;
vis[v]=1;
for(int j=0;j<n;j++)
if(!vis[j]&&adj[v][j]<lowcost[j]) lowcost[j]=adj[v][j];
}
sort(lowcost,lowcost+n);
printf("%.2f\n",lowcost[n-m]);
}
int main()
{
int t,m,n;
scanf("%d",&t);
while(t--){
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
adj[i][j]=adj[j][i]=dis(i,j);
for(int i=0;i<n;i++)
adj[i][i]=0;
prim(0,n,m);
}
return 0;
}