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. 注解:@interface 自定义注解的语法

      自定义注解: 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节.在定义注解时,不能继承其他的注解或接口 ...

  2. jackson 进行json与java对象转换 之二

    主要用于测试学习用jackson包实现json.对象.Map之间的转换. 1.准备测试用的Java类 (1)Link类 package test; /** * Description: 联系方式,被u ...

  3. 问题:oracle case when;结果:Oracle CASE WHEN 用法介绍

    Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...

  4. linux驱动开发的经典书籍

    转载于:http://www.cnblogs.com/xmphoenix/archive/2012/03/27/2420044.html 参加实习也近一个月了,严重感觉知识不够,真是后悔学校里浪费那么 ...

  5. apache重写规则详解

    RewriteEngine on 为重写引擎开关,如果设为off,则任何重写规则定义将不被应用,该开关的另一好处就是如果为了临时拿掉重写规则,则改为off再重启动Apache即可,不必将下面一条条的重 ...

  6. Guava Cache本地缓存

    Guava介绍 Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库. 这个库是为了方便编码,并减少编码错误. 这个库提供用于集合,缓存,支持原语,并发性,常见注解,字 ...

  7. Struts2框架06 ValueStack

    原文地址:点击前往 1 什么是ValueStack 称为值栈,Struts提供的共享数据的数据结构 2 为什么要使用ValueStack 从控制器向浏览器传递数据 存储与请求相关的对象信息(sessi ...

  8. css选择器的一些说明

    标签选择器.ID选择器.类选择器 这三个很简单,没啥可说的. 子选择器得说一下. <ul class="food">    <li>水果        &l ...

  9. Android之Home键监听封装

    众所周知,我们监听返回键事件,无法是下面两个方法: @Override public void onBackPressed() { //do something //super.onBackPress ...

  10. Java NIO学习笔记

    Java NIO学习笔记 一 基本概念 IO 是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. 所有语言运行时系统提供执 ...