Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费
题目大意:
给定一棵树 树上每个点有对应的点权
树上每条边有对应的边权
经过一个点可得到点权 经过一条边必须花费边权
即从u到v 最终得分=u的点权-u到v的边权+v的点权
求树上一条路径使得得分最大
看注释
#include <bits/stdc++.h>
#define LL long long
#define INf 0x3f3f3f3f
using namespace std;
const int N=3e5+;
bool vis[N];
LL w[N], ans;
int n;
struct NODE { int to,nt; LL l; }e[N<<];
int head[N], tot;
void addE(int u,int v,LL l) {
e[tot].to=v, e[tot].l=l;
e[tot].nt=head[u];
head[u]=tot++;
}
void init() {
memset(head,,sizeof(head));
tot=;
}
LL dfs(int u,int fa) {
vis[u]=;
LL ans1=0LL, ans2=0LL;
// ans1由子节点出发的一条路径最大得分 ans2为次大
for(int i=head[u];i;i=e[i].nt) {
int v=e[i].to;
if(v==fa || vis[v]) continue;
LL tmp=dfs(v,u)-e[i].l;
if(tmp>ans1) swap(tmp,ans1);
if(tmp>ans2) swap(tmp,ans2);
}
ans=max(ans,ans1+ans2+w[u]);
// 可由最大得分和次大得分加上u点 得到一条经过u点的路径的最大得分
return ans1+w[u]; // 只返回由u出发的一条路径可得到的最大得分
}
int main()
{
while(~scanf("%d",&n)) {
for(int i=;i<=n;i++) scanf("%I64d",&w[i]);
init();
for(int i=;i<n;i++) {
int u,v; LL l;
scanf("%d%d%I64d",&u,&v,&l);
addE(u,v,l); addE(v,u,l);
}
memset(vis,,sizeof(vis));
ans=0LL;
dfs(,);
printf("%I64d\n",ans);
} return ;
}
Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费的更多相关文章
- Codeforces Round #526 C - The Fair Nut and String /// 组合递推
题目大意: 给定原字符序列 找出其中所有子序列满足 1.序列内字符都为a 2.若有两个以上的字符 则相邻两个字符在原序列中两者之间存在字符b 的数量 将整个字符序列用b分开 此时再得到每个b之间a的数 ...
- 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 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) Solution
A. The Fair Nut and Elevator Solved. 签. #include <bits/stdc++.h> using namespace std; #define ...
- Codeforces Round #526 (Div. 1)
毕竟是上紫之后的第一场div1,还是太菜了啊,看来我要滚回去打div2了. A. The Fair Nut and the Best Path 这题本来是傻逼贪心dfs,结果我越写越麻烦,然后就只有1 ...
- 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 题意: 在一棵树内找一条路径,使得从起点 ...
- CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...
- Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和
Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description ...
随机推荐
- SpringBoot中配置不序列化返回值为null的属性
package com.weiresearch.properties; import com.fasterxml.jackson.annotation.JsonInclude;import com.f ...
- tomcat 相关异常
端口没有被占用却提示Several ports (8005, 8080, 8009) required by Tomcat??? 解决办法:1.运行cmd进入命令行界面:2.运行命令: netsh w ...
- 19、Linux命令对服务器内存进行监控
国际惯例,我们要知道什么是服务器的内存,内存有哪些作用.这里就不做过多介绍,Linux性能监控需要我们对底层要有一定的理解.下面我将会列出我常用的监控内存的工具. vmstat vmstat显示关于进 ...
- kmalloc vs vmalloc
kmalloc分配物理上连续的空间,可以不是整页大小的. vmalloc分配逻辑上连接的空间,可以不是物理上连接的.
- ISA虚拟化的条件
ISA(Instruction Set Architecture) 指令集体系结构,是硬件与软件层之间的接口. 本地系统虚拟机 本地系统虚拟机,就是Bare-Metal虚拟机,直接运行在硬件上,在它上 ...
- nginx新增简单下载站点
配置nginx server { autoindex on;# 显示目录 autoindex_exact_size on;# 显示文件大小 autoindex_localtime on;# 显示文件时 ...
- linux下读取移动硬盘
前提是安装了ntfs-3g软件,系统才能识别到移动硬盘. 第一步.fdisk -l 该命令查看系统识别到的磁盘,如果移动硬盘系统能够识别, 在屏幕上会输出“/dev/sdb1”之类的字样. ...
- eduCF#61 C. Painting the Fence /// DP 选取k段能覆盖的格数
题目大意: 给定n m 接下来给定m个在n范围内的段的左右端 l r 求选取m-2段 最多能覆盖多少格 #include <bits/stdc++.h> using namespace s ...
- 自动化监控系统(二)连接数据库,创建app,添加model,同步数据库
数据库我使用:mysql5.7 程序连接数据库的模块:pymysql 一.创建数据库: dbname:automatedmonitor 二.使用pip安装pymysql,这里我直接在pycharm上安 ...
- 【Codeforces】450 B(div2)
题目链接:http://codeforces.com/problemset/problem/450/B 题意: 求这个的第n项. 题解:$f_{i+1} = f_i - f_{i-1} $ \begi ...