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的更多相关文章

  1. 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 题意: 给出一棵树,走不重复的 ...

  2. 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的 ...

  3. 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的子序列( ...

  4. Codeforces Round #526 (Div. 2) Solution

    A. The Fair Nut and Elevator Solved. 签. #include <bits/stdc++.h> using namespace std; #define ...

  5. Codeforces Round #526 (Div. 1)

    毕竟是上紫之后的第一场div1,还是太菜了啊,看来我要滚回去打div2了. A. The Fair Nut and the Best Path 这题本来是傻逼贪心dfs,结果我越写越麻烦,然后就只有1 ...

  6. Codeforces Round #205 (Div. 2)C 选取数列可以选择的数使总数最大——dp

    http://codeforces.com/contest/353/problem/C Codeforces Round #205 (Div. 2)C #include<stdio.h> ...

  7. A. The Fair Nut and Elevator (Codeforces Round #526 (Div. 2))

    A. The Fair Nut and Elevator 好笨啊QAQ. 暴力枚举的题,连分类都不用. 从电梯初始位置到第一层.人到第一层.间隔的层数,往返路程. #include <bits/ ...

  8. Codeforces Round #526 (Div. 2) A.B

    A. The Fair Nut and Elevator 题目链接:https://codeforces.com/contest/1084/problem/A 题意: 一栋房子有n层楼,同时有个电梯( ...

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

随机推荐

  1. leetcode319

    public class Solution { public int BulbSwitch(int n) { var x = Math.Sqrt(n); var y = Convert.ToInt32 ...

  2. Delphi Android 询问框

    Delphi Android 询问框 http://community.embarcadero.com/blogs/entry/xe7-dialog-box-methods-support-anony ...

  3. 【转载】基于TINY4412的Andorid开发-------简单的LED灯控制

    阅读目录(Content) 一.编写驱动程序 二.编写代码测试驱动程序 三.编写HAL代码 四.编写Framework代码 五.编写JNI代码 六.编写App 参考资料: <Andriod系统源 ...

  4. C#如何生成JSON字符串?(序列化对象)

    第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) 第四章:C#如何生成JSON字符串提交给接口 ...

  5. Jedis连接redis的一些基本操作

    Jedis其实就是redis的一个连接方式 需要的jar包:

  6. ubuntu16部署gitlab

    一.gitlab的安装 1. 安装依赖包 $ sudo apt-get update #如无ssh还需安装openssh-server $ sudo apt-get install postfix c ...

  7. cd命令无效

    原因是没有切换盘符步骤一:C:\Documents and Settings\Administrator>d:步骤二:cd D:\Program Files\Python35-32\Script ...

  8. 激光SLAM Vs 视觉SLAM

    博客转载自:https://www.leiphone.com/news/201707/ETupJVkOYdNkuLpz.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...

  9. hdu6357 Hills And Valleys

    传送门 题目大意 给定一个序列A,求翻转A中一个区间之后的最长不降子序列的长度即翻转的区间 分析 发现直接枚举翻转的区间的话是无论如何都不行的,于是有一个非常神奇的做法.我们再设一个序列B = {0, ...

  10. js的学习

    对于 ff的 relatedTarget    及IE的toElement  fromElement DOM通过event对象的relatedTarget属性提供了相关元素的信息.这个属性只对于mou ...