POJ2349(求生成树中符合题意的边)

Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14977   Accepted: 4777

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
题意及思路:有P个坐标,将P个坐标分为S组,求所有组的生成树的最大边中的最大边。求P个坐标构成的生成树,将边由大到小排序,第S条边即为所求。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=505*505;
const double INF=1.0e8;
typedef pair<double,int> P;
struct Point{
    int x,y,index;
}points[505];
struct Edge{
    int to,next;
    double cost;
}es[MAXN];
int V,E;
double dist(int x1,int y1,int x2,int y2)
{
    return sqrt((double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
bool comp(double a,double b)
{
    return a > b;
}
int head[505];
void add_edge(int u,int v,double cost)
{
    es[E].to=v;
    es[E].cost=cost;
    es[E].next=head[u];
    head[u]=E;
    E++;
}
int cnt;
double res[505];
double d[505];
int vis[505];
void prim(int s)
{
    for(int i=1;i<=V;i++)
    {
        d[i]=INF;
        vis[i]=0;
    }
    d[s]=0;
    cnt=0;
    priority_queue<P,vector<P>,greater<P> > que;
    que.push(P(0,s));
    while(!que.empty())
    {
        P now=que.top();que.pop();
        int v=now.second;
        if(vis[v])    continue;
        res[cnt++]=now.first;
        vis[v]=1;
        for(int i=head[v];i!=-1;i=es[i].next)
        {
            Edge e=es[i];
            if(d[e.to]>e.cost)
            {
                d[e.to]=e.cost;
                que.push(P(d[e.to],e.to));
            }    
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    int S,P;
    while(T--)
    {
        scanf("%d%d",&S,&P);
        memset(head,-1,sizeof(head));
        V=P;
        E=0;
        for(int i=0;i<P;i++)
        {
            scanf("%d%d",&points[i].x,&points[i].y);
            points[i].index=i+1;
        }
        for(int i=0;i<P;i++)
            for(int j=i+1;j<P;j++)
            {
                int indi=points[i].index;
                int indj=points[j].index;
                double co=dist(points[i].x,points[i].y,points[j].x,points[j].y);
                add_edge(indi,indj,co);
                add_edge(indj,indi,co);
            }    
        
        prim(1);
        sort(res,res+cnt,comp);    
        printf("%0.2lf\n",res[S-1]);
    }
    
    return 0;
}

 

posted on 2016-01-25 23:44  vCoders  阅读(171)  评论(0编辑  收藏  举报

导航