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的点权 求树上一条路径使得得分最大 ...
随机推荐
- 业务id转密文短链的一种实现思路
业务场景: 买家通过电商app下单后,会受到一条短信,短信内容中包括改订单详情页面的h5地址连接,因为是出现在短信中,所以对连接有要求: 1.尽量短:2.安全性考虑,订单在数据库中对应的自增主键id不 ...
- SpringMVC如何解决POST请求中文乱码问题,GET的又如何处理呢?
在web.xml中 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-c ...
- 使用Apache HttpClient 4.5设置超时时间
使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间.这两个参数很重要,目的是为了防止访问其他http服务时,由于超时导致自己的应用受影响. 4.5版本中,这两个参数的设置都抽象到了 ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- (转)解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
重新启动服务器,访问web服务发现无法浏览啦!登陆服务器之后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr ...
- Factory(工厂)模式
设计模式一 工厂模式Factory 在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.但是在一些情况下, new操作符直接生成对象会带来一些问题. ...
- paxos协议(1)-朴素paxos
前言 学习paxos协议,最困惑我的两点是: 1. 朴素paxos是怎么样的?这部分主要是原理: 2. paxos协议是怎么运用到分布式系统解决问题的.因为很多博客的开篇说paxos协议可以运用在很多 ...
- win 10 如何关闭自动更新
1.右键“此电脑”图标,点击“管理”,打开“计算机管理”窗口; 2.在“计算机管理”窗口中找到“服务和应用程序”,点击“服务”,打开“服务”窗口; 3.在“服务”中找到“Windows Update” ...
- 快速提高谷歌浏览器(Chrome)自带下载器的网速
之前每次下载东西都是复制好下载链接到迅雷中下载,会提高成倍网速,但是时间一长,感觉不方便,废话不多说,上干货~ 由于中国防火墙(GFW)的强大,在线下载Google浏览器的时候速度非常慢,如果只是单独 ...
- 九九乘法表,全js编写,放入table表格带入页面渲染出来
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...