【链接】 我是链接,点我呀:)

【题意】

题意

【题解】

我们最后要的是一条最长的路径。
这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大。
显然如果我们在某一条边上的累计的权值和=0)
所以如果我们求的是最大的权值和-边权和的话,那么求出来的路径一定不会有中间某个地方走着走着没油的情况
因此我们只要按照树上最长链的类似方法。
求出来,从i的不同子树下的节点到达i节点的点权和减去边权和的最大值和次小值。
对于所有的点,用这两个值的和尝试更新答案即可。

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std; const int N = 3e5; int n;
int a[N+10];
ll f[N+10][2],ans=0; vector<pair<int,int> > g[N+10]; void dfs(int x,int pre){
int len = g[x].size();
f[x][0] = a[x];
for (int i = 0;i < len;i++){
int y = g[x][i].first,cost = g[x][i].second;
if (y==pre) continue;
dfs(y,x);
if (f[x][0]<f[y][0]-cost+a[x]){
f[x][1] = f[x][0];
f[x][0] = f[y][0]-cost+a[x];
}else{
if (f[x][1]<f[y][0]-cost+a[x]){
f[x][1] = f[y][0]-cost+a[x];
}
}
}
if (f[x][1]>0){
ans = max(ans,f[x][0]+f[x][1]-a[x]);
}else ans = max(ans,f[x][0]); } int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >> n;
for (int i = 1;i <= n;i++) cin >>a[i];
for (int i = 1;i <= n-1;i++){
int x,y,z;
cin >> x >> y >> z;
g[x].push_back({y,z});
g[y].push_back({x,z});
}
dfs(1,-1);
cout<<ans<<endl;
return 0;
}

【Codeforces 1083A】The Fair Nut and the Best Path的更多相关文章

  1. 【Codeforces 1073D】Berland Fair

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们可以从左到右枚举一轮. 定义一个cost表示这一轮花费的钱数 如果cost+a[i]<=T那么就可以买它,并且买下它(模拟题目要求) ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

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

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

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

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

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

  6. 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 题意: 在一棵树内找一条路径,使得从起点 ...

  7. CF1083A The Fair Nut and the Best Path

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

  8. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  9. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

随机推荐

  1. Building Forest CodeForces - 195E

    Building Forest CodeForces - 195E 这题意真是难懂啊...话说"An oriented weighted forest is an acyclic weigh ...

  2. Ubuntu 安装 node

    ubuntu安装node和npm的命令行命令: sudo apt install nodejs-legacy sudo apt install npm 最新版本安装方法 1.安装npm sudo ap ...

  3. h5-18-文件上传

    参考博客地址:https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects 参考博客地址:http: ...

  4. AO-XXXX

    一 AO4419:应用于开关应用或PWM应用的场效应管.

  5. STM32HAL库学习之前言

    HAL库:HAL 的全称是: Hardware Abstraction Layer (硬件抽象层) ,是ST最新推荐的库.包括基本库和扩展库(功能外展):三种编程模型(轮询.中断和 DMA) 灵活的回 ...

  6. Webform 内置对象2(Session、Application)、Repeater的Command操作

    内置对象: 1.Session:跟Cookies一样用来存储用户数据,但保存位置不同,保存在服务器内存上 每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 S ...

  7. log4go折腾

    导包 go get -u github.com/alecthomas/log4go log4go.xml配置 <logging> <filter enabled="true ...

  8. [ Luogu 3709 ] 大爷的字符串题

    \(\\\) Description 原题题面太过混乱出题人语文凉凉 给出一个长为 \(n\) 的数列 \(A\) ,多次询问: 对于一个区间 \([L_i,R_i]\),把区间内的所有数最少划分成多 ...

  9. JSP报错The value for the useBean class attribute *** is invalid.

    环境:IDEA+Tomcat9+JDK1.8 在前期学习时,环境一直能够"正常"使用,实际上环境并没有完全搭建成功. 推荐: https://blog.csdn.net/lw_po ...

  10. laravel的scout包安装及laravel-es包安装

    安装laravel/scout 作用:搜索驱动,可随时更换驱动,上层业务逻辑可不用改变 官网文档:https://laravel-china.org/docs/laravel/5.4/scout/12 ...