D. The Fair Nut and the Best Path 树形dp (终于会了)
#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 (终于会了)的更多相关文章
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- 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 #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 1084D The Fair Nut and the Best Path
The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...
- 【Codeforces 1083A】The Fair Nut and the Best Path
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们最后要的是一条最长的路径. 这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大. 显然如果我们在某一条边上的累计的权值和< ...
- Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费
题目大意: 给定一棵树 树上每个点有对应的点权 树上每条边有对应的边权 经过一个点可得到点权 经过一条边必须花费边权 即从u到v 最终得分=u的点权-u到v的边权+v的点权 求树上一条路径使得得分最大 ...
- CF1083E The Fair Nut and Rectangles
CF1083E The Fair Nut and Rectangles 给定 \(n\) 个平面直角坐标系中左下角为坐标原点,右上角为 \((x_i,\ y_i)\) 的互不包含的矩形,每一个矩形拥有 ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
随机推荐
- pl/sql美化
因为这个问题曾经浪费过俺很多时间,不过今天终于发现一个小技巧,分享给大家, 在上面DDL语句前后加上begin 和 end,哈哈,再美化下试试看,DDL被成功被美化了. 具体如下: begin---① ...
- Fiddler系列教程2:手机抓包图文教程
上篇Fiddler教程,我们教了大家Fiddler安装配置及如何使用Fiddler进行基本的Http抓包及模拟请求,今天给大家介绍下如何使用Fiddler进行手机抓包. 运行环境为Windows 10 ...
- Java第5次
1. 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 显示结果: truetruefalse 总结:在Java中,内容相同的字串常量(“ ...
- oo作业总结报告
oo第一次博客 以前从未真正的写过Java代码,接触Java也只是寒假的时候简单的看了看语法,不懂该如何面向对象,但没事,心里不惧,想着什么都是可以学的(直到真正开始写工程的时候,才发现自己还是太天真 ...
- Docker使用jenkins部署java项目到远程linux(三)
实现功能:从本地提交代码到gogs上,本地的代码会被检测一遍 如果检测通过才能commit成功 然后可以继续执行push命令 .push后在gogs上使用web钩子自动推送到jenkins触发构建,j ...
- 在学习DRF之前
学习DRF之前~~~ 在学习DRF之前~我们要先复习一些知识点~~ FBV和CBV 学习Django的时候~我们已经学习过了CBV以及FBV~~我们来复习一下~~ 什么是FBV和CBV呢~~ FB ...
- 关于MEX函数的说明
reference:http://www.mathworks.com/help/matlab/ref/mex.html .MEX文件是一种可在matlab环境中调用的C(或fortran)语言衍生程序 ...
- 20165326 java第一周学习笔记
第一周学习笔记 一.理论视频学习 1.Java的特点:简单.面向对象.平台无关 2.Java的开发步骤&简单的应用程序: 文本编辑器写入代码 命名类名.java,文件类型所有文件,编码ANSI ...
- day 68 增删改查 语法
1 普通正则 2 分组正则 url(r'/blog/(\d+)/(\d+)',views.blog) blog(request,arq1,arq2) 按照位置传参 3 分组命名 url(r'/ ...
- crontab的定时任务实例
实例1:每1分钟执行一次myCommand * * * * * myCommand 实例2:每小时的第3和第15分钟执行 3,15 * * * * myCommand 实例3:在上午8点到11点的第3 ...