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树,给定的两个字符串就 ...
随机推荐
- this&super两个关键字的意义和用法
"this",作为一个特殊的关键字,它的规则如下: 1.可以表示构造函数传递.this(a,b)表示调用另外一个构造函数.这里面的this就是一个特殊语法,不是变量,没有什么类型. ...
- MeshLab 编译
1.需要以下: MeshLab 1.3.3 下载地址 http://sourceforge.net/projects/meshlab/files/meshlab Win7 X64 Visual ...
- 单元测试模拟-moq
1.moq 支持 net core 2.moq 通过一个接口类型 可以产生一个新的类 3.举例 //define interface to be mocked public interface ITe ...
- httppost core net
public static string Post(string url, string data, Encoding encoding) { try { HttpWebRequest req = W ...
- SAL-9 获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'
题目描述 获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'CREATE TABLE `dept_man ...
- SpringMVC 接受页面传递参数
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- js 将文本转换为数据 string number
<span class="Span" > <p>123.81</p> <a> dejiw</a> </span&g ...
- jsch上传文件到服务器
需求就是上传文件到服务器,服务器的存储地址由程序决定然后可以自动创建. 使用第三方:jsch JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文 ...
- 7.Python 正则表达式学习笔记
本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优化正则表达式,这些主题请查看其他教程 ...
- MVC扩展之HtmlHelper辅助方法
1.什么是HtmlHelper辅助方法?其实就是HtmlHelper类的扩展方法,如下所示: namespace System.Web.Mvc.Html { public static class F ...