【Codeforces 1083A】The Fair Nut and the Best Path
【链接】 我是链接,点我呀:)
【题意】
题意
【题解】
我们最后要的是一条最长的路径。
这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大。
显然如果我们在某一条边上的累计的权值和=0)
所以如果我们求的是最大的权值和-边权和的话,那么求出来的路径一定不会有中间某个地方走着走着没油的情况
因此我们只要按照树上最长链的类似方法。
求出来,从i的不同子树下的节点到达i节点的点权和减去边权和的最大值和次小值。
对于所有的点,用这两个值的和尝试更新答案即可。
【代码】
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5;
int n;
int a[N+10];
ll f[N+10][2],ans=0;
vector<pair<int,int> > g[N+10];
void dfs(int x,int pre){
int len = g[x].size();
f[x][0] = a[x];
for (int i = 0;i < len;i++){
int y = g[x][i].first,cost = g[x][i].second;
if (y==pre) continue;
dfs(y,x);
if (f[x][0]<f[y][0]-cost+a[x]){
f[x][1] = f[x][0];
f[x][0] = f[y][0]-cost+a[x];
}else{
if (f[x][1]<f[y][0]-cost+a[x]){
f[x][1] = f[y][0]-cost+a[x];
}
}
}
if (f[x][1]>0){
ans = max(ans,f[x][0]+f[x][1]-a[x]);
}else ans = max(ans,f[x][0]);
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >> n;
for (int i = 1;i <= n;i++) cin >>a[i];
for (int i = 1;i <= n-1;i++){
int x,y,z;
cin >> x >> y >> z;
g[x].push_back({y,z});
g[y].push_back({x,z});
}
dfs(1,-1);
cout<<ans<<endl;
return 0;
}
【Codeforces 1083A】The Fair Nut and the Best Path的更多相关文章
- 【Codeforces 1073D】Berland Fair
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们可以从左到右枚举一轮. 定义一个cost表示这一轮花费的钱数 如果cost+a[i]<=T那么就可以买它,并且买下它(模拟题目要求) ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 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一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...
- 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 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- AtCoder Grand Contest 016 F - Games on DAG
题目传送门:https://agc016.contest.atcoder.jp/tasks/agc016_f 题目大意: 给定一个\(N\)点\(M\)边的DAG,\(x_i\)有边连向\(y_i\) ...
- bzoj1854 [Scoi2010]游戏【构图 并查集】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1854 没想到怎么做真是不应该,看到每个武器都有两个属性,应该要想到连边构图的!太不应该了! ...
- Almost Acyclic Graph Codeforces - 915D
以前做过的题都不会了.... 此题做法:优化的暴力 有一个显然的暴力:枚举每一条边试着删掉 注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES) 因此找出图中 ...
- 谷歌编码风格内容,新建一个xml文件,复制进去就可以在eclipse里面用了,命名--eclipse-java-google-style.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <profi ...
- 关于bootstrap table的server分页
首先是bootstrap初始化的表格参数: // 初始化Table oTableInit.Init = function() { $('#booksTable').bootstrapTable({ u ...
- 507 Perfect Number 完美数
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...
- pscp多线程传输文件
前面说过pscp不支持多线程,所以在此特地实现了一个 程序分三个部分: 1.初始化各种参数,涉及getopt函数的使用 2.重新定义scp,实现传递IP然后远程拷贝 3.启动多线程调用scp,涉及多线 ...
- 2019/05/11 JAVA虚拟机原理堆、栈、方法区概念区别
Java堆 堆内存用于存放由new创建的对象和数组.在堆中分配的内存,由java虚拟机自动垃圾回收器来管理.在堆中产生了一个数组或者对象后,还可以在栈中定义一个特殊的变量,这个变量的取值等于数组或者对 ...
- CCF|最大波动|Java|100
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Sc ...
- Android RxJava小结
一.如何使用 在build.gradle中添加依赖 dependencies { api 'io.reactivex:rxandroid:1.2.1' api 'io.reactivex:rxjava ...