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
题意:
在一棵树内找一条路径,使得从起点到终点的最后剩下的油最多。(中途没油了不能再走了,可以在每个点加wi升油,减少的油量为路径长度)。
分析:
dfs一遍可以求出子树内所有点到子树根节点的最大的路径和次大的路径,然后可以直接合并取max,并且和从根节点出发的路径取max。
两条最大的和次大的合并可能不合法的。从最大的走上来后,不一定可以从根节点在走回去。但是即使不合法的取max是没有影响的,这样的路径一定不是更优的。
代码:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<iostream> 5 #include<cmath> 6 #include<cctype> 7 #include<set> 8 #include<queue> 9 #include<vector> 10 #include<map> 11 using namespace std; 12 typedef long long LL; 13 14 inline int read() { 15 int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; 16 for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; 17 } 18 19 const int N = 300005; 20 21 struct Edge{ 22 int to, nxt, w; 23 Edge() {} 24 Edge(int a,int b,int c) { to = a, nxt = b, w = c; } 25 }e[N << 1]; 26 int head[N], w[N], En; 27 28 inline void add_edge(int u,int v,int w) { 29 ++En; e[En] = Edge(v, head[u], w); head[u] = En; 30 ++En; e[En] = Edge(u, head[v], w); head[v] = En; 31 } 32 33 LL f[N], Ans; 34 void dfs(int u,int fa) { 35 f[u] = w[u]; 36 LL mx1 = -1e18, mx2 = -1e18; 37 for (int i = head[u]; i; i = e[i].nxt) { 38 int v = e[i].to; 39 if (v == fa) continue; 40 dfs(v, u); 41 LL t = f[v] - e[i].w; 42 if (t > mx1) mx2 = mx1, mx1 = t; 43 else if (t > mx2) mx2 = t; 44 if (f[v] > e[i].w) f[u] = max(f[u], f[v] - e[i].w + w[u]); 45 } 46 Ans = max(Ans, f[u]); 47 Ans = max(Ans, mx1 + mx2 + w[u]); 48 } 49 50 int main() { 51 int n = read(); 52 for (int i = 1; i <= n; ++i) w[i] = read(); 53 for (int i = 1; i < n; ++i) { 54 int u = read(), v = read(), w = read(); 55 add_edge(u, v, w); 56 } 57 dfs(1, 0); 58 cout << Ans; 59 return 0; 60 }
转载于:https://www.cnblogs.com/mjtcn/p/10100379.html
CF 1083 A. The Fair Nut and the Best Path相关推荐
- codeforces 1083 A. The Fair Nut and the Best Path(树形dp)
题目大意: 每个节点都给定一个值a[i],从一个节点走到另一个节点会消耗固定值w,但也会得到这个节点的价值,问怎样走才能得到最大的价值. 解题思路: 这个题和树形dp求树的直径差不多(树形DP基本都是 ...
- CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...
- 【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 ...
- 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 ...
- A. The Fair Nut and the Best Path(无根树dp详解)
https://codeforces.com/problemset/problem/1083/A 题意:到达一个点得到这个点的价值,经过一个边花费这个边的价值,求得到的最大价值 思路:之前碰过一些无根 ...
- 【Codeforces 1083A】The Fair Nut and the Best Path
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们最后要的是一条最长的路径. 这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大. 显然如果我们在某一条边上的累计的权值和< ...
- 【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 ...
- C - The Fair Nut and String
C - The Fair Nut and String CodeForces - 1084C 给出一个序列,求符合规则的序列有多少个,规则有两种,一种是只含a,一种是两个a之间夹着b. 对于每一个a有 ...
- Codeforces 1084A - The Fair Nut and Elevator
Codeforces 1084A - The Fair Nut and Elevator 题解链接 https://lucien.ink 题目链接 https://codeforces.com/con ...
最新文章
- Oracle中TO_DATE格式
- 保密计算机责任人变更表,附件6-2-1南京理工大学涉密计算机申报表.doc
- IDEA整合 ssm的详细demo(使用maven)
- 光流 | OpenCV3实现LK Optical Flow(代码类)
- 事务注解放到类上面 下面私有方法有效吗_【面试】足够应付面试的Spring事务源码阅读梳理(建议珍藏)...
- 使用 python 在多个word文件中提取关键字
- matlab 参数辨识,Matlab系统辨识尝试之详细过程1
- 系统发育树操作神器-TreeTools-持续更新
- 记坑Method threw ‘feign.RetryableException‘ exception.
- mysql57压缩包安装教程
- 记工作的第一月--光说不练,假把式
- 分享一个简单好用的快递查询、物流管理软件
- 我的个人博客是如何申请百度联盟通过的?
- 进阶博弈论 Advanced Game Theory (Stanford+UBC)学习笔记
- processing制作熊猫头像跟随鼠标拖尾
- 用.bat文件打开程序
- 《筱静观察》第三季第6期丨区块链安全及其应用
- HTC Ubuntu 解锁
- windows10电脑无法连接到internet怎么解决
- 基于JAVA电影院售票系统设计与实现 开题报告