POJ-2152 树形dp

2006陈启峰 - 消防站解题报告 2006陈启峰 - 消防站解题报告 poj 2152 fire 立即下载

题意:给你一颗树,边上权值表示距离,一个点上可以建消防站,花费为val[i],一个节点要么建消防站,要么周围d[i]距离内必须有一个消防站,求最小花费。

想了好久没想到,看了一眼题解才恍然大悟。我真的是太菜了!
题解:设ans[u]为以u为根的这棵子树的答案,dp[u][v]表示u节点被v保护的答案,dis[v]表示u到v的距离。若v可以保护u,则
dp[u][v]=val[v]+∑min(dp[i][v]−val[v],ans[i]);//其中i是u的直接子节点
ans[u]=min{dp[u][i]};

代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1010;
int dp[maxn][maxn],dis[maxn],val[maxn],d[maxn],ans[maxn];
struct node{
	int x;
	int w;
	node(int _x,int _w)
	{
		x=_x;
		w=_w;
	}
};
vector<node>a[maxn];
int t,n;
void Dfs(int u,int fa)
{
	for(int j=0;j<a[u].size();j++)
	{
		int v=a[u][j].x;
		ll w=a[u][j].w;
		if(v==fa) continue;
		dis[v]=dis[u]+w;
		Dfs(v,u);
	}
}
void dfs(int u,int fa)
{
	dp[u][u]=val[u];
	for(int j=0;j<a[u].size();j++)
	{
		int v=a[u][j].x;
		ll w=a[u][j].w;
		if(v==fa) continue;
		dfs(v,u);
	}
	dis[u]=0;
	Dfs(u,-1);
	for(int i=1;i<=n;i++)
		if(dis[i]<=d[u])///能被覆盖到
		{
			dp[u][i]=val[i];
			for(int j=0;j<a[u].size();j++)
			{
				int v=a[u][j].x;
				if(v==fa) continue;
				dp[u][i]+=min(ans[v],dp[v][i]-val[i]);///
			}
			ans[u]=min(ans[u],dp[u][i]);///
		}
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		scanf("%d",&val[i]);
		for(int i=1;i<=n;i++)
		scanf("%d",&d[i]);
		
		for(int i=1;i<=n;i++)
		a[i].clear();
		memset(dp,0x3f,sizeof dp);
		memset(ans,0x3f,sizeof ans);
		for(int i=1;i<n;i++)
		{
			int x,y,w;
			scanf("%d%d%d",&x,&y,&w);
			a[x].push_back(node(y,w));
			a[y].push_back(node(x,w));
		}
		dfs(1,-1);
		 
		printf("%d\n",ans[1]);
	}
	return 0;
}
基于CANoe的Basic Diagnostics ECU (UDS) 阅读详情

相关推荐

在Windows11下安装完Ubuntu20.04双系统后配置环境的过程

在Windows11下安装完Ubuntu20.04双系统后配置环境的过程 1.NVIDIA驱动检测及安装 2.Anaconda的安装 3.安装cuda、cudnn、pytorch 4.其他报错

aaaccc444的博客 2424

poj 2152 Fire (树形DP难题)

题目链接;https://vjudge.net/contest/246776#problem/C 题目大意:有n个城市,每两个城市只有一条路连接,(所以可以当成是一颗树)。现在要建设一些防火站,使每个城市都可以被覆盖。每个城市的属性是:在本城市建防火站的价钱和不在本城市建设的话,所依赖的别的防火站的最长距离。然后是n-1条路,和这条路的长度。现在问在覆盖所有城市的前提下的最少价格。 题目思路:...

qq_36782366的博客 412

AutoCAD Plant 3D自定义零件库实战:手把手教你用Python创建参数化槽式三通

本文详细介绍了如何利用Python脚本在AutoCAD Plant 3D中创建参数化槽式三通组件,从API原理到实战开发,帮助用户构建高效的自定义零件库。通过Python装饰器系统控制零件属性和行为,实现几何建模、参数管理和规格编辑器集成,提升工业管道设计的标准化和效率。

weixin_28267011的博客 331

POJ - 2152 Fire 树型DP

题目大意:有N座城市,要求在这N座城市中建一个消防系统,使得每座城市城市着火时都能被按时扑灭 现在给出每座城市建一个消防站所需的花费w和每座城市相邻消防站的最远距离lim(和该城市距离超过lim的城市的消防站无法救该城市的火),问要使所有的城市都能被救到火,建消防站的最小花费是多少解题思路:参考了别人的题解,果然还是太弱了。。。 要使该城市能被救到火,那么消防站不是建在该点,就是建在以该点为根的

静静地码 1121

Xilinx Vivado LDPC IP core,TSN IP core. 各种IP core

Xilinx Vivado LDPC IP core,TSN IP core. 各种IP core

qq_1432825237的博客 3991

5G初始注册流程详解(2)-Registration Request

1:了解注册的本质,5G终端为什么要注册? 2:SUCI,SUPI,5G-GUTI的区别? 3:为什么会有COMPLETE REGISTRATION REQUEST呢?

weixin_53220301的博客 3794

poj 2152 一道很难的树型DP

题目:poj 2152  Fire          我想说的:以前做背包的题目做多了,脑子形成了一种就是所有动态规划就是在数组上进行等一些固定的思想。结果最近在做一些题目的时候,感觉无从下手,想好久都没结果。非常庆幸自己做了这类题目,让我从那种狭隘的思想中做出来。以后就要根据实际情况研究状态了。          题意:有n个城市,每两个城市只有一条路连接,(所以可以当成是一颗树)。现在要建

nyist_jiang 3364

WinDbg调试工具安装全攻略:从下载到配置的保姆级教程(Windows 10/11适用)

本文提供了一份详细的WinDbg调试工具安装与配置指南,适用于Windows 10/11用户。文章对比了WinDbg Preview独立版与SDK内置经典版的区别,并手把手指导从Microsoft Store或Windows SDK完成安装。核心部分重点讲解了如何配置符号文件路径与源代码路径,这是让WinDbg发挥强大调试功能的关键步骤,并针对常见安装问题提供了实用的排错方法。

hp777的博客 385

[POJ2152]消防站解题报告

做这道题的时候,我一开始状态设计的太紧了,我设f(i)表示以i为根的子树自己搞定自己需要的最小代价,然后我枚举控制根的点。。然后发现转移是O(N^3)的。 看了论文之后发现其实改变一下状态就可以了,其实显然的一件事就是我们现在是在枚举两个点之间的关系了,那么我们不妨设f(i,j)为j控制i,且以i为根的子树都被完全搞定的最小代价;那么状态转移方程就呼之欲出了。 ①这道题告诉我最大的一点就是当我

TA 1140

K8S部署Java项目(Gitlab CI/CD自动化部署终极版)

每个人都有惰性,但不断学习是好好生活的根本,共勉!

感谢阅读 6954

ABAP RANGES及比较符号

RANGES *选项介绍: *1.SIGN 值为 I 和 E 。 I是包含, E是排除, 一般使用I。 2.OPTION 如果HIGH 为空 ,为单值选择 。有 EQ、NE、GT、LE、LT等逻辑操作 对于的 CP包含 NP是排除 如果HIGH 不为空 为区间选择 有BT,NB可选 *3.LOW 低值 *4.HIGH 高值 lt:less than 小于 le:less than or equal to 小于等于 eq:equal to 等于 ne:

Pegasus666的博客 5389

Fire POJ - 2152 (树形dp)

题意:给你一颗树,每个点都有一个w值,代表在这个点修建工程需要花费w,还有一个d值,代表离这个点距离小于d的点可以作为这个点的建立工程的点。每条边都有权重,求出覆盖所有点的最小花费。 思路:首先分析,是否具有最优子结构性质,发现任意一颗子树的最小值,都满足最优子结构。考虑动态规划的做法。 这个题的难点在于可以选一个点来覆盖其他点,所以我们的一个想法必然是枚举这个被选取的点。 设dp[u][j...

untilyouydc 480

【信号与系统】Multisim 仿真信号合成与分解

软件 Multisim 进行方波信号的产生、分解和合成,观察信号波形的分解与合成,加深对信号频谱的理解。

AXYZdong的博客 2万+

Fire (poj 2152 树形dp)

Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000)。现在要选择某些点,使得整棵树都被覆盖到。当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同时花费为v[i]。问最小花费。 以前做过一道类似的题(水库),这道题也差不多。首先来考虑,用\(best[i]\)表示以i为根的子树的最小花费。这样做有什么问题呢?它无法很好的处理消防站...

171

深度学习工具包 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")

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【POJ 2152】Fire【树形DP】

题意:给出一棵树,要求在节点出

Hacb的专栏 474

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

poj2152(Fire) 树形DP

题目链接:http://poj.org/problem?id=2152 题意:一棵带边权的树,边权表示节点间距离,在i上建立消防站的代价是w[i],如果在一点i没建消防站,那么它与距离这个点最近的消防站之间的距离不能大于d[i]。问满足建站最小的花费; 解法;看了陈启峰的论文才会的,感觉挺难的,不过论文里分情况讨论了,应该不需要;dp[i][j]表示在i处选择j处作为供应站(但是并不一

xiefubao的专栏 1039

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

POJ2152 树形DP

题意:这一题是陈启锋的论文中

江上白衣凌云 1003

POJ 2152 经典树形dp

Fire Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 972   Accepted: 463 Description Country Z has N cities, which are numbered from 1 to N. Cities are connect

_rabbit的专栏 1363

POJ 2152 Fire (树形dp)

转发一篇国家队论文。 树形dp 对这题讲的挺清楚的。 #include<iostream> #include<algorithm> #include<cstdio> #include<stdio.h> #include<string.h> #include<queue> #include<cmath> #inclu...

WayJasy的博客 242

【树形DP】POJ 2152 Fire

通道:http://poj.org/problem?id=2152 题意:n个城市,建防火站,花费w[i],如果这个城市没有防火站,则最近的不超过d[i],求最小花费 思路: 设dp[i][j]表示i点及其子树都符合情况下i点依赖j点的最小花费,有了这个似乎还不够,再开个一维数组best,best[i]表示以i为根的子树符合题目要求的最小花费。这样状态转移方程就是dp[i][j] ...

113

POJ2152 Fire(树形 dp)

Description 给定一棵无根有权树,每个点建消防站都有一定代价 ccc,每个点都有一个限制 limlimlim,表示离它最近的消防站的最大距离。求让所有点安全的最小代价。 Solution 设 fi,jf_{i,j}fi,j​ 为以 iii 为根的子树全安全,且 jjj 管理 iii 的最小代价,bestibest_ibesti​ 为以 iii 为根的子树全安全的最小代价,结果显然为 be...

ylxmf2005's Blog 201

poj 2152 Fire - 经典树形dp

题目链接: http://poj.org/problem?id=2152 不多说。。  2006 陈启峰消防站解题报告 这dp简直是神了。。 #include #include #include #include #include #include #include #include #include #include #include #includ

呼,是出一口气;吸,是争一口气! 666

【poj2152】Fire 树形DP

DescriptionCountry Z has N cities, which are numbered from 1 to N. Cities are connected by highways, and there is exact one path between two different cities. Recently country Z often caught fire, so t

DQS的树洞 1346

poj 2152 Fire (树形dp)

DescriptionCountry Z has N cities, which are numbered from 1 to N. Cities are connected by highways, and there is exact one path between two different cities. Recently country Z often caught fire, so t

阿柟喜欢吃小星星? 373

Poj 2152 Fire (DP_树形DP)

题目链接:http://poj.org/problem?id=2152 题目大意:给定n个节点组成的树,树有边权.现在要在一些点上建立消防站,每个点建站都有个cost[i],如果不在当前的点上建站,也要依赖其他的消防站,并且距离不超过limit[i]。求符合上述条件的最小费用建站方案。n 解题思路:复杂度为O(n^2)的树形DP.因为要依赖其他站点,所以不仅仅只从子树中获

3277

【POJ2152】Fire——树形DP

Fire 树形DP

Johann的博客 737
上一篇: CodeForces - 834D(线段树优化dp)
下一篇: Miller_Rabin素数测试[Fermat小定理][二次探测定理][同余式][Wilson定理]
GoLakerswxy
博客等级 码龄9年 41粉丝 158原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值