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. Oracle Management Packs

    http://kerryosborne.oracle-guy.com/2008/10/oracle-management-packs/ There has been quite a bit of co ...

  2. 管理linked break-off snapshot

    1. 建立linked break-off snapshot   (1) 建立原卷 #> vxassist -g APS2_AFC_DG make vol1 4096000 #> vxpr ...

  3. LAMP 2.3 Apache配置防盗链

    如果你的站点是一个图片站,有很多非常漂亮的美女图片,那我相信,时间久了会有很多人来你网站借图片,有的人直接下载走了,还有的人直接取走图片的地址,比如你的网站域名是 www.123.com,图片地址为 ...

  4. touch: cannot touch `/home/tomcat7/logs/catalina.out': Permission denied

    今天打开虚拟机启动tomcat,Y的包这个错,普通用户登录的,一直报这个错误,竟然没有想起来是为什么,真是感到惭愧,其实原因很简单,就是logs文件夹没有读写的权限,一条 chmod -R 777 l ...

  5. java格式化数字、货币、金钱

    网上摘来的,以后可能会用到 java开发中经常会有数字.货币金钱等格式化需求,货币保留几位小数,货币前端需要加上货币符号等.可以用java.text.NumberFormat和java.text.De ...

  6. Selenium二次封装-Python版本

    from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...

  7. 面试题:struts 值栈 有用

    一. 核心部分 1. [核心试题]完成当天课堂练习 2. [多选题] 阅读如下代码中,下列哪种方式可以在页面正确迭代获取集合中的数据 (ABC) public String add(){ ValueS ...

  8. Elasticsearch - 环境准备

    Precondition: Ubuntu OS 环境准备: 1. JAVA_HOME 1.1 Download the jdk8 (jdk-8u25-linux-x64.tar.gz) from of ...

  9. DNS线路

    文章介绍 填写DNS地址时候,比较好记的就114.114.114.114,8.8.8.8,9.9.9.9,几个,但是常用的有哪些呢?这篇文章就简单介绍下了. 前言 两年多前,曾发帖对国内主流公共 DN ...

  10. bit byte的关系

    字 word 字节 byte 位 bit 字长是指字的长度 1字=2字节(1 word = 2 byte) 1字节=8位(1 byte = 8bit)  一个字的字长为2个字节=2*8=16 一个字节 ...