Arctic Network POJ - 2349 (最小生成树Kruskal)

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个卫星系统,每个卫星系统只能安排在一个站点 ,然后有卫星系统的站点间通讯不需要代价,否则要连接两个站点所花代价为两个站点间的距离。问使得所有站点连通所花费的代价中最大的代价最小是多少。

思路:首先肯定是生成一颗最小生成树,然后将s个卫星放置在前s个距离最大的点上,那么剩下的最大距离即是最小的最大花费。
然后巨坑的是 poj输出 double 也用%f 不然一般会wa ????

代码如下

#include  
#include 
#include 
#include 
#include 
#include 
using namespace std; 
#define ll long long int 

int pre[505];
int tol;
int n;
struct NODE//链式向前星存边
{
    int u,v;
    double cost;
}edge[505*505];

void add(int u,int v,double cost)
{
    edge[tol].u=u;
    edge[tol].v=v;
    edge[tol].cost=cost;
    tol++;
}
int find(int x)
{
   int r=x;
   while (pre[r]!=r)
   r=pre[r];
   int i=x; int j;
   while(i!=r)
   {
       j=pre[i];
       pre[i]=r;
       i=j;
   }
   return r;
}
int join(int a,int b)
{
    int f1=find(a);
    int f2=find(b);
    if(f1==f2) return 0;
    else 
    {
        pre[f2]=f1;
        return 1;
    }
}

void init()
{
    for(int i=1;i<=n;i++)
    pre[i]=i;
    tol=0;
}

bool cmp(NODE a,NODE b)
{
    return a.coststruct node 
{
    int x,y;
};
node dian[505];

double len[505];
int main() 
{  
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int s;
        scanf("%d%d",&s,&n);

        for(int i=1;i<=n;i++)
        scanf("%d%d",&dian[i].x,&dian[i].y);
        init();
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
                double cost;
                cost=sqrt((dian[i].x-dian[j].x)*(dian[i].x-dian[j].x)+(dian[i].y-dian[j].y)*(dian[i].y-dian[j].y));//计算两点间的距离
                add(i,j,cost);
        }

        sort(edge,edge+tol,cmp);
        int flag=n;
        for(int i=0;iif(join(edge[i].u,edge[i].v))
            {
                flag--;
                if(flag==s)//剩下的s个站点都可以用卫星来代替通讯,所以此时的距离为最大距离
                {
                    printf("%.2f\n",edge[i].cost);
                    break;
                }
            }
        }

    }
    return 0;
}

你可能感兴趣的:(最短路&&最小生成树,模拟)