POJ2349Arctic Network之最小生成树kruskal解法

来源:互联网 发布:j2ee服务器端高级编程 编辑:程序博客网 时间:2024/05/17 01:16

Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23142 Accepted: 7110
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


  • 题意:有s个卫星信道和p个前哨站,有卫星信道的那s个前哨站可以直接通信,求最小生成树中最大的那条边的值.
  • 分析:易知s个卫星信道的存在可以减少最小生成树中的s-1条边,无论这s-1条边是否相邻,那么生成最小生成树过程中,把每条边的值用数组保存起来,排序后ans[(V-1)-(s-1)-1]即为所求

#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;int n;int s,p;int V,E;double x[505];double y[505];double ans[505];int par[505];int ran[505];struct edge{    double v,to,cost;};struct edge ed[300000];void init(int n){    for (int i = 0; i < n; i += 1){        par[i] = i;        ran[i] = 0;    }}int find(int x){    if(par[x] == x) return x;    else return par[x] = find(par[x]);}void unite(int x,int y){    x = find(x);    y = find(y);    if(x == y) return;    if(x < y) par[x] = y;    else{        par[y] = x;        ran[x]++;    }}bool same(int x,int y){    return find(x) == find(y);}int cmp(edge e1,edge e2){    return e1.cost < e2.cost;}void kruskal(){    V = p;    sort(ed,ed+E,cmp);    init(V);    int k = 0;    for (int i = 0; i < E; i += 1){        edge e = ed[i];        if (!same(e.v,e.to)){            unite(e.v,e.to);            //数组赋值            ans[k] = e.cost;            k++;        }    }    sort(ans,ans+k);    printf("%.2lf\n",ans[(V-1)-(s-1)-1]);}int main(){    cin >> n;    while (n--){        cin >> s >> p;        for (int i = 0; i < p; i += 1){            cin >> x[i] >> y[i];        }        E = 0;        for (int i = 0; i < p; i += 1){            for (int j = 0; j < p; j += 1){                ed[E].v = i;                ed[E].to = j;                ed[E].cost = sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2));                E++;            }        }        kruskal();    }    return 0;}

用kruskal解题纯粹是为了练习这个算法,但是这个方法显然写起来有点麻烦…

原创粉丝点击