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

【题意】

题意

【题解】

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

【代码】

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. const int N = 3e5;
  5. int n;
  6. int a[N+10];
  7. ll f[N+10][2],ans=0;
  8. vector<pair<int,int> > g[N+10];
  9. void dfs(int x,int pre){
  10. int len = g[x].size();
  11. f[x][0] = a[x];
  12. for (int i = 0;i < len;i++){
  13. int y = g[x][i].first,cost = g[x][i].second;
  14. if (y==pre) continue;
  15. dfs(y,x);
  16. if (f[x][0]<f[y][0]-cost+a[x]){
  17. f[x][1] = f[x][0];
  18. f[x][0] = f[y][0]-cost+a[x];
  19. }else{
  20. if (f[x][1]<f[y][0]-cost+a[x]){
  21. f[x][1] = f[y][0]-cost+a[x];
  22. }
  23. }
  24. }
  25. if (f[x][1]>0){
  26. ans = max(ans,f[x][0]+f[x][1]-a[x]);
  27. }else ans = max(ans,f[x][0]);
  28. }
  29. int main(){
  30. ios::sync_with_stdio(0),cin.tie(0);
  31. cin >> n;
  32. for (int i = 1;i <= n;i++) cin >>a[i];
  33. for (int i = 1;i <= n-1;i++){
  34. int x,y,z;
  35. cin >> x >> y >> z;
  36. g[x].push_back({y,z});
  37. g[y].push_back({x,z});
  38. }
  39. dfs(1,-1);
  40. cout<<ans<<endl;
  41. return 0;
  42. }

【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. 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 ...

  2. 513 Find Bottom Left Tree Value 找树左下角的值

    给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...

  3. 在windows下编译出linux可执行程序

    set GOARCH=amd64 set GOOS=linux go build xx.go 会生成一个没有后缀的xx二进制文件 将该文件放入linux系统某个文件夹下 赋予权限 chmod 777 ...

  4. client系列、offset系列、scroll系列

    一.client系列 clientWidth/clientHeight    是我们设置的宽和高加上内边距(没有边框) clientLeft/clientTop 就是我们设置的边框值 二.offset ...

  5. 【学习笔记】深入理解js原型和闭包(16)——完结

    之前一共用15篇文章,把javascript的原型和闭包讲解了一下. 首先,javascript本来就“不容易学”.不是说它有多难,而是学习它的人,往往都是在学会了其他语言之后,又学javascrip ...

  6. 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 ...

  7. myeclipse 安装svn(subeclipsesite)插件

    (1)到官网下载subeclipsesite,下载最新的版本:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=224 ...

  8. AngularJS日期格式化

    本地化日期格式化:({{ today | date:'medium' }})    Mar 28, 2016 6:42:25 PM({{ today | date:'short' }})   3/28 ...

  9. codevs 5438 zbd之难题(水题日常)

     时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题目描述 Description zbd想要一个计算器,请你编一个计算器. 输入描述 Input Descrip ...

  10. CSS 循环动画效果。

    @-moz-keyframes revolving{ 0{ -moz-transform: rotate(0deg); -webkit-transform: rotate(0deg); } 25%{ ...