[POJ](1789)Arctic Network ---最小生成树(图)

Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22103   Accepted: 6831

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


解题新知:

       ①题意:有两种通信方式,一种是卫星通信,一种是无线电通信。卫星通信可以不限制距离通信,无线电通信必须在距离D以内(包括D)才能通信。两个哨所必须都有卫星或无线电才能实现互相通信,D取决于无线电设备的功率,功率越大,D越大,但成本会更高。出于采购和维修的方便,所有哨所的的无线电收发器一样,即D对于每一个哨所相同。S是给的卫星个数,P给的是哨所个数。然后又给了每个哨所的坐标。现在问你,如何确定收发器的D的最小值,使得每对哨所之间至少有一条通信线路(直接或间接)。

       ②思路:由题意可知这是一个最小生成树的问题,关键在于策略!对,就是有贪心的策略,有S个卫星,那么就有S-1条边,让距离最大的S-1条边用卫星通信,剩下最大的 [(D-1)-(S-1)]这条边所对应的权值就是我们要求的D。


注意:POJ上,如果以G++交,double类型,采用scanf或printf,%lf会WA,必须用%f;如果使用C++交,必须注意sqrt()函数,传入sqrt函数的值必须是浮点型,不然会CE,所以转化一下就好。G++的sqrt()可以传入整数类型。感觉好坑啊/(ㄒoㄒ)/~~


AC代码:

⒈C++:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
double mmap[505][505];
bool vis[505];
double dist[505];
double d[505];
double INF=1e9;
int s,p;
struct node
{
    int x;
    int y;
};
struct node edge[505];
double compute(node a,node b)
{
    return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
}
void process()
{
    double tmp;
    for(int i=1;i<=p;i++)
        mmap[i][i]=0;
    for(int i=1;i<=p;i++)
    {
        for(int j=i+1;j<=p;j++)
        {
            tmp=compute(edge[i],edge[j]);
            mmap[i][j]=mmap[j][i]=tmp;
        }
    }
}
void prim()
{
    for(int i=1;i<=p;i++)
        dist[i]=mmap[1][i];
    int k;
    double mmin;
    int sum=0;
    for(int i=1;i<=p;i++)
    {
        mmin=INF;
        for(int j=1;j<=p;j++)
        {
            if(!vis[j] && dist[j]<mmin)
            {
                mmin=dist[j];
                k=j;
            }
        }
        vis[k]=true;
        d[sum++]=mmin;
        for(int j=1;j<=p;j++)
        {
            if(!vis[j] && mmap[k][j]<dist[j])
                dist[j]=mmap[k][j];
        }
    }
    sort(d,d+sum);
    /*for(int i=0;i<sum;i++)
        cout<<d[i]<<" ";*/
    printf("%.2lf\n",d[sum-s]);
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(vis,false,sizeof(vis));
        memset(dist,0,sizeof(dist));
        cin>>s>>p;
        for(int i=1;i<=p;i++)
        {
            cin>>edge[i].x>>edge[i].y;
        }
        process();
        prim();
    }
    return 0;
}


⒉G++:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
double mmap[505][505];
bool vis[505];
double dist[505];
double d[505];
double INF=1e9;
int s,p;
struct node
{
    int x;
    int y;
};
struct node edge[505];
double compute(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void process()
{
    double tmp;
    for(int i=1;i<=p;i++)
        mmap[i][i]=0;
    for(int i=1;i<=p;i++)
    {
        for(int j=i+1;j<=p;j++)
        {
            tmp=compute(edge[i],edge[j]);
            mmap[i][j]=mmap[j][i]=tmp;
        }
    }
}
void prim()
{
    for(int i=1;i<=p;i++)
        dist[i]=mmap[1][i];
    int k;
    double mmin;
    int sum=0;
    for(int i=1;i<=p;i++)
    {
        mmin=INF;
        for(int j=1;j<=p;j++)
        {
            if(!vis[j] && dist[j]<mmin)
            {
                mmin=dist[j];
                k=j;
            }
        }
        vis[k]=true;
        d[sum++]=mmin;
        for(int j=1;j<=p;j++)
        {
            if(!vis[j] && mmap[k][j]<dist[j])
                dist[j]=mmap[k][j];
        }
    }
    sort(d,d+sum);
    printf("%.2f\n",d[sum-s]);
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(vis,false,sizeof(vis));
        memset(dist,0,sizeof(dist));
        cin>>s>>p;
        for(int i=1;i<=p;i++)
        {
            cin>>edge[i].x>>edge[i].y;
        }
        process();
        prim();
    }
    return 0;
}


posted @ 2017-08-16 09:24  WangMeow  阅读(561)  评论(0编辑  收藏  举报