【Codeforces 1083A】The Fair Nut and the Best Path
【链接】 我是链接,点我呀:)
【题意】
题意
【题解】
我们最后要的是一条最长的路径。
这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大。
显然如果我们在某一条边上的累计的权值和=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的更多相关文章
- 【Codeforces 1073D】Berland Fair
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们可以从左到右枚举一轮. 定义一个cost表示这一轮花费的钱数 如果cost+a[i]<=T那么就可以买它,并且买下它(模拟题目要求) ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 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 题意: 给出一棵树,走不重复的 ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- CodeForces 1084D The Fair Nut and the Best Path
The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...
- 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 题意: 在一棵树内找一条路径,使得从起点 ...
- CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- Could not open logfile" occurred when run "datapatch -verbose"
CAUSE Due to Bug 25459405 - DATAPATCH FAILS WITH SP2-0768 IF NLS_LANGUAGE IS NOT SET TO AMERICANwhic ...
- 513 Find Bottom Left Tree Value 找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...
- 在windows下编译出linux可执行程序
set GOARCH=amd64 set GOOS=linux go build xx.go 会生成一个没有后缀的xx二进制文件 将该文件放入linux系统某个文件夹下 赋予权限 chmod 777 ...
- client系列、offset系列、scroll系列
一.client系列 clientWidth/clientHeight 是我们设置的宽和高加上内边距(没有边框) clientLeft/clientTop 就是我们设置的边框值 二.offset ...
- 【学习笔记】深入理解js原型和闭包(16)——完结
之前一共用15篇文章,把javascript的原型和闭包讲解了一下. 首先,javascript本来就“不容易学”.不是说它有多难,而是学习它的人,往往都是在学会了其他语言之后,又学javascrip ...
- WARN警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended
使用Apache HttpClient发送请求,有大量WARN警告:Going to buffer response body of large or unknown size. Using getR ...
- myeclipse 安装svn(subeclipsesite)插件
(1)到官网下载subeclipsesite,下载最新的版本:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=224 ...
- AngularJS日期格式化
本地化日期格式化:({{ today | date:'medium' }}) Mar 28, 2016 6:42:25 PM({{ today | date:'short' }}) 3/28 ...
- codevs 5438 zbd之难题(水题日常)
时间限制: 1 s 空间限制: 1000 KB 题目等级 : 白银 Silver 题目描述 Description zbd想要一个计算器,请你编一个计算器. 输入描述 Input Descrip ...
- CSS 循环动画效果。
@-moz-keyframes revolving{ 0{ -moz-transform: rotate(0deg); -webkit-transform: rotate(0deg); } 25%{ ...