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 = 505; 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=0;i<=n+1;i++) f[i]=i; for(int i=1;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=1;i<=n;i++){ for(int j=1;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=0; build(x); Kruskal(); int cnt = 0; for(int i=1;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=1;i<=n;i++){ scanf("%d%d",&p[i].x,&p[i].y); } double l=0,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 ",l); } return 0; }