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
     
     
  • 相关阅读:
    [转]如何让ruby中的ri起作用
    大四中软实习笔记20130304
    VC6.0: fatal error C1010: unexpected end of file while looking for precompiled head
    大四中软实习笔记20130307
    C++ odbc 连接数据库错误 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
    大四中软实习笔记20130303
    VC6.0: unresolved external symbol "public
    【分布式锁的演化】什么是锁?
    【分布式锁的演化】常用锁的种类以及解决方案
    【数据库】海量数据切分方案
  • 原文地址:https://www.cnblogs.com/clliff/p/4709389.html
Copyright © 2011-2022 走看看