zoukankan      html  css  js  c++  java
  • POJ 2349 Arctic Network (最小生成树第K大(小)边)

    Arctic Network
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 13108   Accepted: 4256

    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
    

    Source

     
    题意:两个城镇连接有两种方式,卫星和无线技术,卫星连接不需要代价,无线技术距离越远代价越大,M个城镇有N个卫星,求无限技术使用最大距离
    分析:求最小生成树中最大的第K条边
    当N==0要特判
    只是很没搞懂为什么同样一份代码交G++就WA交C++就AC,求大牛解释ORZ
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=1000+5;
    int kase,n,m;
    double ans[MAXN*MAXN/2];
    int p[MAXN];
    struct node
    {
        double x,y;
    }po[MAXN];
    struct edge
    {
        int u,v;
        double val;
        bool operator<(const edge A)const
        {
            return val<A.val;
        }
    }a[MAXN*MAXN/2];
    
    double calc(node A,node B)
    {
        return sqrt( (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y) );
    }
    
    int findfa(int x)
    {
        return p[x]==x?x:p[x]=findfa(p[x]);
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        scanf("%d",&kase);
        while(kase--)
        {
            scanf("%d %d",&n,&m);
            for(int i=1;i<=m;i++)
                scanf("%lf %lf",&po[i].x,&po[i].y);
    
            int cnt=1;
            for(int i=1;i<=m;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    a[cnt].u=i;
                    a[cnt].v=j;
                    a[cnt++].val=calc(po[i],po[j]);
                }
            }
            sort(a+1,a+1+cnt);
            for(int i=1;i<=m;i++) p[i]=i;
    
            int res=0;
            for(int i=1;i<=cnt;i++)
            {
                int u=a[i].u;
                int v=a[i].v;
                int x=findfa(u);
                int y=findfa(v);
                if(x!=y)
                {
                    p[x]=y;
                    ans[res++]=a[i].val;
                    if(res==m-1) break;
                }
            }
            if(n==0) printf("%.2lf
    ",ans[res-1]);
            else printf("%.2lf
    ",ans[res-n]);
        }
        return 0;
    }
    View Code
     
     
  • 相关阅读:
    python 3.6.2 使用VScode 安装requests包
    JExcel读取Excel,完成随机数对应学号,随机选取学生姓名
    HashMap和Map的道道
    腾讯云主机windows 2012 r2搭建ftp服务器
    关于网站域名访问出现welcome to nginx 的问题
    腾讯云主机windows 2012 r2标准版&&搭建java环境(jdk+tomcat)
    Anaconda 安装+使用+换源+更新
    已知三点求平面方程、平面法向量和点到平面的距离
    Open Cascade:计算体积
    VS2015安装QT插件
  • 原文地址:https://www.cnblogs.com/clliff/p/4709389.html
Copyright © 2011-2022 走看看