#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<int> vs[maxn];
map<pair<int,int>,int> mp;
int ans=;
void dfs(int x)
{
visit[x]=;
int max1=;
int max2=;
for(int i=;i<vs[x].size();i++)
{ int p=vs[x][i];
if(visit[p]) continue;//cout<<p<<endl;
dfs(p);
int k=dp[p]-mp[{x,p}];
if(k>=max1) max2=max1,max1=k;
else if(k>=max2) max2=k;
}
//cout<<x<<" "<<dp[x]<<endl;
dp[x]=a[x]+max1;
ans=max(ans,dp[x]);
ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
int n; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++) dp[i]=a[i];
for(int i=;i<n;i++)
{
int x,y,z; cin>>x>>y>>z;
vs[x].push_back(y); mp[{x,y}]=z;
vs[y].push_back(x); mp[{y,x}]=z;
}
dfs();
cout<<ans<<endl;
return ;
}

2994ms

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=;
void dfs(int x)
{
visit[x]=;
int max1=;
int max2=;
for(int i=;i<vs[x].size();i++)
{ int p=vs[x][i].first;
if(visit[p]) continue;//cout<<p<<endl;
dfs(p);
int k=dp[p]-vs[x][i].second;
if(k>=max1) max2=max1,max1=k;
else if(k>=max2) max2=k;
}
//cout<<x<<" "<<dp[x]<<endl;
dp[x]=a[x]+max1;
ans=max(ans,dp[x]);
ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
int n; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++) dp[i]=a[i];
for(int i=;i<n;i++)
{
int x,y,z; cin>>x>>y>>z;
vs[x].push_back({y,z}); //mp[{x,y}]=z;
vs[y].push_back({x,z}); //mp[{y,x}]=z;
}
dfs();
cout<<ans<<endl;
return ;
}

2308ms

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=;
void dfs(int x)
{
visit[x]=;
int max1=;
int max2=;
for(int i=;i<vs[x].size();i++)
{ int p=vs[x][i].first;
if(visit[p]) continue;//cout<<p<<endl;
dfs(p);
int k=dp[p]-vs[x][i].second;
if(k>=max1) max2=max1,max1=k;
else if(k>=max2) max2=k;
}
//cout<<x<<" "<<dp[x]<<endl;
dp[x]=a[x]+max1;
ans=max(ans,dp[x]);
ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++) dp[i]=a[i];
for(int i=;i<n;i++)
{
int x,y,z; cin>>x>>y>>z;
vs[x].push_back({y,z}); //mp[{x,y}]=z;
vs[y].push_back({x,z}); //mp[{y,x}]=z;
}
dfs();
cout<<ans<<endl;
return ;
}

700+ms

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=;
void dfs(int x)
{
visit[x]=;
int max1=;
int max2=;
for(int i=;i<vs[x].size();i++)
{ int p=vs[x][i].first;
if(visit[p]) continue;//cout<<p<<endl;
dfs(p);
int k=dp[p]-vs[x][i].second;
if(k>=max1) max2=max1,max1=k;
else if(k>=max2) max2=k;
}
//cout<<x<<" "<<dp[x]<<endl;
dp[x]=a[x]+max1;
ans=max(ans,dp[x]);
ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
/*ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);*/
int n; scanf("%I64d",&n);
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
for(int i=;i<=n;i++) dp[i]=a[i];
for(int i=;i<n;i++)
{
int x,y,z; scanf("%I64d %I64d %I64d",&x,&y,&z);
vs[x].push_back({y,z}); //mp[{x,y}]=z;
vs[y].push_back({x,z}); //mp[{y,x}]=z;
}
dfs();
printf("%I64d\n",ans);
return ;
}

405ms

D. The Fair Nut and the Best Path 树形dp (终于会了)的更多相关文章

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

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

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

  3. CF1083A The Fair Nut and the Best Path

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

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

  5. CodeForces 1084D The Fair Nut and the Best Path

    The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...

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

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

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

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

  8. CF1083E The Fair Nut and Rectangles

    CF1083E The Fair Nut and Rectangles 给定 \(n\) 个平面直角坐标系中左下角为坐标原点,右上角为 \((x_i,\ y_i)\) 的互不包含的矩形,每一个矩形拥有 ...

  9. CF 1083 B. The Fair Nut and Strings

    B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析:  建出trie树,给定的两个字符串就 ...

随机推荐

  1. learning scala 数组和容器

    数组:可变的,可索引的,元素具有相同类型的数据集合 一维数组 scala> val intValueArr = new Array[Int](3)intValueArr: Array[Int] ...

  2. java的小程序在html中的运行测试

    java的小程序在html中的运行测试,打开vs2012,以网站模式打开,生成,调用iis临时服务器运行.

  3. bzoj1935

    题解: x升序排序 y离散化+树状数组 代码: #include<bits/stdc++.h> using namespace std; ; inline int read() { ,f= ...

  4. java⑾

    1.数组: 01.一组 相同数据类型的集合! 02.数组在内存中会 开辟一串连续的空间来保存数据! ***存储30名学生的姓名! 01.姓名 应该用什么数据类型保存??? String02.难道需要创 ...

  5. MySQL 占用cpu 100%

    目前的线上数据库,分为主从两个库,从库用来做比较耗时的数据统计分析. 今天top了一下从库服务器,发现mysqld 在很长一段时间都占用105% cpu,一开始以为是从库在处理主库的binlog. 两 ...

  6. PE文件 01 导入表

    0x01  导入表结构  数据目录表中的第二个成员标记了导入表的RVA和Size大小,由此可以定位到导入表: typedef struct _IMAGE_DATA_DIRECTORY { DWORD ...

  7. AssetBundle自动标签、打包

    using System;using System.Collections.Generic;using System.IO;using UnityEditor;using UnityEngine; / ...

  8. shiro学习(三)权限 authenrication

    主体 主体,即访问应用的用户,在Shiro中使用Subject代表该用户.用户只有授权后才允许访问相应的资源. 资源 在应用中用户可以访问的任何东西,比如访问JSP页面.查看/编辑某些数据.访问某个业 ...

  9. VOOKI:一款免费的Web应用漏洞扫描工具

    Vooki是一款免费且用户界面友好的Web应用漏扫工具,它可以轻松地为你扫描任何Web应用并查找漏洞.Vooki主要包括三个部分,Web应用扫描器,Rest API扫描器以及报告.Web应用扫描器​V ...

  10. Maven Speed Up

    收录架构 proxy代理仓库 不支持仓库搜索功能 收录版本 所有版本 更新时间 每24小时更新一次 使用说明 一.在maven软件中使用 以Maven 3.5.2为例: 打开maven配置文件 ./a ...