POJ2349 Arctic Network 题解【最小生成树】【Kruskal】【图论】

来源:互联网 发布:ubuntu卸载samba 编辑:程序博客网 时间:2024/05/22 23:59

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
解题报告
这道题就是1-p个点之间两两建边,边权可以求,求一个最小生成树。
代码如下:

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;const int N=500;const int M=125000+100;int T;int s,p;int head[N+5],num;int father[N+5];struct node{    double x,y;}nd[N+5];struct edge{    int u,v;    double w;    int next;    edge(){next=-1;}    bool operator<(const edge& b)const    {return w<b.w;}}ed[M<<2];inline void build(int u,int v,double w){    ed[++num].u=u;    ed[num].v=v;    ed[num].w=w;    ed[num].next=head[u];    head[u]=num;}int getfather(int x)  {    return father[x]!=x?(father[x]=getfather(father[x])):x;  }  inline int unionn(int x,int y)  {    return ((x=getfather(x))!=(y=getfather(y)))&&(father[x]=y);  }  double kruskal(){    double ans=0;    int tot=0;    for(int i=1;i<=p;++i)    father[i]=i;    sort(ed+1,ed+1+num);    for(int i=1;i<=num;++i)    {        int u=getfather(ed[i].v),v=getfather(ed[i].u);        if(u!=v)        {            ++tot,ans=ed[i].w;            unionn(u,v);        }        if(tot==p-s) return ans;    }    return -1.0;}double dis(node a,node b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){    for(scanf("%d",&T);T;--T)    {        num=0;        memset(head,-1,sizeof(head));        scanf("%d%d",&s,&p);        for(int i=1;i<=p;i++)        {            scanf("%lf%lf",&nd[i].x,&nd[i].y);            for(int j=1;j<=i-1;j++)            build(i,j,dis(nd[i],nd[j]));        }        printf("%.2lf\n",kruskal());    }    return 0;}

“`

原创粉丝点击