CF1083A The Fair Nut and the Best Path

  • 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值.
  • 任意时刻权值非负的限制可以不用管,因为若走路径 \(u\to v\) ,走到 \(w\) 权值为负数了,那么直接从 \(w\) 下一个点开始走显然更优.这个限制是多余的.
  • 那么问题就很简单了,经典 \(dp\) 做法,记 \(f(i)\) 为 \(i\) 子树内一点到 \(i\) 所有路径中的最大权值, \(O(n)\) 即可解决问题.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{int x=0;bool pos=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')pos=0;for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return pos?x:-x;
}
const int MAXN=6e5+10;
int cnt,head[MAXN],to[MAXN<<1],nx[MAXN<<1];
int w[MAXN];
inline void addedge(int u,int v)
{++cnt;to[cnt]=v;nx[cnt]=head[u];head[u]=cnt;swap(u,v);++cnt;to[cnt]=v;nx[cnt]=head[u];head[u]=cnt;
}
int n;
ll f[MAXN];
ll ans=0;
void upd(ll x,ll &mx,ll &sc)
{if(x>=mx)sc=mx,mx=x;else if(x>sc)sc=x;
}
void dfs(int u,int fa)
{ll mx=0,sc=0;for(int i=head[u];i;i=nx[i]){int v=to[i];if(v==fa)continue;dfs(v,u);upd(f[v],mx,sc);}f[u]=mx+w[u];ans=max(mx+sc+w[u],ans);
}
int main()
{n=read();for(int i=1;i<=n;++i)w[i]=read();for(int i=1;i<n;++i){int u=read(),v=read();addedge(u,i+n);addedge(v,i+n);w[i+n]=-1*read();}dfs(1,0);cout<<ans<<endl;return 0;
}

转载于:https://www.cnblogs.com/jklover/p/10418077.html

CF1083A The Fair Nut and the Best Path相关推荐

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

  2. 【CodeForces - 1084D】The Fair Nut and the Best Path (树形dp)

    题干: The Fair Nut is going to travel to the Tree Country, in which there are nn cities. Most of the l ...

  3. CodeForces 1084D The Fair Nut and the Best Path(树形dp)

    题目描述 The Fair Nut is going to travel to the Tree Country, in which there are n cities. Most of the l ...

  4. A. The Fair Nut and the Best Path(无根树dp详解)

    https://codeforces.com/problemset/problem/1083/A 题意:到达一个点得到这个点的价值,经过一个边花费这个边的价值,求得到的最大价值 思路:之前碰过一些无根 ...

  5. 【Codeforces 1083A】The Fair Nut and the Best Path

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们最后要的是一条最长的路径. 这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大. 显然如果我们在某一条边上的累计的权值和< ...

  6. codeforces 1083 A. The Fair Nut and the Best Path(树形dp)

    题目大意: 每个节点都给定一个值a[i],从一个节点走到另一个节点会消耗固定值w,但也会得到这个节点的价值,问怎样走才能得到最大的价值. 解题思路: 这个题和树形dp求树的直径差不多(树形DP基本都是 ...

  7. 【CodeForces - 1084C】The Fair Nut and String(思维,组合数学)

    题干: The Fair Nut found a string ss. The string consists of lowercase Latin letters. The Nut is a cur ...

  8. C - The Fair Nut and String

    C - The Fair Nut and String CodeForces - 1084C 给出一个序列,求符合规则的序列有多少个,规则有两种,一种是只含a,一种是两个a之间夹着b. 对于每一个a有 ...

  9. Codeforces 1084A - The Fair Nut and Elevator

    Codeforces 1084A - The Fair Nut and Elevator 题解链接 https://lucien.ink 题目链接 https://codeforces.com/con ...

最新文章

  1. 有感于框架设计难,实施框架更难!
  2. iOS之深入解析bitcode的功能与应用
  3. mac android studio 打不开adb,Android-Macbook ADB无法打开
  4. “算法”的茧房,如何破局?
  5. Python内置GUI模块Tkinter的几点笔记
  6. xilinx sdk对文本内容进行查找(find/replace)的快捷键ctrl+f
  7. Mysql SQLyog 使用详解
  8. 红米手机连接小米wifi显示已连接服务器,小米手机连wifi已连接却无法访问互联网怎么回事...
  9. 计算机傅里叶什么知识美颜,傅里叶变换是用来做什么的,具体举例一下应用?...
  10. window10怎么设置共享计算机,Win10系统网络共享功能怎么用?Windows10网络共享功能使用方法...
  11. 视频帧差异视频卡顿检测
  12. 5nm计算机cpu,芯片14nm、10nm、7nm、7nm、7nm、7nm有什么差别?
  13. Appium学习:雷电模拟器的使用
  14. 公司服务器架设邮箱服务器,如何搭建企业邮箱服务器
  15. 人工智能、机器学习、深度学习从入门到进阶学习资料整理
  16. 洛谷 P2530 [SHOI2001] 化工厂装箱员
  17. C#委托和事件的概念
  18. BAT云战争新动向:收编“旧军”,占山为王
  19. 最近用周立功的IOT3960板子做一个小控制器,现在到了界面美化阶段,各位大侠有没有高招?
  20. 完全卸载office(手动卸载)、微软工具自动卸载、安装Office时提示错误1310、写入文件“GROOVEEX“时错误,请确认您有权限访问该目录

热门文章

  1. android护眼程序原理,Android 护眼模式的实现
  2. 王福强老师的《Spring揭秘》读后感-IOC容器之BeanFactory
  3. 关于华为网络设备操作系统的介绍
  4. python 鸡兔同笼的问题
  5. 东京海上北美服务有限公司(TMNAS)宣布同Akur8达成新合作以改进定价流程
  6. zoj 3734 LIKE vs CANDLE
  7. 使用二维数组,实现五子棋功能
  8. 有趣的机器学习:最简明入门指南
  9. python简单游戏实例_Python实现的简单算术游戏实例
  10. python利用OpenCV调用海康威视摄像头--利用wifi路由器提供网