zoukankan      html  css  js  c++  java
  • POJ2349 Arctic Network(Prim)

    Arctic Network
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 16968   Accepted: 5412

    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

    【题意】有S颗卫星和P个哨所,有卫星的两个哨所之间可以任意通信;否则,一个哨所只能和距离它小于等于D的哨所通信。给出卫星的数量和P个哨所的坐标,求D的最小值。
    【分析】为了练练Prim。可以先用Prim把边建好,存起来,然后把选择的边从小到大排个序,第(n-p-1)个数就是答案。
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include<functional>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pi acos(-1.0)
    using namespace std;
    typedef long long ll;
    const int N=2005;
    const int M=15005;
    double edg[N][N];
    double lowcost[N];//记录未加入树集合的i离树集合中元素最小的距离
    int n,m,t,v;
    double d[N];
    bool cmp(double a,double b){
    return a<b;
    }
    struct man
    {
        double x,y;int num;
    }a[N];
    double fun(man a,man b)
    {
       double cnt=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
       return cnt;
    }
    void Build()
    {
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                edg[i][j]=edg[j][i]=fun(a[i],a[j]);
            }
        }
    }
    void prim()
    {
        double sum=0;lowcost[0]=-1;
        for(int i=1;i<n;i++){
            lowcost[i]=edg[0][i];
        }
        for(int i=1;i<n;i++){
            double minn=inf;int k;
            for(int j=0;j<n;j++){
                if(lowcost[j]!=-1&&lowcost[j]<minn){
                    k=j;minn=lowcost[j];
                }
            }
            sum+=minn;
           d[v++]=minn;
            lowcost[k]=-1;
            for(int j=0;j<n;j++){
                lowcost[j]=min(lowcost[j],edg[k][j]);
            }
        }
        sort(d,d+v,cmp);
        printf("%.2lf
    ",d[n-m-1]);
    }
    int main() {
        scanf("%d",&t);
        while(t--) {
            memset(edg,0,sizeof(edg));
            memset(lowcost,0,sizeof(lowcost));
            memset(d,0,sizeof(d));
            v=0;
            scanf("%d%d",&m,&n);
            for(int i=0;i<n;i++){
                scanf("%lf%lf",&a[i].x,&a[i].y);
                a[i].num=i;
            }
            Build();
            prim();
        }
        return 0;
    }
    View Code
  • 相关阅读:
    spring
    抽象和封装
    Oracle索引的原理
    使用JdbcTemplate.queryForObject 的注意点
    ORM是什么意思
    Java 后台处理数据库的二进制图片流
    Extjs girdPanel显示图片
    斜率dp
    多重背包的二进制优化
    POJ 3249 DAG图最短路
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5730404.html
Copyright © 2011-2022 走看看