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是没有影响的,这样的路径一定不是更优的。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; struct Edge{
int to, nxt, w;
Edge() {}
Edge(int a,int b,int c) { to = a, nxt = b, w = c; }
}e[N << ];
int head[N], w[N], En; inline void add_edge(int u,int v,int w) {
++En; e[En] = Edge(v, head[u], w); head[u] = En;
++En; e[En] = Edge(u, head[v], w); head[v] = En;
} LL f[N], Ans;
void dfs(int u,int fa) {
f[u] = w[u];
LL mx1 = -1e18, mx2 = -1e18;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == fa) continue;
dfs(v, u);
LL t = f[v] - e[i].w;
if (t > mx1) mx2 = mx1, mx1 = t;
else if (t > mx2) mx2 = t;
if (f[v] > e[i].w) f[u] = max(f[u], f[v] - e[i].w + w[u]);
}
Ans = max(Ans, f[u]);
Ans = max(Ans, mx1 + mx2 + w[u]);
} int main() {
int n = read();
for (int i = ; i <= n; ++i) w[i] = read();
for (int i = ; i < n; ++i) {
int u = read(), v = read(), w = read();
add_edge(u, v, w);
}
dfs(, );
cout << Ans;
return ;
}
CF 1083 A. The Fair Nut and the Best Path的更多相关文章
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
- CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...
- 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一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...
- CodeForces 1083 E The Fair Nut and Rectangles 斜率优化DP
The Fair Nut and Rectangles 题意:有n个矩形,然后你可以选择k个矩形,选择一个矩形需要支付代价 ai, 问 总面积- 总支付代价 最大能是多少, 保证没有矩形套矩形. 题解 ...
- D. The Fair Nut and the Best Path 树形dp (终于会了)
#include<bits/stdc++.h> #define int long long using namespace std; ; int a[maxn]; int dp[maxn] ...
- 【Codeforces 1083A】The Fair Nut and the Best Path
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们最后要的是一条最长的路径. 这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大. 显然如果我们在某一条边上的累计的权值和< ...
- Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费
题目大意: 给定一棵树 树上每个点有对应的点权 树上每条边有对应的边权 经过一个点可得到点权 经过一条边必须花费边权 即从u到v 最终得分=u的点权-u到v的边权+v的点权 求树上一条路径使得得分最大 ...
随机推荐
- PhoneGap模仿微信摇一摇功能
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- hdu-3333 Turing Tree 离线区间+树状数组(区间不同数的和)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 题目大意: 给出一数组,以及m个查询区间,每次查询该区间不同数字的和.相同数字只加一次. 解题 ...
- IE8崩溃在CElement::GetUpdatedLayoutWithContext
发了一个我们页游助手的版本时,测试报告在某些机器上点开某网站时崩溃 "0x637e5067指令引用的0x00000008内存,该内存不能为read",查看dump文件,堆栈如下: ...
- HDU 1686 Oulipo (可重叠匹配 KMP)
Oulipo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- C++11之std::future和std::promise
为什么C++11引入std::future和std::promise?C++11创建了线程以后,我们不能直接从thread.join()得到结果,必须定义一个变量,在线程执行时,对这个变量赋值,然后执 ...
- OpenID Connect Core 1.0(四)使用授权码流验证(上)
3.1 使用授权码流验证(Authentication using the Authorization Code Flow) 本节描述如何使用授权码流执行验证.当使用授权码流时,会从令牌终结点返回的所 ...
- oracle安装程序异常终止解决办法
安装Oracle时总是会报程序异常终止,摸不着头脑,作为初学者一下就乱了分寸 工具/原料 Oracle软件包 win764位 方法/步骤 右击Oracle安装图标setup.exe,选 ...
- react路由传参
方法1 <刷新页面参数会消失> <Link className="item" to={{pathname:'/order',params:{index :&quo ...
- MySQL的库表详细操作
MySQL数据库 本节目录 一 库操作 二 表操作 三 行操作 一 库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset utf8; 1.2 数据库命名规则 ...
- Python -- Effective Python:编写高质量Python代码的59个有效方法
第 1 章 用 Pythonic 方式来思考 第 1 条:确认自己所用的 Python 版本 python --version import sys print(sys.version_info) p ...