The Fair Nut and the Best Path

题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数。

题解:

dfs一遍, 求所有儿子走到这个点的最大值和次大值。

我们需要明白如果可以从u -> v  那么一定可以从 v -> u, 当然 指的是 u->v是路径上的最大和。

u->e1->v;

假如:val[u] = 100, val[e1] = 50, val[v] = 60, 那么我们发现可以从 u -> v 也可以从v -> u

val[u] = 100, val[e1] = 50, val[v] = 40, 虽然我们可以从u->v,但是 不能 v->u, 但是根据上面的定义,我们发现 从 u->v反而是亏本的,也就是说 u->u是最大的,我们不在考虑 u->v了。

val[u] = 40, val[e1] = 50, val[v] = 100, 和上面一样的道理。

所以,当一条路是最大的能赚的话, 那么一定可以走双向。

然后 现在还有一个疑问就是  如果从 u的父节点到u呢, 这个东西在 u往上传的时候就解决了。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 3e5 + ;
const int M = *N;
int head[N], to[M], val[M], nt[M], tot;
void add(int u, int v, int w){
to[tot] = v;
val[tot] = w;
nt[tot] = head[u];
head[u] = tot++;
}
LL dp[N][];
int a[N];
LL ans = ;
LL dfs(int o, int u){
for(int i = head[u]; ~i; i = nt[i]){
int v = to[i];
if(v == o) continue;
LL tmp = dfs(u,v) - val[i];
if(tmp > dp[u][]) swap(tmp, dp[u][]);
if(tmp > dp[u][]) swap(tmp, dp[u][]);
}
ans = max(ans, dp[u][]+dp[u][]+a[u]);
return dp[u][] + a[u];
}
int main(){
int n, u, v, w;
scanf("%d", &n);
memset(head, -, sizeof(head));
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i < n; ++i){
scanf("%d%d%d", &u, &v, &w);
add(u,v,w);
add(v,u,w);
}
dfs(,);
cout << ans << endl;
return ;
}

CodeForces 1084D The Fair Nut and the Best Path的更多相关文章

  1. 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 题意: 给出一棵树,走不重复的 ...

  2. Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp

    D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...

  3. 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 题意: 在一棵树内找一条路径,使得从起点 ...

  4. CF1083A The Fair Nut and the Best Path

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

  5. Codeforces 1083E The Fair Nut and Rectangles

    Description 有\(N\)个左下定点为原点的矩阵, 每个矩阵\((x_i,~y_i)\)都有一个数\(a_i\)表示其花费. 没有一个矩阵包含另一个矩阵. 现要你选出若干个矩阵, 使得矩阵组 ...

  6. Codeforces 1083B The Fair Nut and Strings

    Description 给定两个由 \('a'\), \('b'\) 组成的字符串 \(a\), \(b\),以及两个整数 \(n\) 和 \(k\) \(n\) 表示字符串 \(a\),\(b\) ...

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

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

  8. Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费

    题目大意: 给定一棵树 树上每个点有对应的点权 树上每条边有对应的边权 经过一个点可得到点权 经过一条边必须花费边权 即从u到v 最终得分=u的点权-u到v的边权+v的点权 求树上一条路径使得得分最大 ...

  9. 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] ...

随机推荐

  1. MapReduce 编程模型 & WordCount 示例

    学习大数据接触到的第一个编程思想 MapReduce.   前言 之前在学习大数据的时候,很多东西很零散的做了一些笔记,但是都没有好好去整理它们,这篇文章也是对之前的笔记的整理,或者叫输出吧.一来是加 ...

  2. Codeforces Round #192 (Div. 2) (330A) A. Cakeminator

    题意: 如果某一行没有草莓,就可以吃掉这一行,某一列没有也可以吃点这一列,求最多会被吃掉多少块蛋糕. //cf 192 div2 #include <stdio.h> #include & ...

  3. Java悲观锁Pessimistic-Lock常用实现场景

    1:商品库存秒杀采用悲观锁Pessimistic-Lock主要好处是安全,充分利用了数据库的性能来做的一种锁机制. 悲观锁的实现: (1)环境:mysql + jdbctemplate (2)商品表g ...

  4. asp.net core系列 69 Amazon S3 资源文件上传示例

    一.  上传示例 Install-Package AWSSDK.S3 -Version 3.3.104.10 using Amazon; using Amazon.Runtime; using Ama ...

  5. 第五章-处理多窗口 | Electron实战

    本章主要内容: 使用JavaScript Set数据结构跟踪多个窗口 促进主进程和多个渲染器进程之间的通信 使用Node APIs检查应用程序运行在那个平台上 现在,当Fire Sale启动时,它为U ...

  6. 图片格式:gif / png / pg / webp 介绍

    本文引自:https://www.cnblogs.com/changyangzhe/articles/5718285.html GIF介绍 GIF 意为Graphics Interchange for ...

  7. http客户端-性能比较系列-第二篇-多线程

    系列文章: 单线程性能测试:https://www.cnblogs.com/victor2302/p/11077208.html 多线程性能测试:https://www.cnblogs.com/vic ...

  8. 如何成为PHP程序员?

    当今,互联网的蓬勃发展,移动互联网的火热,以及国家提出的“互联网+”.这些趋势可以让我们明显的感觉到互联网的重要,不可替代.网站也是大家最早接触,最早认识的一种新事物.谈到网站,无非最长脸的莫过于PH ...

  9. android ——Tablayout

    Tabs make it easy to explore and switch between different views. 通过TabLayout可以在一个活动中通过滑动或者点击切换到不同的页面 ...

  10. 【0801 | Day 6】Python基础(四)

    Part 13 流程控制之while循环 一.语法 while 条件 code 1 code 2 code 3 ... ​ while True: print('*1'*100) print('*2' ...