题目大意:

给定一棵树 树上每个点有对应的点权

树上每条边有对应的边权

经过一个点可得到点权 经过一条边必须花费边权

即从u到v 最终得分=u的点权-u到v的边权+v的点权

求树上一条路径使得得分最大

看注释

#include <bits/stdc++.h>
#define LL long long
#define INf 0x3f3f3f3f
using namespace std;
const int N=3e5+;
bool vis[N];
LL w[N], ans;
int n;
struct NODE { int to,nt; LL l; }e[N<<];
int head[N], tot;
void addE(int u,int v,LL l) {
e[tot].to=v, e[tot].l=l;
e[tot].nt=head[u];
head[u]=tot++;
}
void init() {
memset(head,,sizeof(head));
tot=;
}
LL dfs(int u,int fa) {
vis[u]=;
LL ans1=0LL, ans2=0LL;
// ans1由子节点出发的一条路径最大得分 ans2为次大
for(int i=head[u];i;i=e[i].nt) {
int v=e[i].to;
if(v==fa || vis[v]) continue;
LL tmp=dfs(v,u)-e[i].l;
if(tmp>ans1) swap(tmp,ans1);
if(tmp>ans2) swap(tmp,ans2);
}
ans=max(ans,ans1+ans2+w[u]);
// 可由最大得分和次大得分加上u点 得到一条经过u点的路径的最大得分
return ans1+w[u]; // 只返回由u出发的一条路径可得到的最大得分
}
int main()
{
while(~scanf("%d",&n)) {
for(int i=;i<=n;i++) scanf("%I64d",&w[i]);
init();
for(int i=;i<n;i++) {
int u,v; LL l;
scanf("%d%d%I64d",&u,&v,&l);
addE(u,v,l); addE(v,u,l);
}
memset(vis,,sizeof(vis));
ans=0LL;
dfs(,);
printf("%I64d\n",ans);
} return ;
}

Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费的更多相关文章

  1. Codeforces Round #526 C - The Fair Nut and String /// 组合递推

    题目大意: 给定原字符序列 找出其中所有子序列满足 1.序列内字符都为a 2.若有两个以上的字符 则相邻两个字符在原序列中两者之间存在字符b 的数量 将整个字符序列用b分开 此时再得到每个b之间a的数 ...

  2. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp

    D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...

  3. CodeForces 1084D The Fair Nut and the Best Path

    The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...

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

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

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

  6. Codeforces Round #526 (Div. 1)

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

  7. CF 1083 A. The Fair Nut and the Best Path

    A. The Fair Nut and the Best Path https://codeforces.com/contest/1083/problem/A 题意: 在一棵树内找一条路径,使得从起点 ...

  8. CF1083A The Fair Nut and the Best Path

    CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...

  9. Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和

    Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description ...

随机推荐

  1. dubbo使用multicast注册方式消费者无法发现服务的一种情况(我遇到的情况)

    今天做dubbo测试的时候,翻出以前的代码,使用multicast广播地址的方式消费者居然无法发现服务.我的情况是因为启用了vmware虚拟机的网卡,导致了消费者无法发现服务,禁用vmware网卡后可 ...

  2. python re 正則匹配規則

  3. Mysql数据库命令行输入错误怎么办

    Mysql数据库命令行输入错误 缺少另一半 错误输入 ' 或 " 时,必须键入另一半才能退出命令. 缺少分号 写入语句缺少分号需要补全. 输入quit或者exit退出 ctrl+c,完全退出 ...

  4. flink中对于window和watermark的一些理解

    package com.chenxiang.flink.demo; import java.io.IOException; import java.net.ServerSocket; import j ...

  5. POJ 1329 Circle Through Three Points(三角形外接圆)

    题目链接:http://poj.org/problem?id=1329 #include<cstdio> #include<cmath> #include<algorit ...

  6. (转载)理解和使用Promise.all和Promise.race

    声明:本文转载自:https://www.jianshu.com/p/7e60fc1be1b2 一.Pomise.all的使用 Promise.all可以将多个Promise实例包装成一个新的Prom ...

  7. 【二】Jmeter接口自动化测试系列之函数使用及扩展

    上一篇文章我们了解了Jmeter的参数化的集中方法,虽然方法不是很多,但已经足够使用! 本篇文章,介绍一下Jmeter自带函数的使用和 函数扩展,来满足测试工作中的各种需求! Jmeter自带函数 点 ...

  8. 使用jQuery的datetimepicker插件

    因为后台系统要使用年月日时分的设置,又因为使用的Bootstrap框架只有datepicker和timepicker控件.所以在jQuery库中找到轻量级的datetimepicker插件,很好地解决 ...

  9. linux 挂载iso文件,挂载ntfs文件系统

    映像档不可录就挂载使用.通过loop命令来执行 好吧.跟同事要了一个win10系统盘.插入,竟然是灰色的. ,一点击,提示无法挂载,仔细看了一下下面的内容,原来不支持ntfs格式,好吧,win10系统 ...

  10. vue中数据绑定遇到的问题

    <!-- 使用element中的表格组件,在编辑的时候传递每行的数据 --> <el-button size="small" type="success ...