poj2349Arctic Network (最小生成树运用)

来源:互联网 发布:redis 持久化数据库 编辑:程序博客网 时间:2024/06/05 01:00
题意:看半个小时的题...大概就是说有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个科研站都能直接或者间接相连。如果任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远。 而安装有无线电设备的两个站,距离不能超过D。 D越长费用越多。

现在有s个卫星设备可以安装,还有足够多的无线电设备,求一个方案,使得费用D最少(D取决与所有用无线电通信的花费最大的那条路径)。


思路:一看前面的使得任意两个科研站直接或者间接相连就想到最小生成树,那么因为D越长费用越多,基于贪心的思想,所以肯定是把最小生成树前s-1条最长的边用卫星连接(S个卫星最多可以连接S-1条边),将最小生成树每一条边的权值保存下来,假设有n个科研站的话那么就有n-1条边相连,把前s-1条边用卫星连接之后,那么剩下的最大的就是d[n-s-1]


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define MAXN 1000010#define INF 0x7FFFFFFF#define ll long longstruct node{  int u,v;  double dis;}edge[MAXN];int father[550];int n,m,cnt,s;double x[550],y[550];double ans[550];bool cmp(node x,node y){  return x.dis<y.dis;}int find(int x){  int t = x;  while(father[t]!=t){    t = father[t];  }  int k = x;  while(k!=t){    int temp = father[k];    father[k] = t;    k = temp;  }  return t;}void kruskal(){  int i,j=0;  for(i=1;i<=n;i++)   father[i] = i;  for(i=0;i<m;i++){    int a = find(edge[i].u);    int b = find(edge[i].v);    if(a!=b){      father[a] = b;      ans[cnt++] = edge[i].dis;      j++;      if(j>=n-1)  break;    }  }}int main(){  int t,i,j,a,b;  scanf("%d",&t);  while(t--){    cnt = 0;    m = 0;    scanf("%d%d",&s,&n);    for(i=1;i<=n;i++){      scanf("%lf%lf",&x[i],&y[i]);      for(j=1;j<i;j++){        edge[m].u = i;        edge[m].v = j;        double temp = (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);        edge[m].dis = sqrt(temp);        m++;      }    }    sort(edge,edge+m,cmp);    kruskal();    printf("%.2f\n",ans[n-s-1]);  }  return 0;}


The Department of NationalDefence (DND) wishes toconnect several northern outpostsby a wireless network.Two different communicationtechnologies are to beused in establishing the network:every outpost willhave a radio transceiver andsome outposts will in additionhave a satellite channel.Any two outposts witha satellite channel can communicatevia the satellite,regardless of their location.Otherwise, two outposts cancommunicate by radio onlyif the distance between themdoes not exceed D, which dependsof the power of thetransceivers. Higher poweryields higher D but costsmore. Due to purchasingand maintenance considerations,the transceivers at theoutposts must be identical;that is, the value of D is thesame for every pair of outposts.Your job is to determinethe minimum D required forthe transceivers. There mustbe 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 contains1 ≤ S ≤ 100, the number of satellite channels, and S < P ≤ 500, the number of outposts. P linesfollow, giving the (x, y) coordinates of each outpost in km (coordinates are integers between 0 and10,000).

Output

For each case, output should consist of a single line giving the minimum D required to connect thenetwork. 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

0 0