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的点权 求树上一条路径使得得分最大 ...
随机推荐
- docker-6-DockerFile解析
1.是什么 Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本. 构建三步骤: 1.编写Dockerfile文件 2.docker build 3.docker ...
- WEB安全 asp+access注入
asp+access注入 数据库 (access数据库没有数据库名) 表名 字段(列名) 记录(行,内容) 注入常用函数: top n 表示查询结果的第n个记录 len() 函数返回文本字段中值的长度 ...
- Autoware 安装流程
1. 前言 最近一个项目采用Autoware的无人驾驶系统,在安装工控机的过程中,踩了不少坑,留下记录,造福后人. 相关的下载在我的百度网盘,大家自行下载.链接: https://pan.baidu. ...
- Qt中QScrollArea类的简单使用心得
平台:windows 64位 Qt版本:5.5.1 MinGW 32bit 根据自己目前的需求简单说下怎么在QScrollArea滚动窗口中实现多个控件的滚动显示,先看看最 ...
- Linux开启路由的方法
Linux开启路由的命令很简单,只需要一条命令即可: [root@localhost ~]# echo 1 > /proc/sys/net/ipv4/ip_forward 这个只是临时修改,如果 ...
- 如何利用java程序实现加密所需的公钥、密钥、数字证书
本篇的主要目的在于实现pdf的数字签名问题,只是作为我学习知识的总结. 1.数字签名算法的概述 本部分主要参考于:https://blog.csdn.net/lovelichao12/article/ ...
- Oracle 自定义实用函数
一.ctod 字符转为date, create or replace function ctod(str in varchar2) return date as begin return to_dat ...
- React Native获取组件位置和大小
RN页面中定位或滚动操作时,需要获取元素的大小和位置信息,有几种常用的方法 获取设备屏幕的宽高 import {Dimensions} from 'react-native'; var {height ...
- CABasicAnimation使用总结
CABasicAnimation使用总结 实例化 使用方法animationWithKeyPath:对 CABasicAnimation进行实例化,并指定Layer的属性作为关键路径进行注册. //围 ...
- svn造成桌面图标显示问号
(1)在使用svn客户端的时候桌面的所有图标上面都加了一个“?”.而且在桌面上新建的文件夹或文件都会打个问号,下面是笔者搜集的方法:在桌面创建记事本文件,把这句话复制进去for /r . %%a in ...