题意:

  在南极有  N  个科研站,要把这些站用卫星和无线电连接起来,是的任意两个之间都能互相通信,如果其中任意的一个地方安装了卫星,那么就可以和其他安装卫星的互相通信,和距离没有关系,但是安装无线电 是需要费用D的,这个费用 D 为在安装无线电的地方中使用的无线电通信花费最大的那个值。现在有S个卫星可以提供给你安装,还有足够多的无线电设备,让你设计一个方案,使得D的费用最少。

思路:

  首先肯定会用到最小生成树。为了使得我们的D值最小那么我们应该给距离最长的 S - 1个路径的俩段安装无线电(因为 S个无线电可以连通S 个科研站,S个科研站有S- 1条路径)。这样的话设最小生成树一共有 K 条边,那么 第 K - S 条排好序的边就是我们要的答案。而 K = N - 1,所以我们最后要求的就是直接求最小生成树的 第 N - S 条边。

代码:

 import java.util.Scanner;
import java.util.Comparator;
import java.util.Arrays;
import java.text.DecimalFormat; class Node{
public int u, v, mark;
public double w;
}
class Points{
public double x;
public double y;
} class mycmp implements Comparator<Node>{
public int compare(Node A, Node B){
if(A.w == B.w) return 0;
else if(A.w < B.w) return -1;
else return 1;
}
}
public class Main {
final static int MAXN = 250000 + 13;
final static int INF = 0x3f3f3f3f;
static int[] pre = new int[MAXN];
static Node[] map = new Node[MAXN];
static Points[] pit = new Points[MAXN];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T != 0){
int S, P;
S = sc.nextInt();
P = sc.nextInt();
mst(P);
for(int i = 1; i <= P; i++){
pit[i] = new Points();
pit[i].x = sc.nextInt();
pit[i].y = sc.nextInt();
}
int len = 1;
for(int i = 1; i < P; i++){
for(int j = i + 1; j <= P; j++){
map[len] = new Node();
map[len].u = i;
map[len].v = j;
map[len].w = dist(pit[i], pit[j]);
len++;
}
}
Arrays.sort(map, 1, len, new mycmp());
int k = P - S; //第 K 条边就是答案
double ans = ksu(P, len, k);
DecimalFormat df = new DecimalFormat("0.00");
String db = df.format(ans);
System.out.println(db);
T--;
}
sc.close();
}
public static double dist(Points a, Points b){
return Math.sqrt( (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) );
}
public static double ksu(int N, int M, int k){
int cnt = 0;
double ans = map[M - 1].w;//初始化为最大的那条边
for(int i = 1; i < M; i++){
int fu = Find(map[i].u);
int fv = Find(map[i].v);
if(fu != fv){
cnt++;
if(cnt == k){ //找到第K条边
ans = map[i].w;
}
pre[fv] = fu;
map[i].mark = 1;
}
}
return ans;
}
public static int Find(int x){
return x == pre[x] ? x : (pre[x] = Find(pre[x]));
}
public static void mst(int N){
for(int i = 1; i <= N; i++){
pre[i] = i;
}
}
}
												

UVA 10369 - Arctic NetWork (求最小生成树)的更多相关文章

  1. uva 10369 Arctic Network (最小生成树加丁点变形)

    The Department of National Defence(DND)wishestoconnectseveral northern outposts by a wireless networ ...

  2. uva 10369 Arctic Network

    题意: 有许多基地,每个基地都有两种收发信号的方式,一种是通过无线电收发机,另一种是通过卫星.两个基地之间可以通过卫星交流不管它们相距多远:但是通过无线电交流,就要求它们的距离不超过D.为了方便布置, ...

  3. POJ 2349 Arctic Network (最小生成树)

    Arctic Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/F Description The Departme ...

  4. POJ 2349 Arctic Network(最小生成树,第k大边权,基础)

    题目 /*********题意解说——来自discuss——by sixshine**************/ 有卫星电台的城市之间可以任意联络.没有卫星电台的城市只能和距离小于等于D的城市联络.题 ...

  5. 【UVA 10369】 Arctic Network (最小生成树)

    [题意] 南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连.任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远. 而安装有无线电设备的两个站 ...

  6. POJ-2349 Arctic Network(最小生成树+减免路径)

    http://poj.org/problem?id=2349 Description The Department of National Defence (DND) wishes to connec ...

  7. POJ 2349 Arctic Network(最小生成树中第s大的边)

    题目链接:http://poj.org/problem?id=2349 Description The Department of National Defence (DND) wishes to c ...

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

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

  9. POJ 2349 Arctic Network (最小生成树)

    Arctic Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

随机推荐

  1. sql 删除重复的类型并且时间相同的项

    delete RemoteDetection WHERE REMOTEDETECTIONID IN ( select ID from ( select MIN(REMOTEDETECTIONID) I ...

  2. ipa和ironic-conductor交互

    IPA使用lookup和hearteat机制与Ironic Conductor进行交互,启动时agent给Conductor的vendor_passthru lookup endpoint(地址为/v ...

  3. HDU 4472 Count (DP)

    题目:问n个节点构成完全对称的树有多少种方法. 因为树是完全对称的,所以它的子树也是完全对称的. 对于每个树,拿出一个根节点,枚举剩下的节点能拆分成多少个子树. #include <cstdio ...

  4. HDU 4101 Ali and Baba (思路好题)

    与其说这是个博弈,倒不如说是个搜索.这题思路不错,感觉很难把情况考虑周全. 在地图外围填充一圈0,两次BFS,第一次从-1点出发,把从-1到达的0点以及包围0的那一圈石头标记出来.如下图: 1 1 1 ...

  5. Thread suspend()挂起resume()恢复

    import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.Actio ...

  6. CentOS程序 开机启动设置与chkconfig命令学习

    CentOS设置程序开机启动的方法: 1.启动命令添加到/etc/rc.d/rc.local 文件中, 如: vim /etc/rc.d/rc.local #!/bin/sh # # This scr ...

  7. 【bzoj3669】[Noi2014]魔法森林 Kruskal+LCT

    原文地址:http://www.cnblogs.com/GXZlegend/p/6797748.html 题目描述 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看 ...

  8. php记日志

    就是把log追加到文件中 用到了一个方法 file_put_contents <?php file_put_contents('a',date('Y m d h:i:s').' some tex ...

  9. 刷题总结——子串(NOIP2015)

    题目: 题目背景 NOIP2015 提高组 Day2 T2 题目描述 有两个仅包含小写英文字母的字符串 A 和 B .现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在 ...

  10. Springboot+Redis序列化坑

    今天在测试springboot整合redis的时候遇到下面这个坑,百度来百度去发现提示都是ajax的问题,真的是醉了,错误提示如下所示,不信大家可以直接复制百度一下答案是什么(流泪中....),错误如 ...