Arctic Network
题目链接:
http://acm.hust.edu.cn/vjudge/contest/124434#problem/F
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
##题意:
求最小的花费使得各点联通,可以建S个免费的道路.
##题解:
裸的最小生成树.
肯定是优先把最小生成树中长度大的边建成免费边.
所以记录一下最小生成树中第S小的边即可.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 550
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
struct node{
int left,right;
double cost;
}road[maxn*maxn];
int cmp(node x,node y) {return x.cost<y.cost;}
int p[maxn],m,n;
int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}
double kruskal(int k)
{
if(k>=n) return 0;
int cnt = 0;
for(int i=1;i<=n;i++) p[i]=i;
sort(road+1,road+m+1,cmp);
for(int i=1;i<=m;i++) {
int x=find(road[i].left);
int y=find(road[i].right);
if(x!=y) {
cnt++;
if(cnt == n-k)
return road[i].cost;
//ans+=road[i].cost;
p[x]=y;
}
}
}
int sign(double x) {
if(fabs(x)<eps) return 0;
return x<0? -1:1;
}
double x[maxn],y[maxn];
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t;
while(t--)
{
int k;
scanf("%d %d", &k,&n);
m = 0;
memset(road,0,sizeof(road));
for(int i=1; i<=n; i++) {
scanf("%lf %lf", &x[i],&y[i]);
}
for(int i=1; i<=n; i++) {
for(int j=i+1; j<=n; j++) {
double d = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));
road[++m].left = i;
road[m].right = j;
road[m].cost = d;
}
}
double ans=kruskal(k);
printf("%.2f\n", ans);
}
return 0;
}