[poj 2349]Arctic Network

【代码超详解】POJ 2349 Arctic Network(Kruskal 算法) 一、题目描述 阅读详情

题意:给p个点,要在这些点中选一些建立无线电站,使得每两个点之间都可以通讯。另外有s个通讯卫星,两个通讯卫星之间无论距离长短都可以自由通信。但是无线电站的规格必须相同,即任意两个无线电站的通讯距离是相同的。比如题目给的样例,我们可以选择(0,100)和(0,300)各建一个无线电站,它们之间通过无线电通信,(0,600)和(150,750)之间类似,然后这四个点被分为了两组,这两组之间通过卫星通信,这个方案可以使得无线电的通讯距离最短,也就是(0,600)和(150,750)间的距离。

思路:本例课通过Kruskal算法求最小生成树得到答案。我们知道,在没有建边时,各个点自己独立为一个连通分量,共有p个,我们通过建边,最后的目的是把它们分成s个(s即为卫星数量)连通分量,然后这些连通分量和连通分量之间就可以直接通过卫星通信。某个连通分量内部间各点则通过无线电通信。由Kruskal算法的思想我们可以分析出来,显然,如果两个点在同一个连通分量里,我们是没有必要在它们之间建边的。从而我们得到算法:

1初始化求出两两点之间的距离,得到p*(p-1)/2条边

2每次从剩下的边中选出距离最短的一条,如果这个边的两端属于相同的连通分量,则删除,继续本步。如果已经没有边可选,直接退出

3将选出的这条边的两端连接,并用这条边的长度更新答案

4重复上述过程直到剩余s个连通分量。这里我们知道,原来一共有p个连通分量,每次加边都会使得连通分量减少一,所以上述循环总共需要p-s次。

由于每次需要选择最短的边,我们用优先队列优化这个过程。判断连通分量可以用并查集。如此可以得到代码:

/*
	初始化每个点一个连通分量 
	每次只需要选最小边即可,然后在剩下s个连通分量时答案就更新完毕鸟
*/ 
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const double INF = 1e10;
const int MAX = 512;
struct Point {
	double x, y;
	double dis(const Point& B) {
		return sqrt((x - B.x) * (x - B.x) + (y - B.y ) * (y - B.y));
	}
} a[MAX];
struct edge {
	int from, to;
	double cost;
};
struct cmp {
	bool operator()(edge& e1, edge& e2) {
		return e1.cost > e2.cost;
	}
};

int father[MAX];

int find(int x) {
	return x == father[x] ? x : father[x] = find(father[x]);
}
void Union(int x, int y) {
	int fx = find(x), fy = find(y);
	if (fx != fy) {
		father[fx] = fy;
	}
}

double gao(int s, int p) {
	priority_queue<edge, vector<edge>, cmp> Q;
	for (int i = 0; i < p; ++i) {
		for (int j = i + 1; j < p; ++j) {
			Q.push((struct edge){i, j, a[i].dis(a[j])});
		}
		father[i] = i;
	}
	double ans = 0.0;
	for (int i = s; i < p; ++i) {
		edge e;
		e.from = e.to = e.cost = 0.0;
		while (!Q.empty()) {
			e = Q.top();
			Q.pop();
			if (find(e.from) != find(e.to)) break;
		}
		
		Union(e.from, e.to);
		if (ans < e.cost) ans = e.cost;
	}
	return ans;
}

int main() {
	int T;
	scanf(" %d", &T);
	while (T--) {
		int s, p;
		scanf(" %d %d", &s, &p);
		for (int i = 0; i < p; ++i) {
			scanf(" %lf %lf", &a[i].x, &a[i].y);
		}
		printf("%.2f\n", gao(s, p));
	}
	return 0;
}



本届挑战赛亚军方案:基于大模型和多AGENT协同的运维 我们分别构建了异常检测基础模型,一旦系统接受到异常即会通知LLM主管Agent,主管Agent对异常问题进行决策与子领域Agent进行桥接,多个子领域Agent协同工作,实现运维故障自动诊断和多个任务模型的编排,提升运维效率。 阅读详情

相关推荐

随机森林模型及案例(Python)

1 集成模型简介 集成学习模型使用一系列弱学习器(也称为基础模型或基模型)进行学习,并将各个弱学习器的结果进行整合,从而获得比单个学习器更好的学习效果。 集成学习模型的常见算法有Bagging算法和Boosting算法两种。 Bagging算法的典型机器学习模型为随机森林模型,而Boosting算法的典型机器学习模型则为AdaBoost、GBDT、XGBoost和LightGBM模型。 1.1 Bagging算法简介 Bagging算法的原理类似投票,每个弱学习器都有一票,最终根据所有弱学习器的投

qq_42433311的博客 4万+

POJ 2349 Arctic Network

POJ 2349 Arctic Network 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 ...

旺崽的博客 3096

UE5与Cesium for Unreal结合:打造高效离线地图解决方案

本文详细介绍了如何利用UE5与Cesium for Unreal结合打造高效离线地图解决方案,特别适用于数字孪生、军事仿真等网络不稳定场景。通过优化配置、数据处理和性能调优,实现TB级地形数据的流畅渲染,并结合实战案例展示了在油田巡检等复杂环境中的应用效果。

weixin_29007135的博客 509

[POJ 2349] Arctic Network

[题目链接] http://poj.org/problem?id=2349 [算法] 首先,我们发现答案是具有单调性的,所以我们可以二分答案 检验时,我们将所有距离小于二分值的点连边,然后判断联通块数量是否小于等于S即可 [代码] #include <algorithm> #inc...

182

C#计算八字

private string[] jq = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };private string[] dz = { "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥", "子" };

m0_72939038的博客 839

YOLOv8改进,YOLOv8添加可变性卷积DCNv2

DCNv2对原始的DCNv1进行了改进,可变形卷积网络的卓越性能源于其适应对象几何变化的能力。通过对其自适应行为的检查,虽然对其神经特征的空间支持比常规的ConvNets更接近于对象结构,但这种支持可能远远超出感兴趣区域,导致特征受到不相关图像内容的影响。为了解决这个问题,我们提出了一种可变形卷积网的重新表述,通过提高建模能力和更强的训练,提高了其专注于相关图像区域的能力。通过更全面地集成网络中的可变形卷积,并引入扩展变形建模范围的调制机制,增强了建模能力。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 3620

poj2349 Arctic Network

题目链接:http://poj.org/problem?id=2349 题意:有s颗星球,有p个雷达站,如果雷达站在同一个星球上,不需要距离就可以直接通信,如果不在一个星球上,他们之间的距离不能超过D,现在给你p个雷达站的坐标,如果要把这整个雷达站连通,问你最小的D是多少 解析:首先要联通全部雷达站,肯定是最小生成树,然后让你求一个D值,雷达站之间连边,相当于如果距离大于D就不能相连,那么可以先

22h's Blog 434

SAR合成孔径雷达成像及处理(附带Matlab源代码)

总结:本文介绍了SAR合成孔径雷达的基本原理,并提供了使用Matlab实现SAR成像和处理的源代码示例。然后,在慢时间域进行方位压缩处理,通过计算每个接收通道的延迟和频移,将多次接收的信号叠加得到最终的成像结果。SAR成像的基本原理是通过接收雷达发射的脉冲信号的回波,并利用多次接收的信号进行叠加处理。以多普勒频移校正为例,根据雷达飞行速度和方位角,计算实际的多普勒频率,并对每个接收通道的数据进行频移校正,以消除因目标运动引起的频率偏移。通过调整参数和算法,可以实现不同的SAR成像和处理效果。

ByteJolt的博客 692

【POJ】[2349]Arctic Network

理解题意之后也并不算太难 大意是给了一些坐标来求最小生成树 然而让输出的是最大边的值,而因为还有卫星可供通讯 所以只需要当有P个点S个卫星时 共P-1条边里舍去S-1条大的边所以这里我感觉用Kruskal更好一些 而且当取了 P-1-(S-1)==P-S条边时,这一条边便是所要求边 不需要进行后续操作了

BoilTask Talk Show 532

【Unity的HDRP渲染管线下实现好用的GUI模糊和外描边流光效果_Blur_OutLine_案例分享(内附源码)】

因为没有用到纹理,但介于是UI层,没有MainTeX的时候,会提示报错,所以这里加上了MainTex,最后不在片段着色渲染出来既可。这样UI层级显示正常了,但旋转后的模糊效果,跟背景拼接不上,有很大的偏移。猜测是屏幕UV采样错误导致,只要找到屏幕UV数值,更正过来就可以了,RenderTexture设置一下Size既可,其他的先不管。没有贴图,用Shader实现外描边流光效果。将主相机拖入Render Camera。新建一个RenderTexture。

暴走约伯的博客 1157

Python实现将txt文件转换成对应的excel或csv文件

此外,在上面的例子中,txt文件是以“空格”分隔的,如果遇到“逗号”分隔的,只需将上述代码中的。本文是该专栏的第29篇,后面会持续分享python的各种干货知识,值得关注。工作上可能会遇到这样的情况,使用python将某个txt文本,按照。转换成对应的excel文件或者是csv文件。”需要替换成你的目标txt文件的绝对路径。库,这里直接引用就好。需要注意的是,上述代码中的“

Python进阶专栏《爬虫实战进阶》,《Pyppeteer从入门到精通》原创作者 4516

POJ2349 Arctic Network

题目链接:http://poj.org/problem?id=2349 求最小生成树中第S大的边,结点给的是坐标(坑爹啊) #include #include #include #include #include #include using namespace std; #define N 500 struct edge { int u, v; double w;

浅雨歌 518

等保测评-达梦数据库

一般适用于等保测评

m0_54146694的博客 1312

Arctic Network POJ - 2349

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: ev...

aocan6909的博客 142

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

poj 2349 Arctic Network

Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5764 Accepted: 2007 题目链接:http://poj.org/problem?id=2349 Description The Department

CSDN515的专栏 1131

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

【POJ 2349 --- Arctic Network】prim || kruskal

【POJ 2349 --- Arctic Network】prim || kruskal 题目来源:点击进入【POJ 2349 — Arctic Network】 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network...

来日浅谈的博客 545

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

poj 2349 Arctic Network

http://poj.org/problem?id=2349 Arctic Network Time Limit:2000MS Memory Limit:65536K Total Submissions:12544 Accepted:4097 Description The Department of Nation...

deyou1988的专栏 134

POJ-2349-Arctic Network

链接:https://vjudge.net/problem/POJ-2349#author=clzls 题意: 国防部(DND)要用无线网络连接北部几个哨所。两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫星频道。 任何两个有卫星信道的哨所可以通过卫星进行通信,而不管他们的位置。同时,当两个哨所之间的距离不超过D时可以通过无线电通讯,D取决于对收发器的功率。...

weixin_33705053的博客 119

POJ - 2349 Arctic Network

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 net...

GxhZxx的博客 319

POJ-2349 Arctic Network

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Tw

JSure的代码库 964

POJ 2349[Arctic Network]

这个题Prim是比较快的,用Kruskal要700+ms,幸亏时限是2000ms…… 这个题就是建图之后求最小生成树中第p-s小的那条边,用kruskal比较容易理解~ Var a,b,f,x,y:array[1..1000000]of longint; e:array[1..1000000]of extended; n,m,an...

161
上一篇: [Java学习]Java中String的内存机制/String的比较
下一篇: [poj2777 Count Color]线段树
bit_line
博客等级 码龄12年 33粉丝 150原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址