POJ2349:Arctic Network(二分+最小生成树)

Arctic Network

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 28311   Accepted: 8570

题目链接http://poj.org/problem?id=2349

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

题意:

有n个点,现在有s个无线通话器,拥有无线通话器的不同点可以直接通信,但其余的通信只能在一个距离D内。

现在求这个最小D是多少。

题解:

问最小D,虽然点很少,但很明显直接枚举是不可行的。所以我们可以考虑二分这个距离D。

为什么可以二分?因为很明显地,这个距离D越大,可以直接通信的人也就越多,也就是这个问题是具有单调性的。

然后二分过后建图跑最小生成树看看有多少连通块,根据连通块个数来确定无线通话器的分配个数就行了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int t;
int s,n,tot;
struct Point{
int x,y;
}p[N];
struct Edge{
int u,v;
double w;
bool operator < (const Edge &A)const{
return w<A.w;
}
}e[N*N];
int f[N];
int find(int x){
return f[x]==x?f[x]:f[x]=find(f[x]);
}
void Kruskal(){
for(int i=;i<=n+;i++) f[i]=i;
for(int i=;i<=tot;i++){
int fx=find(e[i].u),fy=find(e[i].v);
if(fx==fy) continue ;
f[fx]=fy;
}
}
double dis(int x,int y){
return sqrt((double)(p[x].x-p[y].x)*(p[x].x-p[y].x)+(double)(p[x].y-p[y].y)*(p[x].y-p[y].y));
}
void build(double x){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j) continue ;
if(dis(i,j)<=x){
e[++tot].u=i;
e[tot].v=j;
e[tot].w=dis(i,j);
}
}
}
}
bool check(double x){
tot=;
build(x);
Kruskal();
int cnt = ;
for(int i=;i<=n;i++){
if(f[i]==i) cnt++;
}
if(s>=cnt) return true;
return false;
}
int main(){
cin>>t;
while(t--){
scanf("%d%d",&s,&n);
for(int i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
double l=,r=INF,mid;
while(r-l>=0.00001){
mid=(l+r)/2.0;
if(check(mid)) r=mid;
else l=mid+0.0001;
}
printf("%.2f\n",l);
}
return ;
}
上一篇:CoreGraphics之CGContextSaveGState与UIGraphicsPushContext


下一篇:redis外部访问