POJ2349 Arctic Network Prim+堆(优先队列)、Kruskal(并查集)

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

Waterloo local 2002.09.28


题意:给出一些村庄的坐标,要在他们之间建立通讯网络,可以使用卫星设备使得两个村庄的距离相当于为零,问连接所有村庄的路线中最大长度最小是多少?

那么,只需要求出其最小生成树的所有边,取第 S 长边即可,虽然同一张图有可能生成不同的最小生成树,但排序后结果是相同的。


Prim:

#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long LL;
const int maxn=505;
struct Node{
    double x,y;
}node[505];
struct Edge{
    int v;
    double w;
    Edge(int vv,double ww):v(vv),w(ww){}
    bool operator <(const Edge & e)const{
        return w>e.w;
    }
};
vector< vector<Edge> > G(510);

double HeapPrim(const vector< vector<Edge> > &G,int N,int s)
{
    Edge xDist(0,0);
    int sum=0;
    double ans[505];
    priority_queue<Edge> pq;
    vector<double> vDist(N);
    vector<int> vUsed(N);
    for(int i=0;i<N;i++){
        vUsed[i]=0;
        vDist[i]=INF*1.0;
    }
    int nDoneNum=0;
    pq.push(Edge(0,0));
    while(nDoneNum<N&&!pq.empty()){
        do{
            xDist=pq.top();
            pq.pop();
        }while(vUsed[xDist.v]&&!pq.empty());
        if(!vUsed[xDist.v]){
            if(xDist.w>0){
                ans[sum++]=xDist.w;
            }
            vUsed[xDist.v]=1;
            nDoneNum++;
            for(int i=0;i<G[xDist.v].size();i++){
                int k=G[xDist.v][i].v;
                if(!vUsed[k]){
                    double w=G[xDist.v][i].w;
                    if(vDist[k]>w){
                        vDist[k]=w;
                        pq.push(Edge(k,w));
                    }
                }
            }
        }
    }
    sort(ans,ans+sum);
    return ans[sum-s];
}

int main()
{
    int t,s,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&s,&n);
        for(int i=0;i<n;i++){
            G[i].clear();
        }
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&node[i].x,&node[i].y);
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<i;j++){
                double w=sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x)+(node[i].y-node[j].y)*(node[i].y-node[j].y));
                G[i].push_back(Edge(j,w));
                G[j].push_back(Edge(i,w));
            }
        }
        printf("%.2f\n",HeapPrim(G,n,s));
    }
    return 0;
}

Kruskal:

#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long LL;
#define maxn 505
struct Node{
    double x,y;
}node[505];

struct Edge{
    int s,e;
    double w;
    Edge(int ss,int ee,double ww):s(ss),e(ee),w(ww){}
    Edge(){}
    bool operator <(const Edge &e)const{
        return w<e.w;
    }
};

vector<Edge> edges(maxn*maxn);
vector<int> parent(maxn);
vector<double> ans(maxn);

int GetRoot(int a)
{
    if(parent[a]!=a){
        parent[a]=GetRoot(parent[a]);
    }
    return parent[a];
}

void Merge(int a,int b)
{
    int p=GetRoot(a);
    int q=GetRoot(b);
    if(p==q){
        return;
    }
    parent[q]=p;
}

int main()
{
    int t,s,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&s,&n);
        parent.clear();
        edges.clear();
        ans.clear();
        for(int i=0;i<n;i++){
            parent.push_back(i);
        }
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&node[i].x,&node[i].y);
        }
        for(int i=0;i<n;i++){
            edges.push_back(Edge(i,i,0));
            for(int j=0;j<i;j++){
                double w=sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x)+(node[i].y-node[j].y)*(node[i].y-node[j].y));
                edges.push_back(Edge(i,j,w));
                edges.push_back(Edge(j,i,w));
            }
        }
        sort(edges.begin(),edges.end());
        int nDone=0;
        for(int i=0;i<edges.size();i++){
            if(GetRoot(edges[i].s)!=GetRoot(edges[i].e)){
                Merge(edges[i].s,edges[i].e);
                nDone++;
                ans.push_back(edges[i].w);
            }
            if(nDone==n-1) break;
        }
        sort(ans.begin(),ans.end());
        printf("%.2f\n",ans[n-1-s]);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值