Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path
题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的边权,如果当前值小于边的边权,就走不通,问从任意点出发到任意点结束的可以获得的最大权多少(其中到一个点结束的时候也能获得改点的值)
思路:一个很明显的树上dp的问题 \(dp[i]\)表示以i为起点的可以获得的最高的权值是多少
\(dp[i]=w[i]+max(son(dp[j]))\) 其中j为i的儿子节点
表示的是以i为起点得到的最大权 通过以其儿子位起点的最大权来更新
而答案等于 \(max(w[i]+firstmax+secondmax)\)表示以i的权值 加 以i为起点的路径的最大的两条可以获得的权值
#include<bits/stdc++.h>
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;i++)
#define MS(arr,arr_value) memset(arr,arr_value,sizeof(arr))
#define F first
#define S second
#define pii pair<int ,int >
#define mkp make_pair
#define pb push_back
#define arr(zzz) array<ll,zzz>
using namespace std;
typedef long long ll;
#define int ll
const int maxn=3e5+5;
struct Node{
int to,next,value;
}edge[maxn*10];
int head[maxn],w[maxn];
int dp[maxn];
int size=0;
ll ans=0;
void add(int x,int y,int v){
edge[size].to=y;
edge[size].next=head[x];
edge[size].value=v;
head[x]=size++;
}
void dfs(int now,int fa){
dp[now]+=w[now];
//cout<<dp[now]<<" "<<now<<endl;
ll firstmax=0,secondmax=0;
for(int i=head[now];i!=-1;i=edge[i].next){
int y=edge[i].to;
int v=edge[i].value;
if(y!=fa){
dfs(y,now);
if(dp[y]-v>=firstmax){
secondmax=firstmax;
firstmax=dp[y]-v;
}
else if(dp[y]-v>secondmax){
secondmax=dp[y]-v;
}
}
//cout<<dp[now]<<" "<<firstmax<<" "<<secondmax<<" "<<now<<" "<<endl;
//dp[now]+=firstmax;
}
ans=max(ans,dp[now]+secondmax+firstmax);
dp[now]+=firstmax;
//cout<<dp[now]<<" "<<w[now]<<" "<<ans<<" "<<firstmax<<" "<<secondmax<<" "<<now<<endl;
}
int32_t main(){
int n;
memset(head,-1,sizeof(head));
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&w[i]);
}
int x,y,v;
for(int i=1;i<n;i++)
{
scanf("%lld%lld%lld",&x,&y,&v);
add(x,y,v);
add(y,x,v);
}
dfs(1,-1);
cout<<ans<<endl;
return 0;
}
Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp的更多相关文章
- 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 题意: 给出一棵树,走不重复的 ...
- 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 ...
- Codeforces Round #205 (Div. 2)C 选取数列可以选择的数使总数最大——dp
http://codeforces.com/contest/353/problem/C Codeforces Round #205 (Div. 2)C #include<stdio.h> ...
- 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 ...
随机推荐
- Enable SVM while booted from alternate media (ZT)
http://www.seedsofgenius.net/uncategorized/solaris-tips-enable-svm-while-booted-from-alternate-media ...
- Delphi 询问框 汉化
Delphi 询问框 汉化 d:\program files (x86)\embarcadero\studio\17.0\source\fmx\FMX.Consts.pas add this file ...
- 基本的数据类型 void关键字 都存在类类型
- clearfix的用法(转)
clearfix的用法 (2013-12-31 10:41:24) 标签: clearfix 清除浮动 clearboth height zoom 分类: 网页制作 如果有一个DIV作为外部容器,内部 ...
- Codeforces 1136F Cooperative Game (神仙题)
这种题就是难者不会,会者不难. 博客讲的很详细了 代码: #include <bits/stdc++.h> using namespace std; string s; int read( ...
- Swing绘图机制
------------------siwuxie095 工程名:TestSwingPaintMethod 包名:com.siwuxie095.swin ...
- 百度Apollo解析——3.common
1.略读 该目录下主要提供了各个模块公用的函数和class以及一些数学API还有公共的宏定义. 在Apollo 1.0中,common是整个框架的基础.configs是配置文件加载.adapters是 ...
- 托盘在XP下不能显示tooltip,在Vista和Windows7下正常
转自:http://blog.csdn.net/debehe/article/details/4294053 奇怪的问题,想了很多可能的理由,最终的答案竟然是一开始就被我否认了的一种可能!! 问题现象 ...
- PS中图层混合模式的计算方法
https://zhuanlan.zhihu.com/p/23905865 长久以来一直用中文版本的PS,对于软件中的一些专业名字都是顾名思义,容易误入歧途,但当你真正看到英文版本的名字的时候才有豁然 ...
- noi.ac day6t1 queen
传送门 分析 我就是个BT...... 直接排序后开数组记录每条线上的信息,注意由于每个点只会影响前面第一个点和后面第一个点,所以记录每条线的前一个点就行了. 代码 #include<iostr ...