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相关推荐

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

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

  2. CF1083A The Fair Nut and the Best Path

    CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...

  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 nn cities. Most of the l ...

  4. 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 ...

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

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

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

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

  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. Oracle中TO_DATE格式
  2. 保密计算机责任人变更表,附件6-2-1南京理工大学涉密计算机申报表.doc
  3. IDEA整合 ssm的详细demo(使用maven)
  4. 光流 | OpenCV3实现LK Optical Flow(代码类)
  5. 事务注解放到类上面 下面私有方法有效吗_【面试】足够应付面试的Spring事务源码阅读梳理(建议珍藏)...
  6. 使用 python 在多个word文件中提取关键字
  7. matlab 参数辨识,Matlab系统辨识尝试之详细过程1
  8. 系统发育树操作神器-TreeTools-持续更新
  9. 记坑Method threw ‘feign.RetryableException‘ exception.
  10. mysql57压缩包安装教程
  11. 记工作的第一月--光说不练,假把式
  12. 分享一个简单好用的快递查询、物流管理软件
  13. 我的个人博客是如何申请百度联盟通过的?
  14. 进阶博弈论 Advanced Game Theory (Stanford+UBC)学习笔记
  15. processing制作熊猫头像跟随鼠标拖尾
  16. 用.bat文件打开程序
  17. 《筱静观察》第三季第6期丨区块链安全及其应用
  18. HTC Ubuntu 解锁
  19. windows10电脑无法连接到internet怎么解决
  20. 基于JAVA电影院售票系统设计与实现 开题报告

热门文章

  1. Pycharm专业版以及通过高校邮箱激活
  2. C语言之字符串以空格分割
  3. java文件对比7,一个线程读一个线程写、返回给前端进度条数据
  4. 统计学悖论问题——罗尼哈特小姐找朋友
  5. 从n个数中选出m个数的组合
  6. 记录篇---实习成长
  7. 【无标题】博士申请 | 香港科技大学(广州)李松泽老师组招收全奖博士/博后/硕士
  8. 服务器管理器怎么用?
  9. 未来想象计算机图片儿童版,想象未来的海底世界儿童画作品大全
  10. 图片比对算法 显示区域,图片比对算法有哪些