poj2349 Arctic Network

来源:互联网 发布:林弯弯网店淘宝网址 编辑:程序博客网 时间:2024/06/05 10:57

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.

有s个超级节点,自然最后可以剩下s个连通块。仿照标准的kruskal,最后剩下1个连通块。所以只需要从小到大加入n-s条边即可。

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;struct edge{    double l;    int f,t;    bool operator < (const edge & eee) const    {        return l<eee.l;    }}a[500010];double x[510],y[510],ans[510];int fa[510],tot;int find(int p){    if (fa[p]==p) return p;    return fa[p]=find(fa[p]);}void add(int i,int j){    double dis=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));    a[++tot]=(edge){dis,i,j};}int main(){    int i,j,k,m,n,p,q,T,cnt;    double z,now,last;    scanf("%d",&T);    while (T--)    {        tot=cnt=now=last=0;        scanf("%d%d",&m,&n);        for (i=1;i<=n;i++)          scanf("%lf%lf",&x[i],&y[i]);        for (i=1;i<=n;i++)          for (j=i+1;j<=n;j++)            add(i,j);        for (i=1;i<=n;i++)          fa[i]=i;        sort(a+1,a+tot+1);        for (i=1;cnt<n-m;i++)          if (find(a[i].f)!=find(a[i].t))          {            now=a[i].l;            fa[fa[a[i].f]]=fa[a[i].t];            cnt++;          }        printf("%.2f\n",now);    }}
0 0
原创粉丝点击