Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path
D. The Fair Nut and the Best Path
题目链接:https://codeforces.com/contest/1084/problem/D
题意:
给出一棵树,走不重复的路径,每到一个结点加上其权值,经过一条边减去其权值,路径中途减去后不能出现负数,问怎么选择路径可以让最后得到的最大。
题解:
这题考虑用dp来做。
我们定义dp[u]为走到u点的最大值,注意这里的方向,是走到u点。题目中的意思是路径不能走回头路。
对于一个父节点u,那么我们可以根据走到其儿子结点的最大值来更新经过父节点的路径的最大值。
如果两个儿子到u(dp[v1]+dp[v2]-dis(v1,u)-dis(v2,u))都为正数,那么此时ans = a[u]+dp[v1]+dp[v2]-dis(v1,u)-dis(v2,u)。在这里虽然dp指的是走到当前结点的最大值,但是反过来走并不影响其结果。
现在说说反过来走为什么并不影响结果,假设对于从v1已经走到了u,现在u点的值为x。
设v2的dp值为y,dis(v2,u)=z 。假设y+z>0,但x+z<0,也就是不能从u走到v2时但可以从v2走到u时,我们求ans时,会更新x=x+y+z(实际并不能走过去),但根据x+z<0有x+y+z<y。
所以现在并不影响最大值。最后更新dp[u]时也不影响。
对于另外一种情况v2可以走到u,u也可以走到v2,这种情况的证明就显而易见了。
然后具体做法就是每次根据其儿子维护经过当前结点的最大值,在最后根据其儿子选择一个较大值更新当前结点的dp值。
这题要用大最大连续子段和的技巧...
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+;
int n;
ll a[N],dp[N];
ll ans;
vector <pair<int,int> > g[N];
void dfs(int node,int pa){
ans=max(ans,a[node]);
ll mx=;
for(auto v:g[node]){
if(v.first==pa) continue ;
dfs(v.first,node);
ans=max(ans,mx+dp[v.first]-v.second+a[node]);
mx=max(mx,dp[v.first]-v.second);
}
dp[node]=mx+a[node];
return ;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
}
for(int i=;i<n;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
g[u].push_back(make_pair(v,c));
g[v].push_back(make_pair(u,c));
}
dfs(,-);
cout<<ans<<endl;
return ;
}
Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path的更多相关文章
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings
E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的 ...
- Codeforces Round #526 (Div. 2) C. The Fair Nut and String
C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列( ...
- Codeforces Round #526 (Div. 2) Solution
A. The Fair Nut and Elevator Solved. 签. #include <bits/stdc++.h> using namespace std; #define ...
- Codeforces Round #526 (Div. 1)
毕竟是上紫之后的第一场div1,还是太菜了啊,看来我要滚回去打div2了. A. The Fair Nut and the Best Path 这题本来是傻逼贪心dfs,结果我越写越麻烦,然后就只有1 ...
- A. The Fair Nut and Elevator (Codeforces Round #526 (Div. 2))
A. The Fair Nut and Elevator 好笨啊QAQ. 暴力枚举的题,连分类都不用. 从电梯初始位置到第一层.人到第一层.间隔的层数,往返路程. #include <bits/ ...
- Codeforces Round #526 (Div. 2) A.B
A. The Fair Nut and Elevator 题目链接:https://codeforces.com/contest/1084/problem/A 题意: 一栋房子有n层楼,同时有个电梯( ...
- Codeforces Round #449 (Div. 2)-897A.Scarborough Fair(字符替换水题) 897B.Chtholly's request(处理前一半) 897C.Nephren gives a riddle(递归)
A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [Codeforces Round #526 (Div. 2)]
https://codeforces.com/contest/1084 A题 数据量很小,枚举就行 #include<iostream> #include<cstdio> #i ...
随机推荐
- 解决软件启动报error while loading shared libraries: libgd.so.2: cannot open shared object错误
解决软件启动报error while loading shared libraries: libgd.so.2: cannot open shared object错误 今天安装启动nginx的时候报 ...
- MOVE-PERCENTAGE(文字列の部分の代入)
以下の MOVE 命令のバリアントは.c 型項目についてのみ機能します. MOVE c1 TO c2 PERCENTAGE p [RIGHT]. 左寄せした (RIGHT オプションを指定した場合は右 ...
- Oracle错误记录
1 SQLPlus无法登陆oracle,PLSql可以登陆,报错ORA-12560 环境变量 右击计算机属性-->高级系统设置-->高级-->环境变量-->系统变量--> ...
- Hadoop学习(四) FileSystem Shell命令详解
FileSystem Shell中大多数命令都和unix命令相同,只是两者之间的解释不同,如果你对unix命令有基本的了解,那么对于FileSystem Shell的命令,你将会感到很亲切. appe ...
- ubuntu设置ssh登陆
转: 默认请况下,ubuntu是不允许远程登陆的.(因为服务没有开,可以这么理解.) 想要用ssh登陆的话,要在需要登陆的系统上启动服务.即,安装ssh的服务器端 $ sudo apt-get ins ...
- Apache 服务器性能评估
1 查看当前并发连接数 netstat -an | grep ESTABLISHED | wc -l 2 查看当前进程数 ps aux|grep httpd|wc -l
- 【多线程】 Task ,async ,await
[多线程]Task ,async ,await 一. WinForm 里经常会用到多线程, 多线程的好出就不多说了,来说说多线程比较麻烦的地方 1. UI 线程与其他线程的同步,主要是 Form 和 ...
- jmeter使用BeanShell断言
1. 首先存储一个接口的响应结果,如在http请求的BeanShell PostProcessor: import java.io.UnsupportedEncodingException; Syst ...
- VM实例的生命周期管理
有的操作功能比较类似,也有各自的适用场景,简单介绍下上述几个重要的操作: 常规操作: 常规操作中,Launch.Start.Reboot.Shut Off 和 Terminate 都很好理解. 下面几 ...
- Fluentd插件使用方法
这里主要介绍从MongoDB同步数据到ODPS.ruby环境的搭建以及fluent_plugin_mongo_odps插件的安装.1.准备工作1.1安装环境要求Ruby 2.1以上Gem 2.4.5以 ...