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   vCoders  阅读(171)  评论(0编辑  收藏  举报

编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

点击右上角即可分享
微信分享提示