UVA 10369 - Arctic NetWork (求最小生成树)
题意:
在南极有 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 (求最小生成树)的更多相关文章
- uva 10369 Arctic Network (最小生成树加丁点变形)
The Department of National Defence(DND)wishestoconnectseveral northern outposts by a wireless networ ...
- uva 10369 Arctic Network
题意: 有许多基地,每个基地都有两种收发信号的方式,一种是通过无线电收发机,另一种是通过卫星.两个基地之间可以通过卫星交流不管它们相距多远:但是通过无线电交流,就要求它们的距离不超过D.为了方便布置, ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/F Description The Departme ...
- POJ 2349 Arctic Network(最小生成树,第k大边权,基础)
题目 /*********题意解说——来自discuss——by sixshine**************/ 有卫星电台的城市之间可以任意联络.没有卫星电台的城市只能和距离小于等于D的城市联络.题 ...
- 【UVA 10369】 Arctic Network (最小生成树)
[题意] 南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连.任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远. 而安装有无线电设备的两个站 ...
- POJ-2349 Arctic Network(最小生成树+减免路径)
http://poj.org/problem?id=2349 Description The Department of National Defence (DND) wishes to connec ...
- POJ 2349 Arctic Network(最小生成树中第s大的边)
题目链接:http://poj.org/problem?id=2349 Description The Department of National Defence (DND) wishes to c ...
- UVA-10369 Arctic Network (最小生成树)
题目大意:n个村庄的坐标已知,现在要架光纤使所有的村庄都能上网,但受光纤的参数d所限,每根光纤只能给距离不超过d的村庄之间连接.但是有s个信号机,信号机之间能无限畅连.考虑到光纤的价格和参数d有关,现 ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
随机推荐
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目5
2014-03-18 01:40 题目:对字符串进行类似游程编码的压缩,如果压缩完了长度更长,则返回不压缩的结果.比如:aabcccccaaa->a2b1c5a3,abc->abc. 解法 ...
- javascript 数组的常用方法总结
前言 主要讨论一下数组的方法, 1.splice和slice的区别 2.pop和push 3.shift和unshift 4.join 5.forEach(es ...
- .netCore 反射 :Could not load file or assembly 系统找不到指定文件
“System.IO.FileNotFoundException:“Could not load file or assembly 'ClassLibrary2, Culture=neutral, P ...
- 【转载】Linux下安装LoadRunner LoadGenerator
原文地址:[转载]Linux下安装LoadRunner LoadGenerator作者:邱建忠tester LR的负载机安装在linux的理由: 1.windows xp,双核+4G内存,基本上每个v ...
- JMeter学习笔记(九) 参数化1--函数助手:_CSVRead
1.函数助手:_CSVRead 1)准备数据文件 ,文件可以是.csv格式,.dat格式,txt格式等 2)打开函数助手,生成参数 3)添加HTTP请求,引用参数 4)执行HTTP请求,察看结果树中的 ...
- Monkey、Monkeyrunner之间的区别
Monkey.Monkeyrunner之间的区别 一.Monkey Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输 ...
- ymal
https://blog.csdn.net/beginya/article/details/76768968 https://www.jianshu.com/p/97222440cd08
- android studio 配置网络代理
1.首先在vultr网站购买服务器. 然后使用shadowsocksR给服务器配置FQ,再在本地机器配置好shadowsocksR. 参考网址:https://github.com/getlanter ...
- hadoop 客户的的使用
${HADOOP_HOME}/bin/hadoop job Usage: JobClient <command> <args> [-submit <job-file> ...
- .export*读取图片
*读取图片 read_image(Image,'D:/MyFile/halcon/数字识别/1.jpg define PHYS_FLASH2_1 0xBC000000 /* Image2 Bank # ...