poj2349Arctic Network(最小生成树kruscal+第k长的边)

题目链接:

啊哈哈,点我点我

题意:

北极的某区域共有n座村庄( 1 ≤ n ≤ 500 ),每座村庄的坐标用一对整数(x, y)表示,其中 0 ≤ x, y ≤ 10000。为了加强联系,决定在村庄之间建立通讯网络。通讯工具可以是无线电收发机,也可以是卫星设备。所有的村庄都可以拥有一部无线电收发机, 且所有的无线电收发机型号相同。但卫星设备数量有限,只能给一部分村庄配备卫星设备。  不同型号的无线电收发机有一个不同的参数d,两座村庄之间的距离如果不超过d就可以用该型号的无线电收发机直接通讯,d值越大的型号价格越贵。拥有卫星设备的两座村庄无论相距多远都可以直接通讯。

这道题目主要是那卫星不好处理,但是如果正面不好想,那么可以从逆向来想,如果知道了最小距离D,那么去掉最小生成树里面大于D的边,那么图会存在k个连通支,这里给一个定理:如果去掉所有权大于d的边后最小生成树被分割成k个连通分支,图也被分割成k个连通分支,。那么给每个连通支分一个卫星电话即可,那么这个最短距离D就变成了去掉大于D后的最大的边,即第k长的边。。。。那么这个问题就解决了。。。

题目:

Arctic Network

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10211   Accepted: 3374

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

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

const int maxn=500+10;
int s,p,root[maxn],cal;
double dis[maxn*maxn];

struct Point
{
    int x,y;
}point[maxn];

struct Edge
{
    int u,v;
    double distance;
}edge[maxn*maxn];

int findroot(int x)
{
    if(root[x]!=x)
        root[x]=findroot(root[x]);
    return root[x];
}

double caldistance(Point a,Point b)
{
    return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
}

bool cmp(Edge a,Edge b)
{
    return a.distance<b.distance;
}

bool cmp2(double a,double b)
{
    return a>b;
}

void kruscal()
{
    memset(dis,0.0,sizeof(dis));
    int sum=0,i,k=0;
    for(i=1;i<=cal;i++)
    {
        int fx=findroot(edge[i].u);
        int fy=findroot(edge[i].v);
        if(fx==fy)
            continue;
        else
        {
            root[fx]=fy;
            sum++;
            dis[sum]=edge[i].distance;
            if(sum==p-1)
                break;
        }
    }
    sort(dis+1,dis+1+sum,cmp2);
    printf("%.2f\n",dis[s]);
}

void read_graph()
{
    for(int i=1;i<=p;i++)
        root[i]=i;
    for(int i=1;i<=p;i++)
       scanf("%d%d",&point[i].x,&point[i].y);
    for(int i=1;i<p;i++)
       for(int j=i+1;j<=p;+j++)
    {
        double temp=caldistance(point[i],point[j]);
        edge[++cal].u=i;
        edge[cal].v=j;
        edge[cal].distance=temp;
    }
    sort(edge+1,edge+1+cal,cmp);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cal=0;
        scanf("%d%d",&s,&p);
        read_graph();
        kruscal();
    }
    return 0;
}

poj2349Arctic Network(最小生成树kruscal+第k长的边)

时间: 2024-12-16 10:13:44

poj2349Arctic Network(最小生成树kruscal+第k长的边)的相关文章

poj 2349 Arctic Network(最小生成树的第k大边证明)

题目链接: http://poj.org/problem?id=2349 题目大意: 有n个警戒部队,现在要把这n个警戒部队编入一个通信网络, 有两种方式链接警戒部队:1,用卫星信道可以链接无穷远的部队. 2,用信号收发器可以链接周围d米以内的部队. 现在有s个卫星信道,问d最小是多少时能连接全部的警戒部队? 解题思路: 我是用求最小生成树,记录路径长度,对路径长度排序后,第k长的边就是答案, 但是队友是用最小k度限制生成树,因为我的方法它证明不了,也推翻不了~~~~, 最后我下去仔细想了想反证

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, t

POJ_2349_Arctic Network(最小生成树)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11782   Accepted: 3867 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

QS Network(最小生成树)

题意:若两个QS之间要想连网,除了它们间网线的费用外,两者都要买适配器, 求使所有的QS都能连网的最小费用. 分析:这个除了边的权值外,顶点也有权值,因此要想求最小价值,必须算边及顶点的权值和. 解决方法:用prim算法,在构造邻接矩阵时,在i到j的权值的基础上再加上i点的权值和j点的权值即可. 附上AC代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #define infinity 1000

ZOJ1586——QS Network(最小生成树)

QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two network adapters (one for each QS) and a segm

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

Arctic Network——最小生成树

题目链接 题意: 给出p个节点的坐标,S个卫星,每个节点相互之间连通且边权为两点间距离,求这个图的最小生成树. S个卫星相当于S-1条边权值为0的边. 题解: 输出最小生成树中除去最长的s-1条边后最长的边的边权(即 (p-1)-(s-1)),保留两位小数. 因为 求的最小权值所以 让最长的的s-1条边权值为0 所以求第 p-s 条边的边权 代码: #include<iostream> #include<stdio.h> #include<math.h> #includ

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 tec

uva 10369 Arctic Network 最小生成树上的第k条边

开始的时候想错了,以为必须是邻接点才能使用卫星信号所以开始错了好几次!后来看了网上的说法才知道不是直接求出s-1大的边就可以了!(其实还是不明白题意) #include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include