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>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 2010;
int f[maxn];
double x[maxn],y[maxn];
int n,cnt,p,s;
struct node
{
    int u,v;
    double w;
    bool operator < (const node &a)const
    {
        return w<a.w;
    }
} edge[maxn*maxn];

int Find(int x)
{
    return x==f[x]?x:f[x]=Find(f[x]);
}
void add(int u,int v,double w)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt++].w=w;
}

double kruskal()
{
    int sum=0;
    for(int i=0; i<=p; i++)f[i]=i;
    sort(edge,edge+cnt);
    for(int i=0; i<cnt; i++)
    {
        int x=edge[i].u;
        int y=edge[i].v;
        int fx=Find(x);
        int fy=Find(y);
        if(fx!=fy)
        {
            sum++;
            f[fx]=fy;
        }
        if(sum==p-s)return edge[i].w;
    }
    return 0.0;
}
double get_dis(int i,int j)
{
    double dis=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
    //printf("%.2f\n",dis);
    return dis;
}
int main()
{
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d",&s,&p);

        for(int i=1;i<=p;i++)scanf("%lf%lf",&x[i],&y[i]);
        cnt=0;
        for(int i=1;i<=p;i++)for(int j=i+1;j<=p;j++)
        {
            double dis=get_dis(i,j);
            add(i,j,dis);
        }
        printf("%.2f\n",kruskal());
    }
    return 0;
}

kruskal

原文地址:https://www.cnblogs.com/j666/p/11616949.html

时间: 2024-08-14 06:08:01

Arctic Network——最小生成树的相关文章

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

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

poj2349 Arctic Network - 最小生成树

2017-08-04 16:19:13 writer:pprp 题意如下: 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 o

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

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

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

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

Arctic Network (poj 2349 最小生成树)

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

[Poj2349]Arctic Network(二分,最小生成树)

[Poj2349]Arctic Network Description 国防部(DND)要用无线网络连接北部几个哨所.两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道. 任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置.同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率.功率越大,D也越大,但成本更高.出于采购和维修的方便,所有哨所的收发器必须是相同的:那就是说,D值对每一个哨所相同. 你的任务是确定收发器的D的最

POJ2349:Arctic Network(二分+最小生成树)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28311   Accepted: 8570 题目链接:http://poj.org/problem?id=2349 Description: The Department of National Defence (DND) wishes to connect several northern outposts by a wireless net

[kuangbin带你飞]专题六 最小生成树 G - Arctic Network

G - Arctic Network 题目链接:https://vjudge.net/contest/66965#problem/G 题目: 国防部(DND)希望通过无线网络连接几个北部前哨站.在建立网络时将使用两种不同的通信技术:每个前哨站都有一个无线电收发器,一些前哨站还有一个卫星信道.    任何带卫星频道的两个前哨站都可以通过卫星进行通信,无论其位置如何.否则,两个前哨只有当它们之间的距离不超过D时才可以通过无线电进行通信,这取决于收发器的功率.更高的功率产生更高的D但成本更高.由于采购