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树,给定的两个字符串就 ...
随机推荐
- react router @4 和 vue路由 详解(一)vue路由基础和使用
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 1.vue路由基础和使用 a.大概目录 我这里建了一个router文件夹,文件夹下有in ...
- day26 第二阶段共享
第二阶段共享--网络编程 一.C/S架构 : 客户端(client)/服务端(sever)框架 B/S架构: 浏览器(brower)/服务端(sever)架构 软件CS架构: 服务端连接到浏览器,QQ ...
- Java中的运算符及表达式
常用的运算符:赋值运算符(=).加法运算符(+).乘法运算符(*).除法运算符(/).括号运算符(( )).余数运算符(%).布尔运算符. 注释符(//).注释的内容为双反斜杠后的内容至换行结束. j ...
- Scrapy结构
http://scrapy-chs.readthedocs.io/zh_CN/1.0/intro/overview.html scrapy 使用Twisted 这个异步网络库来处理网络通信,使用pyt ...
- 如何使用VSTO自动将Excel中的图表复制到Word
如何使用VSTO自动将Excel中的图表复制到Word 原文地址:https://code.msdn.microsoft.com/How-to-copy-Chart-in-Excel-a29f9029 ...
- Remove duplicates from array
//Given a sorted array, remove the duplicates in place such that each element appear only // once an ...
- 7.7 C++基本关联式容器
参考:http://www.weixueyuan.net/view/6404.html 总结: 基本的关联式容器主要有:set.multiset.map和multimap,这四种容器可以分为两组:ma ...
- 浅议APC
0x01 APC中断请求级别 在Intel x86体系结构中,外部硬件中断是通过处理器上的"中断管脚"或者一个称为"本地APIC(local APIC)" ...
- Centos7下mysql5.7.22主从配置
一:服务器配置 1.1修改root密码: ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxx'; 1.2添加远程登陆用户: GRANT ALL PRIV ...
- Linux(centos) 下curl模拟Http get / post请求 [ curl ]
一.get请求 curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...