UVA-10369 Arctic Network (最小生成树)

题目大意:n个村庄的坐标已知,现在要架光纤使所有的村庄都能上网,但受光纤的参数d所限,每根光纤只能给距离不超过d的村庄之间连接。但是有s个信号机,信号机之间能无限畅连。考虑到光纤的价格和参数d有关,现在要确定最小的参数。

题目分析:最好的方案当然是把s个信号机都用上,这就相当于在一张必选s-1条边(边长视为为0)的无向图中构建最小生成树,答案为树中最长的边长。不过,必选哪s-1条边是不确定的。

代码如下:

# include<iostream>
# include<cstdio>
# include<cmath>
# include<cstring>
# include<algorithm>
using namespace std;
# define REP(i,s,n) for(int i=s;i<n;++i)
# define CL(a,b) memset(a,b,sizeof(a))
# define CLL(a,b,n) fill(a,a+n,b);
# define LL long long

const int N=505;
const int INF=1<<30;
const double EXP=1e-3;
struct Edge
{
    int fr,to;
    double w;
    Edge(){}
    Edge(int _fr,int _to,double _w):fr(_fr),to(_to),w(_w){}
    bool operator < (const Edge &a) const {
        return w<a.w;
    }
};
Edge e[N*(N-1)/2];
int n,s,x[N],y[N],m,fa[N];

double dist(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}

int findFa(int u)
{
    if(u!=fa[u])
        return fa[u]=findFa(fa[u]);
    return u;
}

double kruskal()
{
    double res=0.0;
    REP(i,0,n) fa[i]=i;
    int k=n-s;
    for(int i=0;i<m&&k>0;++i){
        int u=findFa(e[i].fr);
        int v=findFa(e[i].to);
        if(u!=v){
            fa[u]=v;
            --k;
            res=e[i].w;
        }
    }
    return res;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&s,&n);
        REP(i,0,n) scanf("%d%d",x+i,y+i);
        m=0;
        REP(i,0,n) REP(j,i+1,n)
            e[m++]=Edge(i,j,dist(i,j));
        sort(e,e+m);
        printf("%.2lf\n",kruskal());
    }
    return 0;
}

  

时间: 2024-08-02 19:09:32

UVA-10369 Arctic Network (最小生成树)的相关文章

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

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

UVA 10369 Arctic Network

题目来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1310 最小生成树问题,Prim算法在这种给出坐标的情况相对Kruskal算法优势还是很大. 1 /*AC 19ms*/ 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<math.h> 5 #

UVA 10369 Arctic Network【最小生成树】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1310 题意: 有m个卫星,n个站点,卫星可以不用代价相连,剩下要用无线电连,求无线电连接中最大距离的最小 解法: 最小边开始加入,这样第n - m 条边的权值就是答案 代码:略 版权声明:转载请注明出处.

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

10369 - Arctic Network

这题的意思是给出p个点,然后p的点可以由无线电通信或者是卫星通信,然后只有m的点能通过卫星通信,然后用无线电通信的点的距离不能超过D超过D的话惠氏通信的成本增加,问在使通信成本就低的情况下求D 这题还是最小生成树,用prim算法构造MST,在构造的过程将每次算的最小的权值存到数组f里面,然后按照升序排序.因为卫星通信可以无限远,就是说后m的权值可以不用算,所以答案就是f[p-m]. #include<stdio.h> #include<string.h> #include<i

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

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

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

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